Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for interleaving migrations by storing which migrations h…
…ave run in the new schema_migrations table. Closes #11493 [jordi] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9244 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
1 parent
78c2d9f
commit 8a5a9dc
Showing
15 changed files
with
204 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -232,33 +232,41 @@ def structure_dump | |
|
||
# Should not be called normally, but this operation is non-destructive. | ||
# The migrations module handles this automatically. | ||
def initialize_schema_information(current_version=0) | ||
begin | ||
execute "CREATE TABLE #{quote_table_name(ActiveRecord::Migrator.schema_info_table_name)} (version #{type_to_sql(:string)})" | ||
execute "INSERT INTO #{quote_table_name(ActiveRecord::Migrator.schema_info_table_name)} (version) VALUES(#{current_version})" | ||
rescue ActiveRecord::StatementInvalid | ||
# Schema has been initialized, make sure version is a string | ||
version_column = columns(:schema_info).detect { |c| c.name == "version" } | ||
|
||
# can't just alter the table, since SQLite can't deal | ||
unless version_column.type == :string | ||
version = ActiveRecord::Migrator.current_version | ||
execute "DROP TABLE #{quote_table_name(ActiveRecord::Migrator.schema_info_table_name)}" | ||
initialize_schema_information(version) | ||
def initialize_schema_migrations_table | ||
sm_table = ActiveRecord::Migrator.schema_migrations_table_name | ||
|
||
unless tables.detect { |t| t == sm_table } | ||
create_table(sm_table, :id => false) do |schema_migrations_table| | ||
schema_migrations_table.column :version, :string, :null => false | ||
end | ||
end | ||
end | ||
add_index sm_table, :version, :unique => true, | ||
:name => 'unique_schema_migrations' | ||
|
||
# Backwards-compatibility: if we find schema_info, assume we've | ||
# migrated up to that point: | ||
si_table = Base.table_name_prefix + 'schema_info' + Base.table_name_suffix | ||
|
||
if tables.detect { |t| t == si_table } | ||
|
||
def dump_schema_information #:nodoc: | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
robmckinnon
|
||
begin | ||
if (current_schema = ActiveRecord::Migrator.current_version) > 0 | ||
return "INSERT INTO #{quote_table_name(ActiveRecord::Migrator.schema_info_table_name)} (version) VALUES (#{current_schema})" | ||
old_version = select_value("SELECT version FROM #{quote_table_name(si_table)}").to_i | ||
assume_migrated_upto_version(old_version) | ||
drop_table(si_table) | ||
end | ||
rescue ActiveRecord::StatementInvalid | ||
# No Schema Info | ||
end | ||
end | ||
|
||
def assume_migrated_upto_version(version) | ||
sm_table = quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name) | ||
migrated = select_values("SELECT version FROM #{sm_table}").map(&:to_i) | ||
versions = Dir['db/migrate/[0-9]*_*.rb'].map do |filename| | ||
filename.split('/').last.split('_').first.to_i | ||
end | ||
|
||
execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')" unless migrated.include?(version.to_i) | ||
(versions - migrated).select { |v| v < version.to_i }.each do |v| | ||
execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')" | ||
end | ||
end | ||
|
||
def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc: | ||
if native = native_database_types[type] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Removing this method breaks db:structure:dump which breaks db:test:clone_structure; see http://rails.lighthouseapp.com/projects/8994/tickets/21. Not sure how to restore it using the new schema_migrations table.