@@ -152,6 +152,172 @@ class ::TestEmployee < ActiveRecord::Base; end
152
152
end
153
153
end
154
154
155
+ describe "create table with primary key trigger" do
156
+ def create_table_with_trigger ( options = { } )
157
+ options . merge! primary_key_trigger : true , force : true
158
+ schema_define do
159
+ create_table :test_employees , options do |t |
160
+ t . string :first_name
161
+ t . string :last_name
162
+ end
163
+ end
164
+ end
165
+
166
+ def create_table_and_separately_trigger ( options = { } )
167
+ options . merge! force : true
168
+ schema_define do
169
+ create_table :test_employees , options do |t |
170
+ t . string :first_name
171
+ t . string :last_name
172
+ end
173
+ add_primary_key_trigger :test_employees , options
174
+ end
175
+ end
176
+
177
+ def drop_table_with_trigger ( options = { } )
178
+ seq_name = options [ :sequence_name ]
179
+ schema_define do
180
+ drop_table :test_employees , ( seq_name ? { sequence_name : seq_name } : { } )
181
+ end
182
+ Object . send ( :remove_const , "TestEmployee" )
183
+ ActiveRecord ::Base . clear_cache!
184
+ end
185
+
186
+ describe "with default primary key" do
187
+ before ( :all ) do
188
+ @conn = ActiveRecord ::Base . connection
189
+ create_table_with_trigger
190
+ class ::TestEmployee < ActiveRecord ::Base
191
+ end
192
+ end
193
+
194
+ after ( :all ) do
195
+ drop_table_with_trigger
196
+ end
197
+
198
+ it "should populate primary key using trigger" do
199
+ expect do
200
+ @conn . execute "INSERT INTO test_employees (first_name) VALUES ('Raimonds')"
201
+ end . not_to raise_error
202
+ end
203
+
204
+ it "should return new key value using connection insert method" do
205
+ insert_id = @conn . insert ( "INSERT INTO test_employees (first_name) VALUES ('Raimonds')" , nil , "id" )
206
+ expect ( @conn . select_value ( "SELECT test_employees_seq.currval FROM dual" ) ) . to eq ( insert_id )
207
+ end
208
+
209
+ it "should create new record for model" do
210
+ e = TestEmployee . create! ( first_name : "Raimonds" )
211
+ expect ( @conn . select_value ( "SELECT test_employees_seq.currval FROM dual" ) ) . to eq ( e . id )
212
+ end
213
+
214
+ it "should not generate NoMethodError for :returning_id:Symbol" do
215
+ set_logger
216
+ @conn . reconnect! unless @conn . active?
217
+ @conn . insert ( "INSERT INTO test_employees (first_name) VALUES ('Yasuo')" , nil , "id" )
218
+ expect ( @logger . output ( :error ) ) . not_to match ( /^Could not log "sql.active_record" event. NoMethodError: undefined method `name' for :returning_id:Symbol/ )
219
+ clear_logger
220
+ end
221
+
222
+ end
223
+
224
+ describe "with separate creation of primary key trigger" do
225
+ before ( :all ) do
226
+ ActiveRecord ::Base . establish_connection ( CONNECTION_PARAMS )
227
+ @conn = ActiveRecord ::Base . connection
228
+ create_table_and_separately_trigger
229
+ class ::TestEmployee < ActiveRecord ::Base
230
+ end
231
+ end
232
+
233
+ after ( :all ) do
234
+ drop_table_with_trigger
235
+ end
236
+
237
+ it "should populate primary key using trigger" do
238
+ expect do
239
+ @conn . execute "INSERT INTO test_employees (first_name) VALUES ('Raimonds')"
240
+ end . not_to raise_error
241
+ end
242
+
243
+ it "should return new key value using connection insert method" do
244
+ insert_id = @conn . insert ( "INSERT INTO test_employees (first_name) VALUES ('Raimonds')" , nil , "id" )
245
+ expect ( @conn . select_value ( "SELECT test_employees_seq.currval FROM dual" ) ) . to eq ( insert_id )
246
+ end
247
+
248
+ it "should create new record for model" do
249
+ e = TestEmployee . create! ( first_name : "Raimonds" )
250
+ expect ( @conn . select_value ( "SELECT test_employees_seq.currval FROM dual" ) ) . to eq ( e . id )
251
+ end
252
+ end
253
+
254
+ describe "with non-default primary key and non-default sequence name" do
255
+ before ( :all ) do
256
+ ActiveRecord ::Base . establish_connection ( CONNECTION_PARAMS )
257
+ @conn = ActiveRecord ::Base . connection
258
+ @primary_key = "employee_id"
259
+ @sequence_name = "test_employees_s"
260
+ create_table_with_trigger ( primary_key : @primary_key , sequence_name : @sequence_name )
261
+ class ::TestEmployee < ActiveRecord ::Base
262
+ self . primary_key = "employee_id"
263
+ end
264
+ end
265
+
266
+ after ( :all ) do
267
+ drop_table_with_trigger ( sequence_name : @sequence_name )
268
+ end
269
+
270
+ it "should populate primary key using trigger" do
271
+ expect do
272
+ @conn . execute "INSERT INTO test_employees (first_name) VALUES ('Raimonds')"
273
+ end . not_to raise_error
274
+ end
275
+
276
+ it "should return new key value using connection insert method" do
277
+ insert_id = @conn . insert ( "INSERT INTO test_employees (first_name) VALUES ('Raimonds')" , nil , @primary_key )
278
+ expect ( @conn . select_value ( "SELECT #{ @sequence_name } .currval FROM dual" ) ) . to eq ( insert_id )
279
+ end
280
+
281
+ it "should create new record for model with autogenerated sequence option" do
282
+ e = TestEmployee . create! ( first_name : "Raimonds" )
283
+ expect ( @conn . select_value ( "SELECT #{ @sequence_name } .currval FROM dual" ) ) . to eq ( e . id )
284
+ end
285
+ end
286
+
287
+ describe "with non-default sequence name and non-default trigger name" do
288
+ before ( :all ) do
289
+ ActiveRecord ::Base . establish_connection ( CONNECTION_PARAMS )
290
+ @conn = ActiveRecord ::Base . connection
291
+ @sequence_name = "test_employees_s"
292
+ create_table_with_trigger ( sequence_name : @sequence_name , trigger_name : "test_employees_t1" )
293
+ class ::TestEmployee < ActiveRecord ::Base
294
+ self . sequence_name = :autogenerated
295
+ end
296
+ end
297
+
298
+ after ( :all ) do
299
+ drop_table_with_trigger ( sequence_name : @sequence_name )
300
+ end
301
+
302
+ it "should populate primary key using trigger" do
303
+ expect do
304
+ @conn . execute "INSERT INTO test_employees (first_name) VALUES ('Raimonds')"
305
+ end . not_to raise_error
306
+ end
307
+
308
+ it "should return new key value using connection insert method" do
309
+ insert_id = @conn . insert ( "INSERT INTO test_employees (first_name) VALUES ('Raimonds')" , nil , "id" )
310
+ expect ( @conn . select_value ( "SELECT #{ @sequence_name } .currval FROM dual" ) ) . to eq ( insert_id )
311
+ end
312
+
313
+ it "should create new record for model with autogenerated sequence option" do
314
+ e = TestEmployee . create! ( first_name : "Raimonds" )
315
+ expect ( @conn . select_value ( "SELECT #{ @sequence_name } .currval FROM dual" ) ) . to eq ( e . id )
316
+ end
317
+ end
318
+
319
+ end
320
+
155
321
describe "table and column comments" do
156
322
def create_test_employees_table ( table_comment = nil , column_comments = { } )
157
323
schema_define do
@@ -302,6 +468,44 @@ class ::TestEmployee < ActiveRecord::Base; end
302
468
end
303
469
end
304
470
471
+ describe "create triggers" do
472
+
473
+ before ( :all ) do
474
+ @conn = ActiveRecord ::Base . connection
475
+ schema_define do
476
+ create_table :test_employees do |t |
477
+ t . string :first_name
478
+ t . string :last_name
479
+ end
480
+ end
481
+ class ::TestEmployee < ActiveRecord ::Base ; end
482
+ end
483
+
484
+ after ( :all ) do
485
+ schema_define do
486
+ drop_table :test_employees
487
+ end
488
+ Object . send ( :remove_const , "TestEmployee" )
489
+ ActiveRecord ::Base . clear_cache!
490
+ end
491
+
492
+ it "should create table trigger with :new reference" do
493
+ expect do
494
+ @conn . execute <<-SQL
495
+ CREATE OR REPLACE TRIGGER test_employees_pkt
496
+ BEFORE INSERT ON test_employees FOR EACH ROW
497
+ BEGIN
498
+ IF inserting THEN
499
+ IF :new.id IS NULL THEN
500
+ SELECT test_employees_seq.NEXTVAL INTO :new.id FROM dual;
501
+ END IF;
502
+ END IF;
503
+ END;
504
+ SQL
505
+ end . not_to raise_error
506
+ end
507
+ end
508
+
305
509
describe "add index" do
306
510
before ( :all ) do
307
511
@conn = ActiveRecord ::Base . connection
0 commit comments