Repository: rails/rails
File: activerecord/lib/active_record/tasks/database_tasks.rb (Ruby)

Code before:
      end

      def reconstruct_from_schema(db_config, file = nil) # :nodoc:
        file ||= schema_dump_path(db_config, db_config.schema_format)

        check_schema_file(file) if file

        with_temporary_pool(db_config, clobber: true) do
          if schema_up_to_date?(db_config, nil, file)
            truncate_tables(db_config) unless ENV["SKIP_TEST_DATABASE_TRUNCATE"]
          else
            purge(db_config)
            load_schema(db_config, db_config.schema_format, file)
          end
        rescue ActiveRecord::NoDatabaseError
          create(db_config)
          load_schema(db_config, db_config.schema_format, file)
        end
      end

      def dump_all
        seen_schemas = []
        
        with_temporary_pool_for_each do |pool|
          db_config = pool.db_config
          next if seen_schemas.include?(db_config.schema_dump)

          ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, ENV["SCHEMA_FORMAT"] || db_config.schema_format)
          seen_schemas << db_config.schema_dump
        end
      end

      def dump_schema(db_config, format = db_config.schema_format) # :nodoc:
        return unless db_config.schema_dump

        require "active_record/schema_dumper"
        filename = schema_dump_path(db_config, format)
        return unless filename

        FileUtils.mkdir_p(db_dir)
        case format.to_sym
        when :ruby
          File.open(filename, "w:utf-8") do |file|
            ActiveRecord::SchemaDumper.dump(migration_connection_pool, file)
          end
        when :sql
          structure_dump(db_config, filename)
          if migration_connection_pool.schema_migration.table_exists?
            File.open(filename, "a") do |f|
              f.puts migration_connection.dump_schema_versions
              f.print "\n"

Review comment (suggestion):
Reading through `dump_schema` again, I'm wondering if we should handle the `filename = schema_dump_path` case 🤔

I think we would not be de-duping right now if one database was configured with `db/structure.sql` and another was `structure.sql`, but `schema_dump_path()` would end up coercing them both into `db/structure.sql`.

Code after:
      end

      def reconstruct_from_schema(db_config, file = nil) # :nodoc:
        file ||= schema_dump_path(db_config, db_config.schema_format)

        check_schema_file(file) if file

        with_temporary_pool(db_config, clobber: true) do
          if schema_up_to_date?(db_config, nil, file)
            truncate_tables(db_config) unless ENV["SKIP_TEST_DATABASE_TRUNCATE"]
          else
            purge(db_config)
            load_schema(db_config, db_config.schema_format, file)
          end
        rescue ActiveRecord::NoDatabaseError
          create(db_config)
          load_schema(db_config, db_config.schema_format, file)
        end
      end

      def dump_all
        seen_schemas = []

        ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
          schema_path = schema_dump_path(db_config, ENV["SCHEMA_FORMAT"] || db_config.schema_format)

          next if seen_schemas.include?(schema_path)

          ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, ENV["SCHEMA_FORMAT"] || db_config.schema_format)
          seen_schemas << schema_path
        end
      end

      def dump_schema(db_config, format = db_config.schema_format) # :nodoc:
        return unless db_config.schema_dump

        require "active_record/schema_dumper"
        filename = schema_dump_path(db_config, format)
        return unless filename

        with_temporary_pool(db_config) do |pool|
          FileUtils.mkdir_p(db_dir)
          case format.to_sym
          when :ruby
            File.open(filename, "w:utf-8") do |file|
              ActiveRecord::SchemaDumper.dump(pool, file)
            end
          when :sql
            structure_dump(db_config, filename)
            if pool.schema_migration.table_exists?
              File.open(filename, "a") do |f|