Skip to content

Commit 95e3901

Browse files
authored
Merge pull request rails#52830 from Shopify/load-schema-on-migrating-new-database
Load schema when running db:migrate on fresh db
2 parents f6d1039 + 66aacb6 commit 95e3901

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* When running `db:migrate` on a fresh database, load the database schema before running migrations.
2+
3+
*Andrew Novoselac*
4+
15
* Fix an issue where `.left_outer_joins` used with multiple associations that have
26
the same child association but different parents does not join all parents.
37

activerecord/lib/active_record/tasks/database_tasks.rb

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,22 +178,9 @@ def prepare_all
178178
dump_db_configs = []
179179

180180
each_current_configuration(env) do |db_config|
181-
with_temporary_pool(db_config) do
182-
begin
183-
database_initialized = migration_connection_pool.schema_migration.table_exists?
184-
rescue ActiveRecord::NoDatabaseError
185-
create(db_config)
186-
retry
187-
end
181+
database_initialized = initialize_database(db_config)
188182

189-
unless database_initialized
190-
if File.exist?(schema_dump_path(db_config))
191-
load_schema(db_config, ActiveRecord.schema_format, nil)
192-
end
193-
194-
seed = true
195-
end
196-
end
183+
seed = true if database_initialized
197184
end
198185

199186
each_current_environment(env) do |environment|
@@ -259,6 +246,8 @@ def migrate(version = nil)
259246

260247
check_target_version
261248

249+
initialize_database(migration_connection_pool.db_config)
250+
262251
migration_connection_pool.migration_context.migrate(target_version) do |migration|
263252
if version.blank?
264253
scope.blank? || scope == migration.scope
@@ -667,6 +656,26 @@ def check_current_protected_environment!(db_config)
667656
rescue ActiveRecord::NoDatabaseError
668657
end
669658
end
659+
660+
def initialize_database(db_config)
661+
with_temporary_pool(db_config) do
662+
begin
663+
database_already_initialized = migration_connection_pool.schema_migration.table_exists?
664+
rescue ActiveRecord::NoDatabaseError
665+
create(db_config)
666+
retry
667+
end
668+
669+
unless database_already_initialized
670+
schema_dump_path = schema_dump_path(db_config)
671+
if schema_dump_path && File.exist?(schema_dump_path)
672+
load_schema(db_config, ActiveRecord.schema_format, nil)
673+
end
674+
end
675+
676+
!database_already_initialized
677+
end
678+
end
670679
end
671680
end
672681
end

railties/test/application/rake/dbs_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,19 @@ def db_migrate_and_status(expected_database)
350350
db_migrate_and_status database_url_db_name
351351
end
352352

353+
test "db:migrate on new db loads schema" do
354+
app_file "db/schema.rb", <<-RUBY
355+
ActiveRecord::Schema.define(version: 20140423102712) do
356+
create_table(:comments) {}
357+
end
358+
RUBY
359+
360+
rails "db:migrate"
361+
list_tables = lambda { rails("runner", "p ActiveRecord::Base.lease_connection.tables.sort").strip }
362+
363+
assert_equal "[\"ar_internal_metadata\", \"comments\", \"schema_migrations\"]", list_tables[]
364+
end
365+
353366
def db_schema_dump
354367
Dir.chdir(app_path) do
355368
args = ["generate", "model", "book", "title:string"]

0 commit comments

Comments
 (0)