From cff747d304f008c103a7ba92440400a9a0848bee Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 9 Dec 2012 15:52:28 -0700 Subject: Move to the schema-migrations-metadata branch. Pending work on graceful app upgrades. Revert "Merge pull request #8439 from joshsusser/fixes" This reverts commit ce8ac39338f86388e70356b3a470b3ea443802ae, reversing changes made to b0e7b6f67c984d4b1502e801781ed75fad681633. Revert "Merge pull request #8431 from joshsusser/schemadump" This reverts commit 036d3e1c2b65c4b8cbd23de2e20ad67b9b756182, reversing changes made to 0c692f4d121792117b6a71e5ed590a31c3b9d12e. Revert "Merge branch 'joshsusser-master' into merge" This reverts commit 0c692f4d121792117b6a71e5ed590a31c3b9d12e, reversing changes made to 2e299fca715b083a60222a85e48f9d3b8dd8ce93. Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb activerecord/test/cases/schema_dumper_test.rb --- .../abstract/schema_statements.rb | 8 ++--- activerecord/lib/active_record/migration.rb | 24 ++++--------- activerecord/lib/active_record/schema.rb | 40 ++++++---------------- activerecord/lib/active_record/schema_dumper.rb | 21 +++--------- activerecord/lib/active_record/schema_migration.rb | 39 ++------------------- 5 files changed, 30 insertions(+), 102 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 1586e84a25..f1e42dfbbe 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, fingerprint, name) VALUES ('#{sm.version}',CURRENT_TIMESTAMP,'#{sm.fingerprint}','#{sm.name}');" - }.join("\n\n") + "INSERT INTO #{sm_table} (version) VALUES ('#{sm.version}');" + }.join "\n\n" end # Should not be called normally, but this operation is non-destructive. @@ -512,7 +512,7 @@ module ActiveRecord end unless migrated.include?(version) - ActiveRecord::SchemaMigration.create!(:version => version, :migrated_at => Time.now) + execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')" 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 - ActiveRecord::SchemaMigration.create!(:version => v, :migrated_at => Time.now) + execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')" inserted << v end end diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 4ce276d4bf..22347fcaef 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -1,6 +1,5 @@ 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. @@ -555,10 +554,6 @@ module ActiveRecord delegate :migrate, :announce, :write, :to => :migration - def fingerprint - @fingerprint ||= Digest::MD5.hexdigest(File.read(filename)) - end - private def migration @@ -729,7 +724,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) + record_version_state_after_migrating(target.version) end end @@ -752,7 +747,7 @@ module ActiveRecord begin ddl_transaction do migration.migrate(@direction) - record_version_state_after_migrating(migration) + record_version_state_after_migrating(migration.version) end rescue => e canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : "" @@ -810,18 +805,13 @@ module ActiveRecord raise DuplicateMigrationVersionError.new(version) if version end - def record_version_state_after_migrating(target) + def record_version_state_after_migrating(version) if down? - migrated.delete(target.version) - ActiveRecord::SchemaMigration.where(:version => target.version.to_s).delete_all + migrated.delete(version) + ActiveRecord::SchemaMigration.where(:version => version.to_s).delete_all else - 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+_/,'') - ) + migrated << version + ActiveRecord::SchemaMigration.create!(:version => version.to_s) end end diff --git a/activerecord/lib/active_record/schema.rb b/activerecord/lib/active_record/schema.rb index 44b7eb424b..3259dbbd80 100644 --- a/activerecord/lib/active_record/schema.rb +++ b/activerecord/lib/active_record/schema.rb @@ -39,45 +39,27 @@ module ActiveRecord end def define(info, &block) # :nodoc: - @using_deprecated_version_setting = info[:version].present? - SchemaMigration.drop_table - initialize_schema_migrations_table - instance_eval(&block) - # 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 + unless info[:version].blank? + initialize_schema_migrations_table + assume_migrated_upto_version(info[:version], migrations_paths) + end 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.). - 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. + # The +info+ hash is optional, and if given is used to define metadata + # about the current schema (currently, only the schema's version): # - # 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) + # ActiveRecord::Schema.define(version: 20380119000001) do + # ... + # end + def self.define(info={}, &block) + new.define(info, &block) end end end diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 194a77ca16..36bde44e7c 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -24,7 +24,6 @@ module ActiveRecord def dump(stream) header(stream) - migrations(stream) tables(stream) trailer(stream) stream @@ -39,11 +38,13 @@ module ActiveRecord end def header(stream) + define_params = @version ? "version: #{@version}" : "" + if stream.respond_to?(:external_encoding) && stream.external_encoding stream.puts "# encoding: #{stream.external_encoding.name}" end - header_text = < 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 + unless connection.table_exists?(table_name) 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 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.table_exists?(table_name) - if connection.index_exists?(table_name, "version", :unique => true, :name => index_name) - connection.remove_index(table_name, :name => index_name) - end + connection.remove_index table_name, :name => index_name connection.drop_table(table_name) end end @@ -53,17 +32,5 @@ module ActiveRecord def version super.to_i end - - # Construct ruby source to include in schema.rb dump for this migration. - # Pass a string of spaces as +indent+ to allow calling code to control how deeply indented the line is. - # The generated line includes the migration version, fingerprint, and name. Either fingerprint or name - # can be an empty string. - # - # Example output: - # - # migration 20121129235959, "ee4be703f9e6e2fc0f4baddebe6eb8f7", "add_magic_power_to_unicorns" - def schema_line(indent) - %Q(#{indent}migration %s, "%s", "%s") % [version, fingerprint, name] - end end end -- cgit v1.2.3