aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/migration/rename_column_test.rb
blob: 16e09fd80e1c828c5140cb5f122da98a491214cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require "cases/migration/helper"

module ActiveRecord
  class Migration
    class RenameColumnTest < ActiveRecord::TestCase
      include ActiveRecord::Migration::TestHelper

      self.use_transactional_fixtures = false

      # FIXME: this is more of an integration test with AR::Base and the
      # schema modifications.  Maybe we should move this?
      def test_add_rename
        add_column "test_models", "girlfriend", :string
        TestModel.reset_column_information

        TestModel.create :girlfriend => 'bobette'

        rename_column "test_models", "girlfriend", "exgirlfriend"

        TestModel.reset_column_information
        bob = TestModel.find(:first)

        assert_equal "bobette", bob.exgirlfriend
      end

      # FIXME: another integration test.  We should decouple this from the
      # AR::Base implementation.
      def test_rename_column_using_symbol_arguments
        add_column :test_models, :first_name, :string

        TestModel.create :first_name => 'foo'

        rename_column :test_models, :first_name, :nick_name
        TestModel.reset_column_information
        assert TestModel.column_names.include?("nick_name")
        assert_equal ['foo'], TestModel.find(:all).map(&:nick_name)
      end

      # FIXME: another integration test.  We should decouple this from the
      # AR::Base implementation.
      def test_rename_column
        add_column "test_models", "first_name", "string"

        TestModel.create :first_name => 'foo'

        rename_column "test_models", "first_name", "nick_name"
        TestModel.reset_column_information
        assert TestModel.column_names.include?("nick_name")
        assert_equal ['foo'], TestModel.find(:all).map(&:nick_name)
      end

      def test_rename_column_preserves_default_value_not_null
        add_column 'test_models', 'salary', :integer, :default => 70000

        default_before = connection.columns("test_models").find { |c| c.name == "salary" }.default
        assert_equal 70000, default_before

        rename_column "test_models", "salary", "anual_salary"

        assert TestModel.column_names.include?("anual_salary")
        default_after = connection.columns("test_models").find { |c| c.name == "anual_salary" }.default
        assert_equal 70000, default_after
      end

      def test_rename_nonexistent_column
        exception = if current_adapter?(:PostgreSQLAdapter, :OracleAdapter)
                      ActiveRecord::StatementInvalid
                    else
                      ActiveRecord::ActiveRecordError
                    end
        assert_raise(exception) do
          rename_column "test_models", "nonexistent", "should_fail"
        end
      end

      def test_rename_column_with_sql_reserved_word
        add_column 'test_models', 'first_name', :string
        rename_column "test_models", "first_name", "group"

        assert TestModel.column_names.include?("group")
      end

      def test_rename_column_with_an_index
        add_column "test_models", :hat_name, :string
        add_index :test_models, :hat_name

        # FIXME: we should test that the index goes away
        rename_column "test_models", "hat_name", "name"
      end

      def test_remove_column_with_index
        add_column "test_models", :hat_name, :string
        add_index :test_models, :hat_name

        # FIXME: we should test that the index goes away
        remove_column("test_models", "hat_name")
      end

      def test_remove_column_with_multi_column_index
        add_column "test_models", :hat_size, :integer
        add_column "test_models", :hat_style, :string, :limit => 100
        add_index "test_models", ["hat_style", "hat_size"], :unique => true

        # FIXME: we should test that the index goes away
        remove_column("test_models", "hat_size")
      end

      # FIXME: we need to test that these calls do something
      def test_change_type_of_not_null_column
        change_column "test_models", "updated_at", :datetime, :null => false
        change_column "test_models", "updated_at", :datetime, :null => false
        change_column "test_models", "updated_at", :datetime, :null => true
      end

      def test_change_column_nullability
        add_column "test_models", "funny", :boolean
        assert TestModel.columns_hash["funny"].null, "Column 'funny' must initially allow nulls"

        change_column "test_models", "funny", :boolean, :null => false, :default => true

        TestModel.reset_column_information
        refute TestModel.columns_hash["funny"].null, "Column 'funny' must *not* allow nulls at this point"

        change_column "test_models", "funny", :boolean, :null => true
        TestModel.reset_column_information
        assert TestModel.columns_hash["funny"].null, "Column 'funny' must allow nulls again at this point"
      end

      def test_change_column
        add_column 'test_models', 'age', :integer
        add_column 'test_models', 'approved', :boolean, :default => true

        old_columns = connection.columns(TestModel.table_name)

        assert old_columns.find { |c| c.name == 'age' && c.type == :integer }

        change_column "test_models", "age", :string

        new_columns = connection.columns(TestModel.table_name)

        refute new_columns.find { |c| c.name == 'age' and c.type == :integer }
        assert new_columns.find { |c| c.name == 'age' and c.type == :string }

        old_columns = connection.columns(TestModel.table_name)
        assert old_columns.find { |c|
          c.name == 'approved' && c.type == :boolean && c.default == true
        }

        change_column :test_models, :approved, :boolean, :default => false
        new_columns = connection.columns(TestModel.table_name)

        refute new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
        assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false }
        change_column :test_models, :approved, :boolean, :default => true
      end

      def test_change_column_with_nil_default
        add_column "test_models", "contributor", :boolean, :default => true
        assert TestModel.new.contributor?

        change_column "test_models", "contributor", :boolean, :default => nil
        TestModel.reset_column_information
        refute TestModel.new.contributor?
        assert_nil TestModel.new.contributor
      end

      def test_change_column_with_new_default
        add_column "test_models", "administrator", :boolean, :default => true
        assert TestModel.new.administrator?

        change_column "test_models", "administrator", :boolean, :default => false
        TestModel.reset_column_information
        refute TestModel.new.administrator?
      end

      def test_change_column_default
        add_column "test_models", "first_name", :string
        connection.change_column_default "test_models", "first_name", "Tester"

        assert_equal "Tester", TestModel.new.first_name
      end

      def test_change_column_default_to_null
        add_column "test_models", "first_name", :string
        connection.change_column_default "test_models", "first_name", nil
        assert_nil TestModel.new.first_name
      end

      def test_remove_column_no_second_parameter_raises_exception
        assert_raise(ArgumentError) { connection.remove_column("funny") }
      end
    end
  end
end