pg_examiner 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe PGExaminer do
@@ -23,7 +25,7 @@ describe PGExaminer do
23
25
  plpgsql.name.should == 'plpgsql'
24
26
  plpgsql.schema.should be nil
25
27
 
26
- result1.diff(result2)[:extensions].should == {added: ['citext']}
27
- result2.diff(result1)[:extensions].should == {removed: ['citext']}
28
+ result1.diff(result2)["extensions"].should == {"added" => ['citext']}
29
+ result2.diff(result1)["extensions"].should == {"removed" => ['citext']}
28
30
  end
29
31
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe PGExaminer do
@@ -30,8 +32,8 @@ describe PGExaminer do
30
32
  a.should_not == c
31
33
  b.should_not == c
32
34
 
33
- a.diff(c).should == {:schemas=>{"public"=>{:functions=>{:added=>["add_numbers"], :removed=>["add"]}}}}
34
- b.diff(c).should == {:schemas=>{"public"=>{:functions=>{:added=>["add_numbers"], :removed=>["add"]}}}}
35
+ a.diff(c).should == {"schemas"=>{"public"=>{"functions"=>{"added"=>["add_numbers"], "removed"=>["add"]}}}}
36
+ b.diff(c).should == {"schemas"=>{"public"=>{"functions"=>{"added"=>["add_numbers"], "removed"=>["add"]}}}}
35
37
  end
36
38
 
37
39
  it "should be able to differentiate between functions by their argument types" do
@@ -74,8 +76,8 @@ describe PGExaminer do
74
76
  b.should_not == d
75
77
  c.should_not == d
76
78
 
77
- a.diff(b).should == {:schemas=>{"public"=>{:functions=>{"add"=>{:definition=>{"CREATE OR REPLACE FUNCTION public.add(one integer, two integer)\n RETURNS integer\n LANGUAGE sql\nAS $function$\n SELECT one + two\n $function$\n"=>"CREATE OR REPLACE FUNCTION public.add(one integer, two integer, three integer)\n RETURNS integer\n LANGUAGE sql\nAS $function$\n SELECT one + two\n $function$\n"}, :argument_types=>{["int4", "int4"]=>["int4", "int4", "int4"]}}}}}}
78
- a.diff(c).should == {:schemas=>{"public"=>{:functions=>{"add"=>{:definition=>{"CREATE OR REPLACE FUNCTION public.add(one integer, two integer)\n RETURNS integer\n LANGUAGE sql\nAS $function$\n SELECT one + two\n $function$\n"=>"CREATE OR REPLACE FUNCTION public.add(one integer, two integer, three integer[])\n RETURNS integer\n LANGUAGE sql\nAS $function$\n SELECT one + two\n $function$\n"}, :argument_types=>{["int4", "int4"]=>["int4", "int4", "_int4"]}}}}}}
79
+ a.diff(b).should == {"schemas"=>{"public"=>{"functions"=>{"add"=>{"function definition"=>{"CREATE OR REPLACE FUNCTION public.add(one integer, two integer)\n RETURNS integer\n LANGUAGE sql\nAS $function$\n SELECT one + two\n $function$\n"=>"CREATE OR REPLACE FUNCTION public.add(one integer, two integer, three integer)\n RETURNS integer\n LANGUAGE sql\nAS $function$\n SELECT one + two\n $function$\n"}, "argument types"=>{["int4", "int4"]=>["int4", "int4", "int4"]}}}}}}
80
+ a.diff(c).should == {"schemas"=>{"public"=>{"functions"=>{"add"=>{"function definition"=>{"CREATE OR REPLACE FUNCTION public.add(one integer, two integer)\n RETURNS integer\n LANGUAGE sql\nAS $function$\n SELECT one + two\n $function$\n"=>"CREATE OR REPLACE FUNCTION public.add(one integer, two integer, three integer[])\n RETURNS integer\n LANGUAGE sql\nAS $function$\n SELECT one + two\n $function$\n"}, "argument types"=>{["int4", "int4"]=>["int4", "int4", "_int4"]}}}}}}
79
81
  end
80
82
 
81
83
  it "should be able to differentiate between functions by their argument defaults" do
@@ -149,7 +151,7 @@ describe PGExaminer do
149
151
 
150
152
  a.should_not == b
151
153
 
