Skip to content

Commit

Permalink
prefer method sensors over actual ddl changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Jan 16, 2012
1 parent 8739a42 commit 8037c51
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 69 deletions.
69 changes: 0 additions & 69 deletions activerecord/test/cases/migration_test.rb
Expand Up @@ -173,26 +173,6 @@ def test_add_table_with_decimals
assert_raise(ActiveRecord::StatementInvalid) { BigNumber.find(:first) } assert_raise(ActiveRecord::StatementInvalid) { BigNumber.find(:first) }
end end


def test_migrator
assert !Person.column_methods_hash.include?(:last_name)
assert !Reminder.table_exists?

ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid")

assert_equal 3, ActiveRecord::Migrator.current_version
Person.reset_column_information
assert Person.column_methods_hash.include?(:last_name)
assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
assert_equal "hello world", Reminder.find(:first).content

ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/valid")

assert_equal 0, ActiveRecord::Migrator.current_version
Person.reset_column_information
assert !Person.column_methods_hash.include?(:last_name)
assert_raise(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
end

def test_filtering_migrations def test_filtering_migrations
assert !Person.column_methods_hash.include?(:last_name) assert !Person.column_methods_hash.include?(:last_name)
assert !Reminder.table_exists? assert !Reminder.table_exists?
Expand Down Expand Up @@ -249,55 +229,6 @@ def test_instance_based_migration_down
assert migration.went_down, 'have not gone down' assert migration.went_down, 'have not gone down'
end end


def test_migrator_one_up
assert !Person.column_methods_hash.include?(:last_name)
assert !Reminder.table_exists?

ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid", 1)

Person.reset_column_information
assert Person.column_methods_hash.include?(:last_name)
assert !Reminder.table_exists?

ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid", 2)

assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
assert_equal "hello world", Reminder.find(:first).content
end

def test_migrator_one_down
ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid")

ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/valid", 1)

Person.reset_column_information
assert Person.column_methods_hash.include?(:last_name)
assert !Reminder.table_exists?
end

def test_migrator_one_up_one_down
ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid", 1)
ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/valid", 0)

assert !Person.column_methods_hash.include?(:last_name)
assert !Reminder.table_exists?
end

def test_migrator_double_up
assert_equal(0, ActiveRecord::Migrator.current_version)
ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT + "/valid", 1)
assert_nothing_raised { ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT + "/valid", 1) }
assert_equal(1, ActiveRecord::Migrator.current_version)
end

def test_migrator_double_down
assert_equal(0, ActiveRecord::Migrator.current_version)
ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT + "/valid", 1)
ActiveRecord::Migrator.run(:down, MIGRATIONS_ROOT + "/valid", 1)
assert_nothing_raised { ActiveRecord::Migrator.run(:down, MIGRATIONS_ROOT + "/valid", 1) }
assert_equal(0, ActiveRecord::Migrator.current_version)
end

def test_migrator_one_up_with_exception_and_rollback def test_migrator_one_up_with_exception_and_rollback
unless ActiveRecord::Base.connection.supports_ddl_transactions? unless ActiveRecord::Base.connection.supports_ddl_transactions?
skip "not supported on #{ActiveRecord::Base.connection.class}" skip "not supported on #{ActiveRecord::Base.connection.class}"
Expand Down
84 changes: 84 additions & 0 deletions activerecord/test/cases/migrator_test.rb
Expand Up @@ -154,5 +154,89 @@ def test_current_version
ActiveRecord::SchemaMigration.create!(:version => '1000') ActiveRecord::SchemaMigration.create!(:version => '1000')
assert_equal 1000, ActiveRecord::Migrator.current_version assert_equal 1000, ActiveRecord::Migrator.current_version
end end

def test_migrator_one_up
calls, migrations = sensors(3)

ActiveRecord::Migrator.new(:up, migrations, 1).migrate
assert_equal [[:up, 0], [:up, 1]], calls
calls.clear

ActiveRecord::Migrator.new(:up, migrations, 2).migrate
assert_equal [[:up, 2]], calls
end

def test_migrator_one_down
calls, migrations = sensors(3)

ActiveRecord::Migrator.new(:up, migrations).migrate
assert_equal [[:up, 0], [:up, 1], [:up, 2]], calls
calls.clear

ActiveRecord::Migrator.new(:down, migrations, 1).migrate

assert_equal [[:down, 2]], calls
end

def test_migrator_one_up_one_down
calls, migrations = sensors(3)

ActiveRecord::Migrator.new(:up, migrations, 1).migrate
assert_equal [[:up, 0], [:up, 1]], calls
calls.clear

ActiveRecord::Migrator.new(:down, migrations, 0).migrate
assert_equal [[:down, 1]], calls
end

def test_migrator_double_up
calls, migrations = sensors(3)
assert_equal(0, ActiveRecord::Migrator.current_version)

ActiveRecord::Migrator.new(:up, migrations, 1).migrate
assert_equal [[:up, 0], [:up, 1]], calls
calls.clear

ActiveRecord::Migrator.new(:up, migrations, 1).migrate
assert_equal [], calls
end

def test_migrator_double_down
calls, migrations = sensors(3)

assert_equal(0, ActiveRecord::Migrator.current_version)

ActiveRecord::Migrator.new(:up, migrations, 1).run
assert_equal [[:up, 1]], calls
calls.clear

ActiveRecord::Migrator.new(:down, migrations, 1).run
assert_equal [[:down, 1]], calls
calls.clear

ActiveRecord::Migrator.new(:down, migrations, 1).run
assert_equal [], calls

assert_equal(0, ActiveRecord::Migrator.current_version)
end

private
def m(name, version, &block)
x = Sensor.new name, version
x.extend(Module.new {
define_method(:up) { block.call(:up, x); super() }
define_method(:down) { block.call(:down, x); super() }
}) if block_given?
end

def sensors(count)
calls = []
migrations = 3.times.map { |i|

This comment has been minimized.

Copy link
@exviva

exviva Jan 16, 2012

Contributor

Didn't you mean count.times?

This comment has been minimized.

Copy link
@tenderlove

tenderlove Jan 16, 2012

Author Member

No:

irb(main):001:0> 3.times { |i| i }
=> 3
irb(main):002:0> 3.times.map { |i| i }
=> [0, 1, 2]
irb(main):003:0>

This comment has been minimized.

Copy link
@exviva

exviva Jan 16, 2012

Contributor

I meant replacing the hardcoded 3 with count :).

This comment has been minimized.

Copy link
@tenderlove

tenderlove Jan 16, 2012

Author Member

doh! You're right! 💣

This comment has been minimized.

Copy link
@hammerdr

hammerdr Jan 16, 2012

Contributor

Question about count.times.map (and just a question.. not necessarily something worth changing):

Would using (0...count).map be a more straightforward way to convey meaning in this case? As far as I know, count.times.to_a returning (0...count).to_a is something of an unintended side effect of the times function.

This comment has been minimized.

Copy link
@tenderlove

tenderlove Jan 16, 2012

Author Member

I don't think it's an unintended side effect. The times method returns an enumerator. Enumerators are enumerable, so calling map or to_a, or any method provided by enumerable is a valid use.

This comment has been minimized.

Copy link
@tenderlove

tenderlove Jan 16, 2012

Author Member

Oops, forgot to add the relevant documentation:

=== Implementation from Integer
------------------------------------------------------------------------------
  int.times {|i| block }  ->  self
  int.times               ->  an_enumerator


------------------------------------------------------------------------------
m(nil, i) { |c,migration|
calls << [c, migration.version]
}
}
[calls, migrations]
end
end end
end end

0 comments on commit 8037c51

Please sign in to comment.