rolling out migrated_at until I can fix the build

This commit is contained in:
Aaron Patterson 2010-12-01 17:08:01 -08:00
parent c15c14563e
commit 3ec212e3c9
8 changed files with 25 additions and 156 deletions

@ -1,9 +1,5 @@
*Rails 3.1.0 (unreleased)* *Rails 3.1.0 (unreleased)*
* The `schema_migrations` table now contains a column `name` which stores the
name of the migration that was executed, and `migrated_at` which stores the date
when the migration was executed. (Thanks Josh Susser!)
* ActiveRecord::Base#dup and ActiveRecord::Base#clone semantics have changed * ActiveRecord::Base#dup and ActiveRecord::Base#clone semantics have changed
to closer match normal Ruby dup and clone semantics. to closer match normal Ruby dup and clone semantics.

@ -422,21 +422,9 @@ def dump_schema_information #:nodoc:
def initialize_schema_migrations_table def initialize_schema_migrations_table
sm_table = ActiveRecord::Migrator.schema_migrations_table_name sm_table = ActiveRecord::Migrator.schema_migrations_table_name
if table_exists?(sm_table) unless table_exists?(sm_table)
cols = columns(sm_table).collect { |col| col.name }
unless cols.include?("migrated_at")
add_column sm_table, :migrated_at, :datetime
update "UPDATE #{quote_table_name(sm_table)} SET migrated_at = '#{quoted_date(Time.now)}' WHERE migrated_at IS NULL"
change_column sm_table, :migrated_at, :datetime, :null => false
end
unless cols.include?("name")
add_column sm_table, :name, :string, :null => false, :default => ""
end
else
create_table(sm_table, :id => false) do |schema_migrations_table| create_table(sm_table, :id => false) do |schema_migrations_table|
schema_migrations_table.column :version, :string, :null => false schema_migrations_table.column :version, :string, :null => false
schema_migrations_table.column :name, :string, :null => false, :default => ""
schema_migrations_table.column :migrated_at, :datetime, :null => false
end end
add_index sm_table, :version, :unique => true, add_index sm_table, :version, :unique => true,
:name => "#{Base.table_name_prefix}unique_schema_migrations#{Base.table_name_suffix}" :name => "#{Base.table_name_prefix}unique_schema_migrations#{Base.table_name_suffix}"
@ -464,7 +452,7 @@ def assume_migrated_upto_version(version, migrations_path = ActiveRecord::Migrat
end end
unless migrated.include?(version) unless migrated.include?(version)
execute "INSERT INTO #{sm_table} (version,migrated_at) VALUES ('#{version}','#{Time.now.to_s(:db)}')" execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')"
end end
inserted = Set.new inserted = Set.new
@ -472,7 +460,7 @@ def assume_migrated_upto_version(version, migrations_path = ActiveRecord::Migrat
if inserted.include?(v) if inserted.include?(v)
raise "Duplicate migration #{v}. Please renumber your migrations to resolve the conflict." raise "Duplicate migration #{v}. Please renumber your migrations to resolve the conflict."
elsif v < version elsif v < version
execute "INSERT INTO #{sm_table} (version,migrated_at) VALUES ('#{v}','#{Time.now.to_s(:db)}')" execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')"
inserted << v inserted << v
end end
end end

@ -628,7 +628,7 @@ def run
raise UnknownMigrationVersionError.new(@target_version) if target.nil? raise UnknownMigrationVersionError.new(@target_version) if target.nil?
unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i)) unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i))
target.migrate(@direction) target.migrate(@direction)
record_version_state_after_migrating(target) record_version_state_after_migrating(target.version)
end end
end end
@ -664,7 +664,7 @@ def migrate
begin begin
ddl_transaction do ddl_transaction do
migration.migrate(@direction) migration.migrate(@direction)
record_version_state_after_migrating(migration) record_version_state_after_migrating(migration.version)
end end
rescue => e rescue => e
canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : "" canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : ""
@ -690,20 +690,16 @@ def migrated
end end
private private
def record_version_state_after_migrating(target) def record_version_state_after_migrating(version)
table = Arel::Table.new(self.class.schema_migrations_table_name) table = Arel::Table.new(self.class.schema_migrations_table_name)
@migrated_versions ||= [] @migrated_versions ||= []
if down? if down?
@migrated_versions.delete(target.version) @migrated_versions.delete(version)
table.where(table["version"].eq(target.version.to_s)).delete table.where(table["version"].eq(version.to_s)).delete
else else
@migrated_versions.push(target.version).sort! @migrated_versions.push(version).sort!
table.insert( table.insert table["version"] => version.to_s
table["version"] => target.version.to_s,
table["name"] => File.basename(target.filename,'.rb').gsub(/^\d+_/,''),
table["migrated_at"] => Time.now
)
end end
end end