152
- a.diff(b)[:schemas]['public'][:functions]['add'][:language].should == {'sql' => 'plpgsql'}
154
+ a.diff(b)["schemas"]['public']["functions"]['add']["language"].should == {'sql' => 'plpgsql'}
153
155
  end
154
156
 
155
157
  it "should be able to differentiate between functions by their other flags" do
@@ -253,217 +255,4 @@ describe PGExaminer do
253
255
  a.should_not == c
254
256
  a.should_not == d
255
257
  end
256
-
257
- it "should be able to differentiate between triggers by their names" do
258
- a = examine <<-SQL
259
- CREATE TABLE test_table (
260
- a integer
261
- );
262
-
263
- CREATE FUNCTION func() RETURNS trigger AS $$
264
- BEGIN
265
- NEW.a = 56;
266
- RETURN NEW;
267
- END;
268
- $$
269
- LANGUAGE plpgsql;
270
-
271
- CREATE TRIGGER trig BEFORE INSERT ON test_table FOR EACH ROW EXECUTE PROCEDURE func();
272
- SQL
273
-
274
- b = examine <<-SQL
275
- CREATE TABLE test_table (
276
- a integer
277
- );
278
-
279
- CREATE FUNCTION func() RETURNS trigger AS $$
280
- BEGIN
281
- NEW.a = 56;
282
- RETURN NEW;
283
- END;
284
- $$
285
- LANGUAGE plpgsql;
286
-
287
- CREATE TRIGGER trig BEFORE INSERT ON test_table FOR EACH ROW EXECUTE PROCEDURE func();
288
- SQL
289
-
290
- c = examine <<-SQL
291
- CREATE TABLE test_table (
292
- a integer
293
- );
294
-
295
- CREATE FUNCTION func() RETURNS trigger AS $$
296
- BEGIN
297
- NEW.a = 56;
298
- RETURN NEW;
299
- END;
300
- $$
301
- LANGUAGE plpgsql;
302
-
303
- CREATE TRIGGER trig2 BEFORE INSERT ON test_table FOR EACH ROW EXECUTE PROCEDURE func();
304
- SQL
305
-
306
- a.should == b
307
- a.should_not == c
308
- b.should_not == c
309
-
310
- a.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:triggers=>{:added=>["trig2"], :removed=>["trig"]}}}}}}
311
- b.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:triggers=>{:added=>["trig2"], :removed=>["trig"]}}}}}}
312
- end
313
-
314
- it "should be able to differentiate between triggers by their parent tables" do
315
- a = examine <<-SQL
316
- CREATE TABLE test_table_a (
317
- a integer
318
- );
319
-
320
- CREATE TABLE test_table_b (
321
- a integer
322
- );
323
-
324
- CREATE FUNCTION func() RETURNS trigger AS $$
325
- BEGIN
326
- NEW.a = 56;
327
- RETURN NEW;
328
- END;
329
- $$
330
- LANGUAGE plpgsql;
331
-
332
- CREATE TRIGGER trig BEFORE INSERT ON test_table_a FOR EACH ROW EXECUTE PROCEDURE func();
333
- SQL
334
-
335
- b = examine <<-SQL
336
- CREATE TABLE test_table_a (
337
- a integer
338
- );
339
-
340
- CREATE TABLE test_table_b (
341
- a integer
342
- );
343
-
344
- CREATE FUNCTION func() RETURNS trigger AS $$
345
- BEGIN
346
- NEW.a = 56;
347
- RETURN NEW;
348
- END;
349
- $$
350
- LANGUAGE plpgsql;
351
-
352
- CREATE TRIGGER trig BEFORE INSERT ON test_table_b FOR EACH ROW EXECUTE PROCEDURE func();
353
- SQL
354
-
355
- a.should_not == b
356
-
357
- a.diff(b).should == {:schemas=>{"public"=>{:tables=>{"test_table_a"=>{:triggers=>{:removed=>["trig"]}}, "test_table_b"=>{:triggers=>{:added=>["trig"]}}}}}}
358
- end
359
-
360
- it "should be able to differentiate between triggers by their associated functions" do
361
- a = examine <<-SQL
362
- CREATE TABLE test_table (
363
- a integer
364
- );
365
-
366
- CREATE FUNCTION func1() RETURNS trigger AS $$
367
- BEGIN
368
- NEW.a = 56;
369
- RETURN NEW;
370
- END;
371
- $$
372
- LANGUAGE plpgsql;
373
-
374
- CREATE FUNCTION func2() RETURNS trigger AS $$
375
- BEGIN
376
- NEW.a = 56;
377
- RETURN NEW;
378
- END;
379
- $$
380
- LANGUAGE plpgsql;
381
-
382
- CREATE TRIGGER trig BEFORE INSERT ON test_table FOR EACH ROW EXECUTE PROCEDURE func1();
383
- SQL
384
-
385
- b = examine <<-SQL
386
- CREATE TABLE test_table (
387
- a integer
388
- );
389
-
390
- CREATE FUNCTION func1() RETURNS trigger AS $$
391
- BEGIN
392
- NEW.a = 56;
393
- RETURN NEW;
394
- END;
395
- $$
396
- LANGUAGE plpgsql;
397
-
398
- CREATE FUNCTION func2() RETURNS trigger AS $$
399
- BEGIN
400
- NEW.a = 56;
401
- RETURN NEW;
402
- END;
403
- $$
404
- LANGUAGE plpgsql;
405
-
406
- CREATE TRIGGER trig BEFORE INSERT ON test_table FOR EACH ROW EXECUTE PROCEDURE func2();
407
- SQL
408
-
409
- a.should_not == b
410
-
411
- a.diff(b).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:triggers=>{"trig"=>{:function=>{"func1"=>"func2"}}}}}}}}
412
- end
413
-
414
- it "should be able to differentiate between triggers by their firing conditions" do
415
- a = examine <<-SQL
416
- CREATE TABLE test_table (
417
- a integer
418
- );
419
-
420
- CREATE FUNCTION func() RETURNS trigger AS $$
421
- BEGIN
422
- NEW.a = 56;
423
- RETURN NEW;
424
- END;
425
- $$
426
- LANGUAGE plpgsql;
427
-
428
- CREATE TRIGGER trig BEFORE INSERT ON test_table FOR EACH ROW EXECUTE PROCEDURE func();
429
- SQL
430
-
431
- b = examine <<-SQL
432
- CREATE TABLE test_table (
433
- a integer
434
- );
435
-
436
- CREATE FUNCTION func() RETURNS trigger AS $$
437
- BEGIN
438
- NEW.a = 56;
439
- RETURN NEW;
440
- END;
441
- $$
442
- LANGUAGE plpgsql;
443
-
444
- CREATE TRIGGER trig BEFORE UPDATE ON test_table FOR EACH ROW EXECUTE PROCEDURE func();
445
- SQL
446
-
447
- c = examine <<-SQL
448
- CREATE TABLE test_table (
449
- a integer
450
- );
451
-
452
- CREATE FUNCTION func() RETURNS trigger AS $$
453
- BEGIN
454
- NEW.a = 56;
455
- RETURN NEW;
456
- END;
457
- $$
458
- LANGUAGE plpgsql;
459
-
460
- CREATE TRIGGER trig BEFORE DELETE ON test_table FOR EACH ROW EXECUTE PROCEDURE func();
461
- SQL
462
-
463
- a.should_not == b
464
- a.should_not == c
465
- b.should_not == c
466
-
467
- a.diff(b).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:triggers=>{"trig"=>{:tgtype=>{"7"=>"19"}}}}}}}}
468
- end
469
258
  end
