mirror of https://github.com/rails/rails
When running db:migrate on a fresh database, load the database schema 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.
This commit is contained in:
parent
9356db3d4b
commit
66aacb6333
|
@ -1,3 +1,7 @@
|
|||
* When running `db:migrate` on a fresh database, load the database schema before running migrations.
|
||||
|
||||
*Andrew Novoselac*
|
||||
|
||||
* Fix an issue where `.left_outer_joins` used with multiple associations that have
|
||||
the same child association but different parents does not join all parents.
|
||||
|
||||
|
|
|
@ -178,22 +178,9 @@ module ActiveRecord
|
|||
dump_db_configs = []
|
||||
|
||||
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
|
||||
end
|
||||
seed = true if database_initialized
|
||||
end
|
||||
|
||||
each_current_environment(env) do |environment|
|
||||
|
@ -259,6 +246,8 @@ module ActiveRecord
|
|||
|
||||
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 +656,26 @@ module ActiveRecord
|
|||
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
|
||||
schema_dump_path = schema_dump_path(db_config)
|
||||
if schema_dump_path && File.exist?(schema_dump_path)
|
||||
load_schema(db_config, ActiveRecord.schema_format, nil)
|
||||
end
|
||||
end
|
||||
|
||||
!database_already_initialized
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -350,6 +350,19 @@ module ApplicationTests
|
|||
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"]
|
||||
|
|
Loading…
Reference in New Issue