diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index f187835bb0ae3..82949dde4891a 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* When running `db:migrate` on a fresh database, load the database schema before running migrations. + + *Andrew Novoselac* + * Deprecate `unsigned_float` and `unsigned_decimal` short-hand column methods. As of MySQL 8.0.17, the UNSIGNED attribute is deprecated for columns of type FLOAT, DOUBLE, diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 9fadc7184b594..a9cafeb641c82 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -179,20 +179,9 @@ def prepare_all each_current_configuration(env) do |db_config| with_temporary_pool(db_config) do - begin - database_initialized = migration_connection_pool.schema_migration.table_exists? - rescue ActiveRecord::NoDatabaseError - create(db_config) - retry - end + database_initialized = initialize_database(db_config) - unless database_initialized - if File.exist?(schema_dump_path(db_config)) - load_schema(db_config, ActiveRecord.schema_format, nil) - end - - seed = true - end + seed = true if database_initialized end end @@ -259,6 +248,8 @@ def migrate(version = nil) check_target_version + initialize_database(migration_connection_pool.db_config) + migration_connection_pool.migration_context.migrate(target_version) do |migration| if version.blank? scope.blank? || scope == migration.scope @@ -667,6 +658,25 @@ def check_current_protected_environment!(db_config) rescue ActiveRecord::NoDatabaseError end end + + def initialize_database(db_config) + with_temporary_pool(db_config) do + begin + database_already_initialized = migration_connection_pool.schema_migration.table_exists? + rescue ActiveRecord::NoDatabaseError + create(db_config) + retry + end + + unless database_already_initialized + if File.exist?(schema_dump_path(db_config)) + load_schema(db_config, ActiveRecord.schema_format, nil) + end + end + + !database_already_initialized + end + end end end end diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index f99980fceba39..8b9d3cb83ff12 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -350,6 +350,19 @@ def db_migrate_and_status(expected_database) db_migrate_and_status database_url_db_name end + test "db:migrate on new db loads schema" do + app_file "db/schema.rb", <<-RUBY + ActiveRecord::Schema.define(version: 20140423102712) do + create_table(:comments) {} + end + RUBY + + rails "db:migrate" + list_tables = lambda { rails("runner", "p ActiveRecord::Base.lease_connection.tables.sort").strip } + + assert_equal "[\"ar_internal_metadata\", \"comments\", \"schema_migrations\"]", list_tables[] + end + def db_schema_dump Dir.chdir(app_path) do args = ["generate", "model", "book", "title:string"]