data/spec/index_spec.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe PGExaminer do
@@ -30,8 +32,8 @@ describe PGExaminer do
30
32
  a.should_not == c
31
33
  b.should_not == c
32
34
 
33
- a.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:indexes=>{:added=>["int_idx2"], :removed=>["int_idx"]}}}}}}
34
- b.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:indexes=>{:added=>["int_idx2"], :removed=>["int_idx"]}}}}}}
35
+ a.diff(c).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"indexes"=>{"added"=>["int_idx2"], "removed"=>["int_idx"]}}}}}}
36
+ b.diff(c).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"indexes"=>{"added"=>["int_idx2"], "removed"=>["int_idx"]}}}}}}
35
37
  end
36
38
 
37
39
  it "should consider the columns indexes are on when determining equivalency" do
@@ -66,9 +68,9 @@ describe PGExaminer do
66
68
  a.should_not == c
67
69
  b.should_not == c
68
70
 
69
- a.diff(b).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:indexes=>{"int_idx"=>{:expression=>{["a"]=>["b"]}}}}}}}}
70
- a.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:indexes=>{"int_idx"=>{:expression=>{["a"]=>["a", "b"]}}}}}}}}
71
- b.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:indexes=>{"int_idx"=>{:expression=>{["b"]=>["a", "b"]}}}}}}}}
71
+ a.diff(b).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"indexes"=>{"int_idx"=>{"expression"=>{["a"]=>["b"]}}}}}}}}
72
+ a.diff(c).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"indexes"=>{"int_idx"=>{"expression"=>{["a"]=>["a", "b"]}}}}}}}}
73
+ b.diff(c).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"indexes"=>{"int_idx"=>{"expression"=>{["b"]=>["a", "b"]}}}}}}}}
72
74
  end
