@@ -120,7 +120,101 @@ def up
120
120
] , calls [ 1 ..4 ] . map ( &:first )
121
121
end
122
122
123
- def test_add_column_before_pg_11
123
+ def test_add_column_before_pg_11_without_null_or_default_value
124
+ @connection . create_table ( :users )
125
+ @migration =
126
+ Class . new ( ActiveRecord ::Migration ::Current ) do
127
+ def up
128
+ add_column ( :users , :admin , :boolean )
129
+ end
130
+ end . new
131
+
132
+ SafePgMigrations . stub ( :get_pg_version_num , 96_000 ) do
133
+ execute_calls = nil
134
+ write_calls =
135
+ record_calls ( @migration , :write ) do
136
+ execute_calls = record_calls ( @connection , :execute ) { run_migration }
137
+ end
138
+ assert_calls [
139
+ # The column is added without any default.
140
+ 'ALTER TABLE "users" ADD "admin" boolean' ,
141
+ ] , execute_calls
142
+
143
+ assert_equal [
144
+ '== 8128 : migrating ===========================================================' ,
145
+ '-- add_column(:users, :admin, :boolean)' ,
146
+ ] , write_calls . map ( &:first ) [ 0 ...-3 ]
147
+ end
148
+ end
149
+
150
+ def test_add_column_before_pg_11_with_null_constraint
151
+ @connection . create_table ( :users )
152
+ @migration =
153
+ Class . new ( ActiveRecord ::Migration ::Current ) do
154
+ def up
155
+ add_column ( :users , :admin , :boolean , null : false )
156
+ end
157
+ end . new
158
+
159
+ SafePgMigrations . stub ( :get_pg_version_num , 96_000 ) do
160
+ execute_calls = nil
161
+ write_calls =
162
+ record_calls ( @migration , :write ) do
163
+ execute_calls = record_calls ( @connection , :execute ) { run_migration }
164
+ end
165
+ assert_calls [
166
+ # The column is added without any default.
167
+ 'ALTER TABLE "users" ADD "admin" boolean' ,
168
+
169
+ # The not-null constraint is added.
170
+ "SET statement_timeout TO '5s'" ,
171
+ 'ALTER TABLE "users" ALTER COLUMN "admin" SET NOT NULL' ,
172
+ "SET statement_timeout TO '70s'" ,
173
+ ] , execute_calls
174
+
175
+ assert_equal [
176
+ '== 8128 : migrating ===========================================================' ,
177
+ '-- add_column(:users, :admin, :boolean, {:null=>false})' ,
178
+ ' -> add_column("users", :admin, :boolean, {})' ,
179
+ ' -> change_column_null("users", :admin, false)' ,
180
+ ] , write_calls . map ( &:first ) [ 0 ...-3 ]
181
+ end
182
+ end
183
+
184
+ def test_add_column_before_pg_11_with_default_value
185
+ @connection . create_table ( :users )
186
+ @migration =
187
+ Class . new ( ActiveRecord ::Migration ::Current ) do
188
+ def up
189
+ add_column ( :users , :admin , :boolean , default : false )
190
+ end
191
+ end . new
192
+
193
+ SafePgMigrations . stub ( :get_pg_version_num , 96_000 ) do
194
+ execute_calls = nil
195
+ write_calls =
196
+ record_calls ( @migration , :write ) do
197
+ execute_calls = record_calls ( @connection , :execute ) { run_migration }
198
+ end
199
+ assert_calls [
200
+ # The column is added without any default.
201
+ 'ALTER TABLE "users" ADD "admin" boolean' ,
202
+
203
+ # The default is added.
204
+ 'ALTER TABLE "users" ALTER COLUMN "admin" SET DEFAULT FALSE' ,
205
+ ] , execute_calls
206
+
207
+ assert_equal [
208
+ '== 8128 : migrating ===========================================================' ,
209
+ '-- add_column(:users, :admin, :boolean, {:default=>false})' ,
210
+ ' -> add_column("users", :admin, :boolean, {})' ,
211
+ ' -> change_column_default("users", :admin, false)' ,
212
+ ' -> backfill_column_default("users", :admin)' ,
213
+ ] , write_calls . map ( &:first ) [ 0 ...-3 ]
214
+ end
215
+ end
216
+
217
+ def test_add_column_before_pg_11_with_null_and_default_value
124
218
@connection . create_table ( :users )
125
219
@migration =
126
220
Class . new ( ActiveRecord ::Migration ::Current ) do
@@ -175,20 +269,13 @@ def up
175
269
execute_calls = record_calls ( @connection , :execute ) { run_migration }
176
270
end
177
271
assert_calls [
178
- # The column is added with the default without any trick
179
- 'ALTER TABLE "users" ADD "admin" boolean DEFAULT FALSE' ,
180
-
181
- # The not-null constraint is added.
182
- "SET statement_timeout TO '5s'" ,
183
- 'ALTER TABLE "users" ALTER COLUMN "admin" SET NOT NULL' ,
184
- "SET statement_timeout TO '70s'" ,
272
+ # The column is added with the default and not null constraint without any tricks
273
+ 'ALTER TABLE "users" ADD "admin" boolean DEFAULT FALSE NOT NULL' ,
185
274
] , execute_calls
186
275
187
276
assert_equal [
188
277
'== 8128 : migrating ===========================================================' ,
189
278
'-- add_column(:users, :admin, :boolean, {:default=>false, :null=>false})' ,
190
- ' -> add_column("users", :admin, :boolean, {:default=>false})' ,
191
- ' -> change_column_null("users", :admin, false)' ,
192
279
] , write_calls . map ( &:first ) [ 0 ...-3 ]
193
280
end
194
281
end
0 commit comments