Skip to content

Commit

Permalink
When running db:migrate on a fresh database, load the database schema…
Browse files Browse the repository at this point in the history
… before running migrations.

If we have an existing schema file and we run db:migrate before setting up the database, we will not load the schema before running migrations, so when we dump the schema will overwrite the existing schema file and lose the tables define there. Instead, we should check if the db is setup and if it is not lload the schema, before running migrations.
  • Loading branch information
andrewn617 committed Sep 7, 2024
1 parent 1f64e4d commit bbbe7c0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
36 changes: 23 additions & 13 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
13 changes: 13 additions & 0 deletions railties/test/application/rake/dbs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit bbbe7c0

Please sign in to comment.