73
75
 
74
76
  it "should consider the filters indexes have when determining equivalency" do
@@ -104,8 +106,8 @@ describe PGExaminer do
104
106
  a.should_not == c
105
107
  b.should_not == c
106
108
 
107
- a.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:indexes=>{"int_idx"=>{:filter=>{"(a > 0)"=>nil}}}}}}}}
108
- b.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:indexes=>{"int_idx"=>{:filter=>{"(a > 0)"=>nil}}}}}}}}
109
+ a.diff(c).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"indexes"=>{"int_idx"=>{"filter expression"=>{"(a > 0)"=>nil}}}}}}}}
110
+ b.diff(c).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"indexes"=>{"int_idx"=>{"filter expression"=>{"(a > 0)"=>nil}}}}}}}}
109
111
  end
110
112
 
111
113
  it "should consider the expressions indexes are on, if any" do
@@ -141,8 +143,8 @@ describe PGExaminer do
141
143
  a.should_not == c
142
144
  b.should_not == c
143
145
 
144
- a.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:indexes=>{"text_idx"=>{:expression=>{"lower(a)"=>["a"]}}}}}}}}
145
- b.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:indexes=>{"text_idx"=>{:expression=>{"lower(a)"=>["a"]}}}}}}}}
146
+ a.diff(c).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"indexes"=>{"text_idx"=>{"expression"=>{"lower(a)"=>["a"]}}}}}}}}
147
+ b.diff(c).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"indexes"=>{"text_idx"=>{"expression"=>{"lower(a)"=>["a"]}}}}}}}}
146
148
  end
147
149
 
148
150
  it "should consider the uniqueness and primary key status of an index, if any" do
@@ -174,8 +176,127 @@ describe PGExaminer do
174
176
  a.should_not == c
175
177
  b.should_not == c
176
178
 
