提交 a6d4e5e5 编写于 作者: M Matthew Draper

Internal test migrations use the private 'Current' version

Apart from specific versioning support, our tests should focus on the
behaviour of whatever version they're accompanying, regardless of when
they were written.

Application code should *not* do this.
上级 dc8d40af
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
class PostgresqlExtensionMigrationTest < ActiveRecord::PostgreSQLTestCase class PostgresqlExtensionMigrationTest < ActiveRecord::PostgreSQLTestCase
self.use_transactional_tests = false self.use_transactional_tests = false
class EnableHstore < ActiveRecord::Migration class EnableHstore < ActiveRecord::Migration::Current
def change def change
enable_extension "hstore" enable_extension "hstore"
end end
end end
class DisableHstore < ActiveRecord::Migration class DisableHstore < ActiveRecord::Migration::Current
def change def change
disable_extension "hstore" disable_extension "hstore"
end end
......
...@@ -86,7 +86,7 @@ def test_change_table_supports_hstore ...@@ -86,7 +86,7 @@ def test_change_table_supports_hstore
end end
def test_hstore_migration def test_hstore_migration
hstore_migration = Class.new(ActiveRecord::Migration) do hstore_migration = Class.new(ActiveRecord::Migration::Current) do
def change def change
change_table("hstores") do |t| change_table("hstores") do |t|
t.hstore :keys t.hstore :keys
......
...@@ -5,7 +5,7 @@ class Horse < ActiveRecord::Base ...@@ -5,7 +5,7 @@ class Horse < ActiveRecord::Base
module ActiveRecord module ActiveRecord
class InvertibleMigrationTest < ActiveRecord::TestCase class InvertibleMigrationTest < ActiveRecord::TestCase
class SilentMigration < ActiveRecord::Migration class SilentMigration < ActiveRecord::Migration::Current
def write(text = '') def write(text = '')
# sssshhhhh!! # sssshhhhh!!
end end
...@@ -105,7 +105,7 @@ def change ...@@ -105,7 +105,7 @@ def change
end end
end end
class LegacyMigration < ActiveRecord::Migration class LegacyMigration < ActiveRecord::Migration::Current
def self.up def self.up
create_table("horses") do |t| create_table("horses") do |t|
t.column :content, :text t.column :content, :text
......
...@@ -339,7 +339,7 @@ def test_keeping_default_and_notnull_constraints_on_change ...@@ -339,7 +339,7 @@ def test_keeping_default_and_notnull_constraints_on_change
def test_change_column_null def test_change_column_null
testing_table_with_only_foo_attribute do testing_table_with_only_foo_attribute do
notnull_migration = Class.new(ActiveRecord::Migration) do notnull_migration = Class.new(ActiveRecord::Migration::Current) do
def change def change
change_column_null :testings, :foo, false change_column_null :testings, :foo, false
end end
......
...@@ -224,7 +224,7 @@ def test_schema_dumping_on_delete_and_on_update_options ...@@ -224,7 +224,7 @@ def test_schema_dumping_on_delete_and_on_update_options
assert_match %r{\s+add_foreign_key "astronauts",.+on_update: :cascade,.+on_delete: :nullify$}, output assert_match %r{\s+add_foreign_key "astronauts",.+on_update: :cascade,.+on_delete: :nullify$}, output
end end
class CreateCitiesAndHousesMigration < ActiveRecord::Migration class CreateCitiesAndHousesMigration < ActiveRecord::Migration::Current
def change def change
create_table("cities") { |t| } create_table("cities") { |t| }
...@@ -243,7 +243,7 @@ def test_add_foreign_key_is_reversible ...@@ -243,7 +243,7 @@ def test_add_foreign_key_is_reversible
silence_stream($stdout) { migration.migrate(:down) } silence_stream($stdout) { migration.migrate(:down) }
end end
class CreateSchoolsAndClassesMigration < ActiveRecord::Migration class CreateSchoolsAndClassesMigration < ActiveRecord::Migration::Current
def change def change
create_table(:schools) create_table(:schools)
......
...@@ -132,12 +132,12 @@ def test_create_table_with_force_true_does_not_drop_nonexisting_table ...@@ -132,12 +132,12 @@ def test_create_table_with_force_true_does_not_drop_nonexisting_table
end end
def test_migration_instance_has_connection def test_migration_instance_has_connection
migration = Class.new(ActiveRecord::Migration).new migration = Class.new(ActiveRecord::Migration::Current).new
assert_equal ActiveRecord::Base.connection, migration.connection assert_equal ActiveRecord::Base.connection, migration.connection
end end
def test_method_missing_delegates_to_connection def test_method_missing_delegates_to_connection
migration = Class.new(ActiveRecord::Migration) { migration = Class.new(ActiveRecord::Migration::Current) {
def connection def connection
Class.new { Class.new {
def create_table; "hi mom!"; end def create_table; "hi mom!"; end
...@@ -226,7 +226,7 @@ def test_filtering_migrations ...@@ -226,7 +226,7 @@ def test_filtering_migrations
assert_raise(ActiveRecord::StatementInvalid) { Reminder.first } assert_raise(ActiveRecord::StatementInvalid) { Reminder.first }
end end
class MockMigration < ActiveRecord::Migration class MockMigration < ActiveRecord::Migration::Current
attr_reader :went_up, :went_down attr_reader :went_up, :went_down
def initialize def initialize
@went_up = false @went_up = false
...@@ -268,7 +268,7 @@ def test_instance_based_migration_down ...@@ -268,7 +268,7 @@ def test_instance_based_migration_down
def test_migrator_one_up_with_exception_and_rollback def test_migrator_one_up_with_exception_and_rollback
assert_no_column Person, :last_name assert_no_column Person, :last_name
migration = Class.new(ActiveRecord::Migration) { migration = Class.new(ActiveRecord::Migration::Current) {
def version; 100 end def version; 100 end
def migrate(x) def migrate(x)
add_column "people", "last_name", :string add_column "people", "last_name", :string
...@@ -289,7 +289,7 @@ def migrate(x) ...@@ -289,7 +289,7 @@ def migrate(x)
def test_migrator_one_up_with_exception_and_rollback_using_run def test_migrator_one_up_with_exception_and_rollback_using_run
assert_no_column Person, :last_name assert_no_column Person, :last_name
migration = Class.new(ActiveRecord::Migration) { migration = Class.new(ActiveRecord::Migration::Current) {
def version; 100 end def version; 100 end
def migrate(x) def migrate(x)
add_column "people", "last_name", :string add_column "people", "last_name", :string
...@@ -310,7 +310,7 @@ def migrate(x) ...@@ -310,7 +310,7 @@ def migrate(x)
def test_migration_without_transaction def test_migration_without_transaction
assert_no_column Person, :last_name assert_no_column Person, :last_name
migration = Class.new(ActiveRecord::Migration) { migration = Class.new(ActiveRecord::Migration::Current) {
self.disable_ddl_transaction! self.disable_ddl_transaction!
def version; 101 end def version; 101 end
...@@ -525,7 +525,7 @@ def test_out_of_range_limit_should_raise ...@@ -525,7 +525,7 @@ def test_out_of_range_limit_should_raise
if ActiveRecord::Base.connection.supports_advisory_locks? if ActiveRecord::Base.connection.supports_advisory_locks?
def test_migrator_generates_valid_lock_id def test_migrator_generates_valid_lock_id
migration = Class.new(ActiveRecord::Migration).new migration = Class.new(ActiveRecord::Migration::Current).new
migrator = ActiveRecord::Migrator.new(:up, [migration], 100) migrator = ActiveRecord::Migrator.new(:up, [migration], 100)
lock_id = migrator.send(:generate_migrator_advisory_lock_id) lock_id = migrator.send(:generate_migrator_advisory_lock_id)
...@@ -539,7 +539,7 @@ def test_migrator_generates_valid_lock_id ...@@ -539,7 +539,7 @@ def test_migrator_generates_valid_lock_id
def test_generate_migrator_advisory_lock_id def test_generate_migrator_advisory_lock_id
# It is important we are consistent with how we generate this so that # It is important we are consistent with how we generate this so that
# exclusive locking works across migrator versions # exclusive locking works across migrator versions
migration = Class.new(ActiveRecord::Migration).new migration = Class.new(ActiveRecord::Migration::Current).new
migrator = ActiveRecord::Migrator.new(:up, [migration], 100) migrator = ActiveRecord::Migrator.new(:up, [migration], 100)
lock_id = migrator.send(:generate_migrator_advisory_lock_id) lock_id = migrator.send(:generate_migrator_advisory_lock_id)
...@@ -556,7 +556,7 @@ def test_generate_migrator_advisory_lock_id ...@@ -556,7 +556,7 @@ def test_generate_migrator_advisory_lock_id
def test_migrator_one_up_with_unavailable_lock def test_migrator_one_up_with_unavailable_lock
assert_no_column Person, :last_name assert_no_column Person, :last_name
migration = Class.new(ActiveRecord::Migration) { migration = Class.new(ActiveRecord::Migration::Current) {
def version; 100 end def version; 100 end
def migrate(x) def migrate(x)
add_column "people", "last_name", :string add_column "people", "last_name", :string
...@@ -577,7 +577,7 @@ def migrate(x) ...@@ -577,7 +577,7 @@ def migrate(x)
def test_migrator_one_up_with_unavailable_lock_using_run def test_migrator_one_up_with_unavailable_lock_using_run
assert_no_column Person, :last_name assert_no_column Person, :last_name
migration = Class.new(ActiveRecord::Migration) { migration = Class.new(ActiveRecord::Migration::Current) {
def version; 100 end def version; 100 end
def migrate(x) def migrate(x)
add_column "people", "last_name", :string add_column "people", "last_name", :string
......
...@@ -6,7 +6,7 @@ class MigratorTest < ActiveRecord::TestCase ...@@ -6,7 +6,7 @@ class MigratorTest < ActiveRecord::TestCase
# Use this class to sense if migrations have gone # Use this class to sense if migrations have gone
# up or down. # up or down.
class Sensor < ActiveRecord::Migration class Sensor < ActiveRecord::Migration::Current
attr_reader :went_up, :went_down attr_reader :went_up, :went_down
def initialize name = self.class.name, version = nil def initialize name = self.class.name, version = nil
......
...@@ -312,7 +312,7 @@ def test_do_not_dump_foreign_keys_for_ignored_tables ...@@ -312,7 +312,7 @@ def test_do_not_dump_foreign_keys_for_ignored_tables
end end
end end
class CreateDogMigration < ActiveRecord::Migration class CreateDogMigration < ActiveRecord::Migration::Current
def up def up
create_table("dog_owners") do |t| create_table("dog_owners") do |t|
end end
...@@ -357,7 +357,7 @@ def test_schema_dump_with_table_name_prefix_and_suffix ...@@ -357,7 +357,7 @@ def test_schema_dump_with_table_name_prefix_and_suffix
def test_schema_dump_with_table_name_prefix_and_ignoring_tables def test_schema_dump_with_table_name_prefix_and_ignoring_tables
original, $stdout = $stdout, StringIO.new original, $stdout = $stdout, StringIO.new
create_cat_migration = Class.new(ActiveRecord::Migration) do create_cat_migration = Class.new(ActiveRecord::Migration::Current) do
def change def change
create_table("cats") do |t| create_table("cats") do |t|
end end
......
class AddExpressions < ActiveRecord::Migration class AddExpressions < ActiveRecord::Migration::Current
def self.up def self.up
create_table("expressions") do |t| create_table("expressions") do |t|
t.column :expression, :string t.column :expression, :string
......
class GiveMeBigNumbers < ActiveRecord::Migration class GiveMeBigNumbers < ActiveRecord::Migration::Current
def self.up def self.up
create_table :big_numbers do |table| create_table :big_numbers do |table|
table.column :bank_balance, :decimal, :precision => 10, :scale => 2 table.column :bank_balance, :decimal, :precision => 10, :scale => 2
......
# coding: ISO-8859-15 # coding: ISO-8859-15
class CurrenciesHaveSymbols < ActiveRecord::Migration class CurrenciesHaveSymbols < ActiveRecord::Migration::Current
def self.up def self.up
# We use for default currency symbol # We use for default currency symbol
add_column "currencies", "symbol", :string, :default => "" add_column "currencies", "symbol", :string, :default => ""
......
class PeopleHaveMiddleNames < ActiveRecord::Migration class PeopleHaveMiddleNames < ActiveRecord::Migration::Current
def self.up def self.up
add_column "people", "middle_name", :string add_column "people", "middle_name", :string
end end
......
class PeopleHaveLastNames < ActiveRecord::Migration class PeopleHaveLastNames < ActiveRecord::Migration::Current
def self.up def self.up
add_column "people", "last_name", :string add_column "people", "last_name", :string
end end
......
class WeNeedReminders < ActiveRecord::Migration class WeNeedReminders < ActiveRecord::Migration::Current
def self.up def self.up
create_table("reminders") do |t| create_table("reminders") do |t|
t.column :content, :text t.column :content, :text
......
class InnocentJointable < ActiveRecord::Migration class InnocentJointable < ActiveRecord::Migration::Current
def self.up def self.up
create_table("people_reminders", :id => false) do |t| create_table("people_reminders", :id => false) do |t|
t.column :reminder_id, :integer t.column :reminder_id, :integer
......
class WeNeedThings < ActiveRecord::Migration class WeNeedThings < ActiveRecord::Migration::Current
def self.up def self.up
create_table("things") do |t| create_table("things") do |t|
t.column :content, :text t.column :content, :text
......
class RenameThings < ActiveRecord::Migration class RenameThings < ActiveRecord::Migration::Current
def self.up def self.up
rename_table "things", "awesome_things" rename_table "things", "awesome_things"
end end
......
class PeopleHaveLastNames < ActiveRecord::Migration class PeopleHaveLastNames < ActiveRecord::Migration::Current
def self.up def self.up
add_column "people", "hobbies", :text add_column "people", "hobbies", :text
end end
......
class PeopleHaveLastNames < ActiveRecord::Migration class PeopleHaveLastNames < ActiveRecord::Migration::Current
def self.up def self.up
add_column "people", "description", :text add_column "people", "description", :text
end end
......
class CreateArticles < ActiveRecord::Migration class CreateArticles < ActiveRecord::Migration::Current
def self.up def self.up
end end
......
class CreateArticles < ActiveRecord::Migration class CreateArticles < ActiveRecord::Migration::Current
def self.up def self.up
end end
......
class PeopleHaveLastNames < ActiveRecord::Migration class PeopleHaveLastNames < ActiveRecord::Migration::Current
def self.up def self.up
add_column "people", "hobbies", :string add_column "people", "hobbies", :string
end end
......
class PeopleHaveLastNames < ActiveRecord::Migration class PeopleHaveLastNames < ActiveRecord::Migration::Current
def self.up def self.up
add_column "people", "hobbies", :text add_column "people", "hobbies", :text
end end
......
class PeopleHaveLastNames < ActiveRecord::Migration class PeopleHaveLastNames < ActiveRecord::Migration::Current
def self.up def self.up
add_column "people", "description", :text add_column "people", "description", :text
end end
......
class CreateArticles < ActiveRecord::Migration class CreateArticles < ActiveRecord::Migration::Current
def self.up def self.up
end end
......
class CreateComments < ActiveRecord::Migration class CreateComments < ActiveRecord::Migration::Current
def self.up def self.up
end end
......
class ValidPeopleHaveLastNames < ActiveRecord::Migration class ValidPeopleHaveLastNames < ActiveRecord::Migration::Current
def self.up def self.up
add_column "people", "last_name", :string add_column "people", "last_name", :string
end end
......
class WeNeedReminders < ActiveRecord::Migration class WeNeedReminders < ActiveRecord::Migration::Current
def self.up def self.up
create_table("reminders") do |t| create_table("reminders") do |t|
t.column :content, :text t.column :content, :text
......
class InnocentJointable < ActiveRecord::Migration class InnocentJointable < ActiveRecord::Migration::Current
def self.up def self.up
create_table("people_reminders", :id => false) do |t| create_table("people_reminders", :id => false) do |t|
t.column :reminder_id, :integer t.column :reminder_id, :integer
......
class ValidPeopleHaveLastNames < ActiveRecord::Migration class ValidPeopleHaveLastNames < ActiveRecord::Migration::Current
def self.up def self.up
add_column "people", "last_name", :string add_column "people", "last_name", :string
end end
......
class WeNeedReminders < ActiveRecord::Migration class WeNeedReminders < ActiveRecord::Migration::Current
def self.up def self.up
create_table("reminders") do |t| create_table("reminders") do |t|
t.column :content, :text t.column :content, :text
......
class InnocentJointable < ActiveRecord::Migration class InnocentJointable < ActiveRecord::Migration::Current
def self.up def self.up
create_table("people_reminders", :id => false) do |t| create_table("people_reminders", :id => false) do |t|
t.column :reminder_id, :integer t.column :reminder_id, :integer
......
class ValidWithTimestampsPeopleHaveLastNames < ActiveRecord::Migration class ValidWithTimestampsPeopleHaveLastNames < ActiveRecord::Migration::Current
def self.up def self.up
add_column "people", "last_name", :string add_column "people", "last_name", :string
end end
......
class ValidWithTimestampsWeNeedReminders < ActiveRecord::Migration class ValidWithTimestampsWeNeedReminders < ActiveRecord::Migration::Current
def self.up def self.up
create_table("reminders") do |t| create_table("reminders") do |t|
t.column :content, :text t.column :content, :text
......
class ValidWithTimestampsInnocentJointable < ActiveRecord::Migration class ValidWithTimestampsInnocentJointable < ActiveRecord::Migration::Current
def self.up def self.up
create_table("people_reminders", :id => false) do |t| create_table("people_reminders", :id => false) do |t|
t.column :reminder_id, :integer t.column :reminder_id, :integer
......
class MigrationVersionCheck < ActiveRecord::Migration class MigrationVersionCheck < ActiveRecord::Migration::Current
def self.up def self.up
raise "incorrect migration version" unless version == 20131219224947 raise "incorrect migration version" unless version == 20131219224947
end end
......
...@@ -103,7 +103,7 @@ class MyLogger < ::Logger ...@@ -103,7 +103,7 @@ class MyLogger < ::Logger
RUBY RUBY
app_file 'db/migrate/20140708012246_create_user.rb', <<-RUBY app_file 'db/migrate/20140708012246_create_user.rb', <<-RUBY
class CreateUser < ActiveRecord::Migration class CreateUser < ActiveRecord::Migration::Current
def change def change
create_table :users create_table :users
end end
......
...@@ -290,7 +290,7 @@ class Post < ActiveRecord::Base ...@@ -290,7 +290,7 @@ class Post < ActiveRecord::Base
extend Rack::Test::Methods extend Rack::Test::Methods
app_file "db/migrate/1_create_posts.rb", <<-MIGRATION app_file "db/migrate/1_create_posts.rb", <<-MIGRATION
class CreatePosts < ActiveRecord::Migration class CreatePosts < ActiveRecord::Migration::Current
def change def change
create_table :posts do |t| create_table :posts do |t|
t.string :title, default: "TITLE" t.string :title, default: "TITLE"
...@@ -306,7 +306,7 @@ def change ...@@ -306,7 +306,7 @@ def change
assert_equal "TITLE", last_response.body assert_equal "TITLE", last_response.body
app_file "db/migrate/2_add_body_to_posts.rb", <<-MIGRATION app_file "db/migrate/2_add_body_to_posts.rb", <<-MIGRATION
class AddBodyToPosts < ActiveRecord::Migration class AddBodyToPosts < ActiveRecord::Migration::Current
def change def change
add_column :posts, :body, :text, default: "BODY" add_column :posts, :body, :text, default: "BODY"
end end
......
...@@ -18,7 +18,7 @@ def teardown ...@@ -18,7 +18,7 @@ def teardown
`bin/rails generate model user username:string password:string` `bin/rails generate model user username:string password:string`
app_file "db/migrate/01_a_migration.bukkits.rb", <<-MIGRATION app_file "db/migrate/01_a_migration.bukkits.rb", <<-MIGRATION
class AMigration < ActiveRecord::Migration class AMigration < ActiveRecord::Migration::Current
end end
MIGRATION MIGRATION
...@@ -158,12 +158,12 @@ class AMigration < ActiveRecord::Migration ...@@ -158,12 +158,12 @@ class AMigration < ActiveRecord::Migration
Dir.chdir(app_path) do Dir.chdir(app_path) do
app_file "db/migrate/1_one_migration.rb", <<-MIGRATION app_file "db/migrate/1_one_migration.rb", <<-MIGRATION
class OneMigration < ActiveRecord::Migration class OneMigration < ActiveRecord::Migration::Current
end end
MIGRATION MIGRATION
app_file "db/migrate/02_two_migration.rb", <<-MIGRATION app_file "db/migrate/02_two_migration.rb", <<-MIGRATION
class TwoMigration < ActiveRecord::Migration class TwoMigration < ActiveRecord::Migration::Current
end end
MIGRATION MIGRATION
......
...@@ -63,22 +63,22 @@ def boot_rails ...@@ -63,22 +63,22 @@ def boot_rails
test "copying migrations" do test "copying migrations" do
@plugin.write "db/migrate/1_create_users.rb", <<-RUBY @plugin.write "db/migrate/1_create_users.rb", <<-RUBY
class CreateUsers < ActiveRecord::Migration class CreateUsers < ActiveRecord::Migration::Current
end end
RUBY RUBY
@plugin.write "db/migrate/2_add_last_name_to_users.rb", <<-RUBY @plugin.write "db/migrate/2_add_last_name_to_users.rb", <<-RUBY
class AddLastNameToUsers < ActiveRecord::Migration class AddLastNameToUsers < ActiveRecord::Migration::Current
end end
RUBY RUBY
@plugin.write "db/migrate/3_create_sessions.rb", <<-RUBY @plugin.write "db/migrate/3_create_sessions.rb", <<-RUBY
class CreateSessions < ActiveRecord::Migration class CreateSessions < ActiveRecord::Migration::Current
end end
RUBY RUBY
app_file "db/migrate/1_create_sessions.rb", <<-RUBY app_file "db/migrate/1_create_sessions.rb", <<-RUBY
class CreateSessions < ActiveRecord::Migration class CreateSessions < ActiveRecord::Migration::Current
def up def up
end end
end end
...@@ -123,12 +123,12 @@ class Engine < ::Rails::Engine ...@@ -123,12 +123,12 @@ class Engine < ::Rails::Engine
end end
@plugin.write "db/migrate/1_create_users.rb", <<-RUBY @plugin.write "db/migrate/1_create_users.rb", <<-RUBY
class CreateUsers < ActiveRecord::Migration class CreateUsers < ActiveRecord::Migration::Current
end end
RUBY RUBY
@blog.write "db/migrate/2_create_blogs.rb", <<-RUBY @blog.write "db/migrate/2_create_blogs.rb", <<-RUBY
class CreateBlogs < ActiveRecord::Migration class CreateBlogs < ActiveRecord::Migration::Current
end end
RUBY RUBY
...@@ -163,11 +163,11 @@ class Engine < ::Rails::Engine; end ...@@ -163,11 +163,11 @@ class Engine < ::Rails::Engine; end
end end
@core.write "db/migrate/1_create_users.rb", <<-RUBY @core.write "db/migrate/1_create_users.rb", <<-RUBY
class CreateUsers < ActiveRecord::Migration; end class CreateUsers < ActiveRecord::Migration::Current; end
RUBY RUBY
@api.write "db/migrate/2_create_keys.rb", <<-RUBY @api.write "db/migrate/2_create_keys.rb", <<-RUBY
class CreateKeys < ActiveRecord::Migration; end class CreateKeys < ActiveRecord::Migration::Current; end
RUBY RUBY
boot_rails boot_rails
...@@ -190,7 +190,7 @@ class Engine < ::Rails::Engine ...@@ -190,7 +190,7 @@ class Engine < ::Rails::Engine
RUBY RUBY
@plugin.write "db/migrate/0_add_first_name_to_users.rb", <<-RUBY @plugin.write "db/migrate/0_add_first_name_to_users.rb", <<-RUBY
class AddFirstNameToUsers < ActiveRecord::Migration class AddFirstNameToUsers < ActiveRecord::Migration::Current
end end
RUBY RUBY
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册