From 0a5afa229d769bce9e221f34053bb93b60817a5a Mon Sep 17 00:00:00 2001 From: Josh Susser Date: Sat, 1 Dec 2012 18:32:23 -0800 Subject: Add metadata to schema_migrations migrated_at: timestamp when migration run fingerprint: md5 hash of migration source name: filename without version or extension --- .../abstract/schema_statements.rb | 6 ++--- activerecord/lib/active_record/migration.rb | 24 ++++++++++++------ activerecord/lib/active_record/schema_migration.rb | 29 +++++++++++++++++++--- 3 files changed, 45 insertions(+), 14 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index f1e42dfbbe..7802cb02d8 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -490,7 +490,7 @@ module ActiveRecord sm_table = ActiveRecord::Migrator.schema_migrations_table_name ActiveRecord::SchemaMigration.order('version').map { |sm| - "INSERT INTO #{sm_table} (version) VALUES ('#{sm.version}');" + "INSERT INTO #{sm_table} (version, migrated_at, name) VALUES ('#{sm.version}',LOCALTIMESTAMP,'#{sm.name}');" }.join "\n\n" end @@ -512,7 +512,7 @@ module ActiveRecord end unless migrated.include?(version) - execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')" + ActiveRecord::SchemaMigration.create!(:version => version, :migrated_at => Time.now) end inserted = Set.new @@ -520,7 +520,7 @@ module ActiveRecord if inserted.include?(v) raise "Duplicate migration #{v}. Please renumber your migrations to resolve the conflict." elsif v < version - execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')" + ActiveRecord::SchemaMigration.create!(:version => v, :migrated_at => Time.now) inserted << v end end diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 22347fcaef..4ce276d4bf 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -1,5 +1,6 @@ require "active_support/core_ext/class/attribute_accessors" require 'set' +require 'digest/md5' module ActiveRecord # Exception that can be raised to stop migrations from going backwards. @@ -554,6 +555,10 @@ module ActiveRecord delegate :migrate, :announce, :write, :to => :migration + def fingerprint + @fingerprint ||= Digest::MD5.hexdigest(File.read(filename)) + end + private def migration @@ -724,7 +729,7 @@ module ActiveRecord raise UnknownMigrationVersionError.new(@target_version) if target.nil? unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i)) target.migrate(@direction) - record_version_state_after_migrating(target.version) + record_version_state_after_migrating(target) end end @@ -747,7 +752,7 @@ module ActiveRecord begin ddl_transaction do migration.migrate(@direction) - record_version_state_after_migrating(migration.version) + record_version_state_after_migrating(migration) end rescue => e canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : "" @@ -805,13 +810,18 @@ module ActiveRecord raise DuplicateMigrationVersionError.new(version) if version end - def record_version_state_after_migrating(version) + def record_version_state_after_migrating(target) if down? - migrated.delete(version) - ActiveRecord::SchemaMigration.where(:version => version.to_s).delete_all + migrated.delete(target.version) + ActiveRecord::SchemaMigration.where(:version => target.version.to_s).delete_all else - migrated << version - ActiveRecord::SchemaMigration.create!(:version => version.to_s) + migrated << target.version + ActiveRecord::SchemaMigration.create!( + :version => target.version.to_s, + :migrated_at => Time.now, + :fingerprint => target.fingerprint, + :name => File.basename(target.filename,'.rb').gsub(/^\d+_/,'') + ) end end diff --git a/activerecord/lib/active_record/schema_migration.rb b/activerecord/lib/active_record/schema_migration.rb index 9830abe7d8..fd226d5eba 100644 --- a/activerecord/lib/active_record/schema_migration.rb +++ b/activerecord/lib/active_record/schema_migration.rb @@ -14,17 +14,38 @@ module ActiveRecord end def self.create_table - unless connection.table_exists?(table_name) + if connection.table_exists?(table_name) + cols = connection.columns(table_name).collect { |col| col.name } + unless cols.include?("migrated_at") + connection.add_column(table_name, "migrated_at", :datetime) + q_table_name = connection.quote_table_name(table_name) + q_timestamp = connection.quoted_date(Time.now) + connection.update("UPDATE #{q_table_name} SET migrated_at = '#{q_timestamp}' WHERE migrated_at IS NULL") + connection.change_column(table_name, "migrated_at", :datetime, :null => false) + end + unless cols.include?("fingerprint") + connection.add_column(table_name, "fingerprint", :string, :limit => 32) + end + unless cols.include?("name") + connection.add_column(table_name, "name", :string) + end + else connection.create_table(table_name, :id => false) do |t| - t.column :version, :string, :null => false + t.column "version", :string, :null => false + t.column "migrated_at", :datetime, :null => false + t.column "fingerprint", :string, :limit => 32 + t.column "name", :string end - connection.add_index table_name, :version, :unique => true, :name => index_name + connection.add_index(table_name, "version", :unique => true, :name => index_name) end + reset_column_information end def self.drop_table + if connection.index_exists?(table_name, "version", :unique => true, :name => index_name) + connection.remove_index(table_name, :name => index_name) + end if connection.table_exists?(table_name) - connection.remove_index table_name, :name => index_name connection.drop_table(table_name) end end -- cgit v1.2.3 From f02d2185ebbe01f455a9a91216ff7094b014ea72 Mon Sep 17 00:00:00 2001 From: Josh Susser Date: Sun, 2 Dec 2012 21:16:32 -0800 Subject: Add migration history to schema.rb dump --- .../abstract/schema_statements.rb | 4 +-- activerecord/lib/active_record/schema.rb | 40 ++++++++++++++++------ activerecord/lib/active_record/schema_dumper.rb | 17 +++++++-- activerecord/lib/active_record/schema_migration.rb | 12 +++++++ 4 files changed, 58 insertions(+), 15 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 7802cb02d8..a470e8de07 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -490,8 +490,8 @@ module ActiveRecord sm_table = ActiveRecord::Migrator.schema_migrations_table_name ActiveRecord::SchemaMigration.order('version').map { |sm| - "INSERT INTO #{sm_table} (version, migrated_at, name) VALUES ('#{sm.version}',LOCALTIMESTAMP,'#{sm.name}');" - }.join "\n\n" + "INSERT INTO #{sm_table} (version, migrated_at, fingerprint, name) VALUES ('#{sm.version}',LOCALTIMESTAMP,'#{sm.fingerprint}','#{sm.name}');" + }.join("\n\n") end # Should not be called normally, but this operation is non-destructive. diff --git a/activerecord/lib/active_record/schema.rb b/activerecord/lib/active_record/schema.rb index eaa4aa7086..7ec06e699b 100644 --- a/activerecord/lib/active_record/schema.rb +++ b/activerecord/lib/active_record/schema.rb @@ -34,27 +34,45 @@ module ActiveRecord end def define(info, &block) + @using_deprecated_version_setting = info[:version].present? + SchemaMigration.drop_table + initialize_schema_migrations_table + instance_eval(&block) - unless info[:version].blank? - initialize_schema_migrations_table - assume_migrated_upto_version(info[:version], migrations_paths) - end + # handle files from pre-4.0 that used :version option instead of dumping migration table + assume_migrated_upto_version(info[:version], migrations_paths) if @using_deprecated_version_setting end # Eval the given block. All methods available to the current connection # adapter are available within the block, so you can easily use the # database definition DSL to build up your schema (+create_table+, # +add_index+, etc.). - # - # The +info+ hash is optional, and if given is used to define metadata - # about the current schema (currently, only the schema's version): - # - # ActiveRecord::Schema.define(version: 20380119000001) do - # ... - # end def self.define(info={}, &block) new.define(info, &block) end + + # Create schema migration history. Include migration statements in a block to this method. + # + # migrations do + # migration 20121128235959, "44f1397e3b92442ca7488a029068a5ad", "add_horn_color_to_unicorns" + # migration 20121129235959, "4a1eb3965d94406b00002b370854eae8", "add_magic_power_to_unicorns" + # end + def migrations + raise(ArgumentError, "Can't set migrations while using :version option") if @using_deprecated_version_setting + yield + end + + # Add a migration to the ActiveRecord::SchemaMigration table. + # + # The +version+ argument is an integer. + # The +fingerprint+ and +name+ arguments are required but may be empty strings. + # The migration's +migrated_at+ attribute is set to the current time, + # instead of being set explicitly as an argument to the method. + # + # migration 20121129235959, "4a1eb3965d94406b00002b370854eae8", "add_magic_power_to_unicorns" + def migration(version, fingerprint, name) + SchemaMigration.create!(version: version, migrated_at: Time.now, fingerprint: fingerprint, name: name) + end end end diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 36bde44e7c..73c0f5b9eb 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -24,6 +24,7 @@ module ActiveRecord def dump(stream) header(stream) + migrations(stream) tables(stream) trailer(stream) stream @@ -44,7 +45,7 @@ module ActiveRecord stream.puts "# encoding: #{stream.external_encoding.name}" end - stream.puts <
Date: Mon, 3 Dec 2012 21:26:35 -0800 Subject: style cleanup --- activerecord/lib/active_record/schema_migration.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/schema_migration.rb b/activerecord/lib/active_record/schema_migration.rb index 4464f54145..b2ae369eb6 100644 --- a/activerecord/lib/active_record/schema_migration.rb +++ b/activerecord/lib/active_record/schema_migration.rb @@ -31,10 +31,10 @@ module ActiveRecord end else connection.create_table(table_name, :id => false) do |t| - t.column "version", :string, :null => false - t.column "migrated_at", :datetime, :null => false - t.column "fingerprint", :string, :limit => 32 - t.column "name", :string + t.column :version, :string, :null => false + t.column :migrated_at, :datetime, :null => false + t.column :fingerprint, :string, :limit => 32 + t.column :name, :string end connection.add_index(table_name, "version", :unique => true, :name => index_name) end -- cgit v1.2.3