177
- a.diff(b).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:indexes=>{"int_idx"=>{:indisunique=>{"f"=>"t"}}}}}}}}
178
- a.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:columns=>{"a"=>{:attnotnull=>{"f"=>"t"}}}, :indexes=>{:added=>["test_table_pkey"], :removed=>["int_idx"]}, :constraints=>{:added=>["test_table_pkey"]}}}}}}
179
- b.diff(c).should == {:schemas=>{"public"=>{:tables=>{"test_table"=>{:columns=>{"a"=>{:attnotnull=>{"f"=>"t"}}}, :indexes=>{:added=>["test_table_pkey"], :removed=>["int_idx"]}, :constraints=>{:added=>["test_table_pkey"]}}}}}}
179
+ a.diff(b).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"indexes"=>{"int_idx"=>{"index is unique"=>{"f"=>"t"}}}}}}}}
180
+ a.diff(c).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"columns"=>{"a"=>{"column is marked not-null"=>{"f"=>"t"}}}, "indexes"=>{"added"=>["test_table_pkey"], "removed"=>["int_idx"]}, "constraints"=>{"added"=>["test_table_pkey"]}}}}}}
181
+ b.diff(c).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"columns"=>{"a"=>{"column is marked not-null"=>{"f"=>"t"}}}, "indexes"=>{"added"=>["test_table_pkey"], "removed"=>["int_idx"]}, "constraints"=>{"added"=>["test_table_pkey"]}}}}}}
182
+ end
183
+
184
+ it "should recognize the difference between a unique index and a unique constraint" do
185
+ a = examine <<-SQL
186
+ CREATE TABLE test_table (
187
+ a integer,
188
+ UNIQUE (a)
189
+ );
190
+ SQL
191
+
192
+ b = examine <<-SQL
193
+ CREATE TABLE test_table (
194
+ a integer UNIQUE
195
+ );
196
+ SQL
197
+
198
+ c = examine <<-SQL
199
+ CREATE TABLE test_table (
200
+ a integer
201
+ );
202
+
203
+ ALTER TABLE test_table ADD CONSTRAINT test_table_a_key UNIQUE (a);
204
+ SQL
205
+
206
+ d = examine <<-SQL
207
+ CREATE TABLE test_table (
208
+ a integer
209
+ );
210
+
211
+ CREATE UNIQUE INDEX test_table_a_key ON test_table (a);
212
+ SQL
213
+
214
+ a.should == b
215
+ a.should == c
216
+ a.should_not == d
217
+
218
+ a.diff(d).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"constraints"=>{"removed"=>["test_table_a_key"]}}}}}}
219
+ end
220
+
221
+ it "should recognize the difference between unique indices with different deferrable states" do
222
+ a = examine <<-SQL
223
+ CREATE TABLE test_table (
224
+ a integer
225
+ );
226
+
227
+ ALTER TABLE test_table ADD CONSTRAINT test_table_a_key UNIQUE (a);
228
+ SQL
229
+
230
+ b = examine <<-SQL
231
+ CREATE TABLE test_table (
232
+ a integer
233
+ );
234
+
235
+ ALTER TABLE test_table ADD CONSTRAINT test_table_a_key UNIQUE (a) NOT DEFERRABLE;
236
+ SQL
237
+
238
+ c = examine <<-SQL
239
+ CREATE TABLE test_table (
240
+ a integer
241
+ );
242
+
243
+ ALTER TABLE test_table ADD CONSTRAINT test_table_a_key UNIQUE (a) DEFERRABLE;
244
+ SQL
245
+
246
+ d = examine <<-SQL
247
+ CREATE TABLE test_table (
248
+ a integer
249
+ );
250
+
251
+ ALTER TABLE test_table ADD CONSTRAINT test_table_a_key UNIQUE (a) DEFERRABLE INITIALLY IMMEDIATE;
252
+ SQL
253
+
254
+ e = examine <<-SQL
255
+ CREATE TABLE test_table (
256
+ a integer
257
+ );
258
+
259
+ ALTER TABLE test_table ADD CONSTRAINT test_table_a_key UNIQUE (a) DEFERRABLE INITIALLY DEFERRED;
260
+ SQL
261
+
262
+ c2 = examine <<-SQL
263
+ CREATE TABLE test_table (
264
+ a integer
265
+ );
266
+
267
+ ALTER TABLE test_table ADD CONSTRAINT test_table_a_key UNIQUE (a) DEFERRABLE;
268
+ SQL
269
+
270
+ d2 = examine <<-SQL
271
+ CREATE TABLE test_table (
272
+ a integer
273
+ );
274
+
275
+ ALTER TABLE test_table ADD CONSTRAINT test_table_a_key UNIQUE (a) DEFERRABLE INITIALLY IMMEDIATE;
276
+ SQL
277
+
278
+ e2 = examine <<-SQL
279
+ CREATE TABLE test_table (
280
+ a integer
281
+ );
282
+
283
+ ALTER TABLE test_table ADD CONSTRAINT test_table_a_key UNIQUE (a) DEFERRABLE INITIALLY DEFERRED;
284
+ SQL
285
+
286
+ a.should == b
287
+ a.should_not == c
288
+ a.should_not == d
289
+ c.should == d
290
+ c.should_not == e
291
+ d.should_not == e
292
+
293
+ c.should == c2
294
+ d.should == d2
295
+ e.should == e2
296
+
297
+ a.diff(c).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"constraints"=>{"test_table_a_key"=>{"constraint definition"=>{"UNIQUE (a)"=>"UNIQUE (a) DEFERRABLE"}}}}}}}}
298
+ a.diff(d).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"constraints"=>{"test_table_a_key"=>{"constraint definition"=>{"UNIQUE (a)"=>"UNIQUE (a) DEFERRABLE"}}}}}}}}
299
+ c.diff(e).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"constraints"=>{"test_table_a_key"=>{"constraint definition"=>{"UNIQUE (a) DEFERRABLE"=>"UNIQUE (a) DEFERRABLE INITIALLY DEFERRED"}}}}}}}}
300
+ d.diff(e).should == {"schemas"=>{"public"=>{"tables"=>{"test_table"=>{"constraints"=>{"test_table_a_key"=>{"constraint definition"=>{"UNIQUE (a) DEFERRABLE"=>"UNIQUE (a) DEFERRABLE INITIALLY DEFERRED"}}}}}}}}
180
301
  end
181
302
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe PGExaminer do