aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/migration/references_foreign_key_test.rb
blob: 405fd7aaed74d336ab1daf86357f4396685793e0 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# frozen_string_literal: true
require "cases/helper"

if ActiveRecord::Base.connection.supports_foreign_keys_in_create?
  module ActiveRecord
    class Migration
      class ReferencesForeignKeyInCreateTest < ActiveRecord::TestCase
        setup do
          @connection = ActiveRecord::Base.connection
          @connection.create_table(:testing_parents, force: true)
        end

        teardown do
          @connection.drop_table "testings", if_exists: true
          @connection.drop_table "testing_parents", if_exists: true
        end

        test "foreign keys can be created with the table" do
          @connection.create_table :testings do |t|
            t.references :testing_parent, foreign_key: true
          end

          fk = @connection.foreign_keys("testings").first
          assert_equal "testings", fk.from_table
          assert_equal "testing_parents", fk.to_table
        end

        test "no foreign key is created by default" do
          @connection.create_table :testings do |t|
            t.references :testing_parent
          end

          assert_equal [], @connection.foreign_keys("testings")
        end

        test "foreign keys can be created in one query when index is not added" do
          assert_queries(1) do
            @connection.create_table :testings do |t|
              t.references :testing_parent, foreign_key: true, index: false
            end
          end
        end

        test "options hash can be passed" do
          @connection.change_table :testing_parents do |t|
            t.references :other, index: { unique: true }
          end
          @connection.create_table :testings do |t|
            t.references :testing_parent, foreign_key: { primary_key: :other_id }
          end

          fk = @connection.foreign_keys("testings").find { |k| k.to_table == "testing_parents" }
          assert_equal "other_id", fk.primary_key
        end

        test "to_table option can be passed" do
          @connection.create_table :testings do |t|
            t.references :parent, foreign_key: { to_table: :testing_parents }
          end
          fks = @connection.foreign_keys("testings")
          assert_equal([["testings", "testing_parents", "parent_id"]],
                       fks.map { |fk| [fk.from_table, fk.to_table, fk.column] })
        end
      end
    end
  end
end

