aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/migration/foreign_key_test.rb
blob: c69fc18d82c520a98fed5e3f2148461ecfa0251f (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
require 'cases/helper'
require 'support/ddl_helper'
require 'support/schema_dumping_helper'

if ActiveRecord::Base.connection.supports_foreign_keys?
module ActiveRecord
  class Migration
    class ForeignKeyTest < ActiveRecord::TestCase
      include DdlHelper
      include SchemaDumpingHelper

      class Rocket < ActiveRecord::Base
      end

      class Astronaut < ActiveRecord::Base
      end

      setup do
        @connection = ActiveRecord::Base.connection
        @connection.create_table "rockets" do |t|
          t.string :name
        end

        @connection.create_table "astronauts" do |t|
          t.string :name
          t.references :rocket
        end
      end

      teardown do
        if defined?(@connection)
          @connection.execute "DROP TABLE IF EXISTS astronauts"
          @connection.execute "DROP TABLE IF EXISTS rockets"
        end
      end

      def test_foreign_keys
        foreign_keys = @connection.foreign_keys("fk_test_has_fk")
        assert_equal 1, foreign_keys.size

        fk = foreign_keys.first
        assert_equal "fk_test_has_fk", fk.from_table
        assert_equal "fk_test_has_pk", fk.to_table
        assert_equal "fk_id", fk.column
        assert_equal "id", fk.primary_key
        assert_equal "fk_name", fk.name
      end

      def test_add_foreign_key_inferes_column
        @connection.add_foreign_key :astronauts, :rockets

        foreign_keys = @connection.foreign_keys("astronauts")
        assert_equal 1, foreign_keys.size

        fk = foreign_keys.first
        assert_equal "astronauts", fk.from_table
        assert_equal "rockets", fk.to_table
        assert_equal "rocket_id", fk.column
        assert_equal "id", fk.primary_key
        assert_equal "astronauts_rocket_id_fk", fk.name
      end

      def test_add_foreign_key_with_column
        @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id"

        foreign_keys = @connection.foreign_keys("astronauts")
        assert_equal 1, foreign_keys.size

        fk = foreign_keys.first
        assert_equal "astronauts", fk.from_table
        assert_equal "rockets", fk.to_table
        assert_equal "rocket_id", fk.column
        assert_equal "id", fk.primary_key
        assert_equal "astronauts_rocket_id_fk", fk.name
      end

      def test_add_foreign_key_with_non_standard_primary_key
        with_example_table @connection, "space_shuttles", "pk integer PRIMARY KEY" do
          @connection.add_foreign_key(:astronauts, :space_shuttles,
                                      column: "rocket_id", primary_key: "pk", name: "custom_pk")

          foreign_keys = @connection.foreign_keys("astronauts")
          assert_equal 1, foreign_keys.size

          fk = foreign_keys.first
          assert_equal "astronauts", fk.from_table
          assert_equal "space_shuttles", fk.to_table
          assert_equal "pk", fk.primary_key

          @connection.remove_foreign_key :astronauts, name: "custom_pk"
        end
      end

      def test_add_on_delete_restrict_foreign_key
        @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :restrict

        foreign_keys = @connection.foreign_keys("astronauts")
        assert_equal 1, foreign_keys.size

        fk = foreign_keys.first
        if current_adapter?(:MysqlAdapter, :Mysql2Adapter)
          # ON DELETE RESTRICT is the default on MySQL
          assert_equal nil, fk.on_delete
        else
          assert_equal :restrict, fk.on_delete
        end
      end

      def test_add_on_delete_cascade_foreign_key
        @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :cascade

        foreign_keys = @connection.foreign_keys("astronauts")
        assert_equal 1, foreign_keys.size

        fk = foreign_keys.first
        assert_equal :cascade, fk.on_delete
      end

      def test_add_on_delete_nullify_foreign_key
        @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :nullify

        foreign_keys = @connection.foreign_keys("astronauts")
        assert_equal 1, foreign_keys.size

        fk = foreign_keys.first
        assert_equal :nullify, fk.on_delete
      end

      def test_add_foreign_key_with_on_update
        @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_update: :nullify

        foreign_keys = @connection.foreign_keys("astronauts")
        assert_equal 1, foreign_keys.size

        fk = foreign_keys.first
        assert_equal :nullify, fk.on_update
      end

      def test_remove_foreign_key_inferes_column
        @connection.add_foreign_key :astronauts, :rockets

        assert_equal 1, @connection.foreign_keys("astronauts").size
        @connection.remove_foreign_key :astronauts, :rockets
        assert_equal [], @connection.foreign_keys("astronauts")
      end

      def test_remove_foreign_key_by_column
        @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id"

        assert_equal 1, @connection.foreign_keys("astronauts").size
        @connection.remove_foreign_key :astronauts, column: "rocket_id"
        assert_equal [], @connection.foreign_keys("astronauts")
      end

      def test_remove_foreign_key_by_name
        @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", name: "fancy_named_fk"

        assert_equal 1, @connection.foreign_keys("astronauts").size
        @connection.remove_foreign_key :astronauts, name: "fancy_named_fk"
        assert_equal [], @connection.foreign_keys("astronauts")
      end

      def test_schema_dumping
        output = dump_table_schema "fk_test_has_fk"
        assert_match %r{\s+add_foreign_key "fk_test_has_fk", "fk_test_has_pk", column: "fk_id", primary_key: "id", name: "fk_name"$}, output
      end

      def test_schema_dumping_on_delete_and_on_update_options
        @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :nullify, on_update: :cascade

        output = dump_table_schema "astronauts"
        assert_match %r{\s+add_foreign_key "astronauts",.+on_update: :cascade,.+on_delete: :nullify$}, output
      end

      class CreateCitiesAndHousesMigration < ActiveRecord::Migration
        def change
          create_table("cities") { |t| }

          create_table("houses") do |t|
            t.column :city_id, :integer
          end
          add_foreign_key :houses, :cities, column: "city_id"
        end
      end

      def test_add_foreign_key_is_reversible
        migration = CreateCitiesAndHousesMigration.new
        silence_stream($stdout) { migration.migrate(:up) }
        assert_equal ["houses_city_id_fk"], @connection.foreign_keys("houses").map(&:name)
      ensure
        silence_stream($stdout) { migration.migrate(:down) }
      end
    end
  end
end
end