|
| 1 | +require 'jdbc_common' |
| 2 | +require 'db/mysql' |
| 3 | + |
| 4 | +class MysqlChangeColumnTest < Test::Unit::TestCase |
| 5 | + |
| 6 | + class Person < ActiveRecord::Base; end |
| 7 | + |
| 8 | + class CreatePeopleTable < ActiveRecord::Migration |
| 9 | + def self.up |
| 10 | + create_table :people do |t| |
| 11 | + t.integer :phone |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + def self.down |
| 16 | + drop_table :people |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + def setup |
| 21 | + CreatePeopleTable.up |
| 22 | + end |
| 23 | + |
| 24 | + def teardown |
| 25 | + CreatePeopleTable.down |
| 26 | + end |
| 27 | + |
| 28 | + def test_should_change_column_type |
| 29 | + ActiveRecord::Migration.change_column :people, :phone, :string |
| 30 | + Person.reset_column_information |
| 31 | + |
| 32 | + p = Person.create!(:phone => 'ABC') |
| 33 | + assert_equal 'ABC', p.phone |
| 34 | + end |
| 35 | + |
| 36 | + def test_sets_defaults_on_column |
| 37 | + ActiveRecord::Migration.change_column :people, :phone, :string, :default => '123456' |
| 38 | + Person.reset_column_information |
| 39 | + |
| 40 | + p = Person.create! |
| 41 | + assert_equal '123456', p.phone |
| 42 | + end |
| 43 | + |
| 44 | + def test_should_change_column_default_value |
| 45 | + ActiveRecord:: Migration.add_column :people, :email, :string, :default => '[email protected]' |
| 46 | + ActiveRecord:: Migration.change_column :people, :email, :string, :default => '[email protected]' |
| 47 | + Person.reset_column_information |
| 48 | + |
| 49 | + p = Person.create! |
| 50 | + assert_equal '[email protected]', p.email |
| 51 | + end |
| 52 | + |
| 53 | + def test_should_set_non_null_restriction |
| 54 | + ActiveRecord::Migration.change_column :people, :phone, :string, :null => false |
| 55 | + Person.reset_column_information |
| 56 | + assert_raise(ActiveRecord::StatementInvalid) { Person.create! } |
| 57 | + end |
| 58 | + |
| 59 | + def test_should_set_null_restriction_with_default |
| 60 | + p = Person.create! |
| 61 | + ActiveRecord::Migration.change_column :people, :phone, :string, :null => true, :default => '123456' |
| 62 | + Person.reset_column_information |
| 63 | + |
| 64 | + assert_nil p.reload.phone |
| 65 | + assert_equal '123456', Person.create!.phone |
| 66 | + end |
| 67 | +end |
| 68 | + |
0 commit comments