if ActiveRecord::Base.connection.supports_foreign_keys?
  module ActiveRecord
    class Migration
      class ReferencesForeignKeyTest < ActiveRecord::TestCase
        setup do
          @connection = ActiveRecord::Base.connection
          @connection.create_table(:testing_parents, force: true)
        end

        teardown do
          @connection.drop_table "testings", if_exists: true
          @connection.drop_table "testing_parents", if_exists: true
        end

        test "foreign keys cannot be added to polymorphic relations when creating the table" do
          @connection.create_table :testings do |t|
            assert_raises(ArgumentError) do
              t.references :testing_parent, polymorphic: true, foreign_key: true
            end
          end
        end

        test "foreign keys can be created while changing the table" do
          @connection.create_table :testings
          @connection.change_table :testings do |t|
            t.references :testing_parent, foreign_key: true
          end

          fk = @connection.foreign_keys("testings").first
          assert_equal "testings", fk.from_table
          assert_equal "testing_parents", fk.to_table
        end

        test "foreign keys are not added by default when changing the table" do
          @connection.create_table :testings
          @connection.change_table :testings do |t|
            t.references :testing_parent
          end

          assert_equal [], @connection.foreign_keys("testings")
        end

        test "foreign keys accept options when changing the table" do
          @connection.change_table :testing_parents do |t|
            t.references :other, index: { unique: true }
          end
          @connection.create_table :testings
          @connection.change_table :testings do |t|
            t.references :testing_parent, foreign_key: { primary_key: :other_id }
          end

          fk = @connection.foreign_keys("testings").find { |k| k.to_table == "testing_parents" }
          assert_equal "other_id", fk.primary_key
        end

        test "foreign keys cannot be added to polymorphic relations when changing the table" do
          @connection.create_table :testings
          @connection.change_table :testings do |t|
            assert_raises(ArgumentError) do
              t.references :testing_parent, polymorphic: true, foreign_key: true
            end
          end
        end

        test "foreign key column can be removed" do
          @connection.create_table :testings do |t|
            t.references :testing_parent, index: true, foreign_key: true
          end

          assert_difference "@connection.foreign_keys('testings').size", -1 do
            @connection.remove_reference :testings, :testing_parent, foreign_key: true
          end
        end

        test "removing column removes foreign key" do
          @connection.create_table :testings do |t|
            t.references :testing_parent, index: true, foreign_key: true
          end

          assert_difference "@connection.foreign_keys('testings').size", -1 do
            @connection.remove_column :testings, :testing_parent_id
          end
        end

        test "foreign key methods respect pluralize_table_names" do
          begin
            original_pluralize_table_names = ActiveRecord::Base.pluralize_table_names
            ActiveRecord::Base.pluralize_table_names = false
            @connection.create_table :testing
            @connection.change_table :testing_parents do |t|
              t.references :testing, foreign_key: true
            end

            fk = @connection.foreign_keys("testing_parents").first
            assert_equal "testing_parents", fk.from_table
            assert_equal "testing", fk.to_table

            assert_difference "@connection.foreign_keys('testing_parents').size", -1 do
              @connection.remove_reference :testing_parents, :testing, foreign_key: true
            end
          ensure
            ActiveRecord::Base.pluralize_table_names = original_pluralize_table_names
            @connection.drop_table "testing", if_exists: true
          end
        end

        class CreateDogsMigration < ActiveRecord::Migration::Current
          def change
            create_table :dog_owners

            create_table :dogs do |t|
              t.references :dog_owner, foreign_key: true
            end
          end
        end

        def test_references_foreign_key_with_prefix
          ActiveRecord::Base.table_name_prefix = "p_"
          migration = CreateDogsMigration.new
          silence_stream($stdout) { migration.migrate(:up) }
          assert_equal 1, @connection.foreign_keys("p_dogs").size
        ensure
          silence_stream($stdout) { migration.migrate(:down) }
          ActiveRecord::Base.table_name_prefix = nil
        end

        def test_references_foreign_key_with_suffix
          ActiveRecord::Base.table_name_suffix = "_s"
          migration = CreateDogsMigration.new
          silence_stream($stdout) { migration.migrate(:up) }
          assert_equal 1, @connection.foreign_keys("dogs_s").size
        ensure
          silence_stream($stdout) { migration.migrate(:down) }
          ActiveRecord::Base.table_name_suffix = nil
        end

        test "multiple foreign keys can be added to the same table" do
          @connection.create_table :testings do |t|
            t.references :parent1, foreign_key: { to_table: :testing_parents }
            t.references :parent2, foreign_key: { to_table: :testing_parents }
          end

          fks = @connection.foreign_keys("testings").sort_by(&:column)

          fk_definitions = fks.map { |fk| [fk.from_table, fk.to_table, fk.column] }
          assert_equal([["testings", "testing_parents", "parent1_id"],
                        ["testings", "testing_parents", "parent2_id"]], fk_definitions)
        end

        test "multiple foreign keys can be removed to the selected one" do
          @connection.create_table :testings do |t|
            t.references :parent1, foreign_key: { to_table: :testing_parents }
            t.references :parent2, foreign_key: { to_table: :testing_parents }
          end

          assert_difference "@connection.foreign_keys('testings').size", -1 do
            @connection.remove_reference :testings, :parent1, foreign_key: { to_table: :testing_parents }
          end

          fks = @connection.foreign_keys("testings").sort_by(&:column)

          fk_definitions = fks.map { |fk| [fk.from_table, fk.to_table, fk.column] }
          assert_equal([["testings", "testing_parents", "parent2_id"]], fk_definitions)
        end
      end
    end
  end
else
  class ReferencesWithoutForeignKeySupportTest < ActiveRecord::TestCase
    setup do
      @connection = ActiveRecord::Base.connection
      @connection.create_table(:testing_parents, force: true)
    end

    teardown do
      @connection.drop_table("testings", if_exists: true)
      @connection.drop_table("testing_parents", if_exists: true)
    end

    test "ignores foreign keys defined with the table" do
      @connection.create_table :testings do |t|
        t.references :testing_parent, foreign_key: true
      end

      assert_includes @connection.data_sources, "testings"
    end
  end
end