@ -46,24 +46,13 @@ def migrations_path
# ... # ...
# end # end
def self.define(info={}, &block) def self.define(info={}, &block)
Base.connection.drop_table(ActiveRecord::Migrator.schema_migrations_table_name)
initialize_schema_migrations_table
schema = new schema = new
schema.instance_eval(&block) schema.instance_eval(&block)
assume_migrated_upto_version(info[:version], schema.migrations_path) unless info[:version].blank? unless info[:version].blank?
end initialize_schema_migrations_table
assume_migrated_upto_version(info[:version], schema.migrations_path)
def migration(version, name="", options={}) end
name, options = "", name if name.is_a?(Hash)
table = Arel::Table.new(ActiveRecord::Migrator.schema_migrations_table_name)
table.insert(
table["version"] => version,
table["name"] => name,
table["migrated_at"] => Time.now
)
end end
end end
end end

@ -24,7 +24,6 @@ def self.dump(connection=ActiveRecord::Base.connection, stream=STDOUT)
def dump(stream) def dump(stream)
header(stream) header(stream)
migrations(stream)
tables(stream) tables(stream)
trailer(stream) trailer(stream)
stream stream
@ -35,9 +34,12 @@ def dump(stream)
def initialize(connection) def initialize(connection)
@connection = connection @connection = connection
@types = @connection.native_database_types @types = @connection.native_database_types
@version = Migrator::current_version rescue nil
end end
def header(stream) def header(stream)
define_params = @version ? ":version => #{@version}" : ""
stream.puts <<HEADER stream.puts <<HEADER
# This file is auto-generated from the current state of the database. Instead # This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to # of editing this file, please use the migrations feature of Active Record to
@ -51,7 +53,7 @@ def header(stream)
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define do ActiveRecord::Schema.define(#{define_params}) do
HEADER HEADER
end end
@ -60,16 +62,6 @@ def trailer(stream)
stream.puts "end" stream.puts "end"
end end
def migrations(stream)
rows = @connection.select_all("SELECT * FROM #{@connection.quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name)}")
rows.each do |migration|
line = %Q( migration "#{migration['version']}")
line << %Q(, "#{migration['name']}") unless migration['name'].blank?
stream.puts line
end
stream.puts ""
end
def tables(stream) def tables(stream)
@connection.tables.sort.each do |tbl| @connection.tables.sort.each do |tbl|
next if ['schema_migrations', ignore_tables].flatten.any? do |ignored| next if ['schema_migrations', ignore_tables].flatten.any? do |ignored|

@ -39,39 +39,4 @@ def test_schema_raises_an_error_for_invalid_column_type
end end
end end
class ActiveRecordSchemaMigrationsTest < ActiveRecordSchemaTest
def setup
super
@sm_table = ActiveRecord::Migrator.schema_migrations_table_name
@connection.execute "DELETE FROM #{@connection.quote_table_name(@sm_table)}"
end
def test_migration_adds_row_to_migrations_table
schema = ActiveRecord::Schema.new
schema.migration("123001")
schema.migration("123002", "add_magic_power_to_unicorns")
rows = @connection.select_all("SELECT * FROM #{@connection.quote_table_name(@sm_table)}")
assert_equal 2, rows.length
assert_equal "123001", rows[0]["version"]
assert_equal "", rows[0]["name"]
assert_not_nil(rows[0]["migrated_at"])
assert_equal "123002", rows[1]["version"]
assert_equal "add_magic_power_to_unicorns", rows[1]["name"]
assert_not_nil(rows[1]["migrated_at"])
end
def test_define_clears_schema_migrations
assert_nothing_raised do
ActiveRecord::Schema.define do
migration("123001")
end
ActiveRecord::Schema.define do
migration("123001")
end
end
end
end
end end

@ -27,46 +27,22 @@ def puts(text="")
end end
class MigrationTableAndIndexTest < ActiveRecord::TestCase class MigrationTableAndIndexTest < ActiveRecord::TestCase
def setup def test_add_schema_info_respects_prefix_and_suffix
@conn = ActiveRecord::Base.connection conn = ActiveRecord::Base.connection
@conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if @conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
end
def test_add_schema_migrations_respects_prefix_and_suffix conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
# Use shorter prefix and suffix as in Oracle database identifier cannot be larger than 30 characters # Use shorter prefix and suffix as in Oracle database identifier cannot be larger than 30 characters
ActiveRecord::Base.table_name_prefix = 'p_' ActiveRecord::Base.table_name_prefix = 'p_'
ActiveRecord::Base.table_name_suffix = '_s' ActiveRecord::Base.table_name_suffix = '_s'
@conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if @conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name) conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
@conn.initialize_schema_migrations_table conn.initialize_schema_migrations_table
assert_equal "p_unique_schema_migrations_s", @conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name] assert_equal "p_unique_schema_migrations_s", conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name]
ensure ensure
ActiveRecord::Base.table_name_prefix = "" ActiveRecord::Base.table_name_prefix = ""
ActiveRecord::Base.table_name_suffix = "" ActiveRecord::Base.table_name_suffix = ""
end end
def test_schema_migrations_columns
@conn.initialize_schema_migrations_table
columns = @conn.columns(ActiveRecord::Migrator.schema_migrations_table_name).collect(&:name)
%w[version name migrated_at].each { |col| assert columns.include?(col) }
end
def test_add_name_and_migrated_at_to_exisiting_schema_migrations
sm_table = ActiveRecord::Migrator.schema_migrations_table_name
@conn.create_table(sm_table, :id => false) do |schema_migrations_table|
schema_migrations_table.column :version, :string, :null => false
end
@conn.insert "INSERT INTO #{@conn.quote_table_name(sm_table)} (version) VALUES (100)"
@conn.insert "INSERT INTO #{@conn.quote_table_name(sm_table)} (version) VALUES (200)"
@conn.initialize_schema_migrations_table
rows = @conn.select_all("SELECT * FROM #{@conn.quote_table_name(sm_table)}")
assert rows[0].has_key?("name")
assert rows[0].has_key?("migrated_at")
end
end end
class MigrationTest < ActiveRecord::TestCase class MigrationTest < ActiveRecord::TestCase
@ -1462,21 +1438,6 @@ def test_schema_migrations_table_name
ActiveRecord::Base.table_name_suffix = "" ActiveRecord::Base.table_name_suffix = ""
end end
def test_migration_row_includes_name_and_timestamp
conn = ActiveRecord::Base.connection
sm_table = ActiveRecord::Migrator.schema_migrations_table_name
ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid")
rows = conn.select_all("SELECT * FROM #{conn.quote_table_name(sm_table)}")
rows.each do |row|
assert_not_nil(row["migrated_at"], "missing migrated_at")
end
assert_equal "people_have_last_names", rows[0]["name"]
assert_equal "we_need_reminders", rows[1]["name"]
assert_equal "innocent_jointable", rows[2]["name"]
end
def test_proper_table_name def test_proper_table_name
assert_equal "table", ActiveRecord::Migrator.proper_table_name('table') assert_equal "table", ActiveRecord::Migrator.proper_table_name('table')
assert_equal "table", ActiveRecord::Migrator.proper_table_name(:table) assert_equal "table", ActiveRecord::Migrator.proper_table_name(:table)
@ -2132,3 +2093,4 @@ def test_copying_migrations_to_empty_directory
end end
end end
end end

@ -17,25 +17,6 @@ def test_schema_dump
assert_no_match %r{create_table "schema_migrations"}, output assert_no_match %r{create_table "schema_migrations"}, output
end end
def test_schema_dump_includes_migrations
$".delete_if do |fname|
fname == (MIGRATIONS_ROOT + "/valid/1_people_have_last_names.rb")
end
conn = ActiveRecord::Base.connection
sm_table = ActiveRecord::Migrator.schema_migrations_table_name
conn.execute "DELETE FROM #{conn.quote_table_name(sm_table)}"
conn.remove_column "people", "last_name" rescue nil
conn.drop_table "reminders" rescue nil
conn.drop_table "people_reminders" rescue nil
ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid")
output = standard_dump
assert_match %r{migration "1", "people_have_last_names"}, output
assert_match %r{migration "2", "we_need_reminders"}, output
assert_match %r{migration "3", "innocent_jointable"}, output
end
def test_schema_dump_excludes_sqlite_sequence def test_schema_dump_excludes_sqlite_sequence
output = standard_dump output = standard_dump
assert_no_match %r{create_table "sqlite_sequence"}, output assert_no_match %r{create_table "sqlite_sequence"}, output