pg_trunk 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +4 -15
  3. data/CHANGELOG.md +6 -0
  4. data/README.md +1 -0
  5. data/lib/pg_trunk/operations/check_constraints/add_check_constraint.rb +3 -3
  6. data/lib/pg_trunk/operations/check_constraints/drop_check_constraint.rb +4 -4
  7. data/lib/pg_trunk/operations/check_constraints/rename_check_constraint.rb +2 -2
  8. data/lib/pg_trunk/operations/check_constraints/validate_check_constraint.rb +1 -1
  9. data/lib/pg_trunk/operations/composite_types/change_composite_type.rb +2 -2
  10. data/lib/pg_trunk/operations/composite_types/create_composite_type.rb +1 -1
  11. data/lib/pg_trunk/operations/composite_types/drop_composite_type.rb +3 -3
  12. data/lib/pg_trunk/operations/composite_types/rename_composite_type.rb +1 -1
  13. data/lib/pg_trunk/operations/domains/create_domain.rb +5 -5
  14. data/lib/pg_trunk/operations/domains/drop_domain.rb +6 -6
  15. data/lib/pg_trunk/operations/domains/rename_domain.rb +1 -1
  16. data/lib/pg_trunk/operations/enums/create_enum.rb +2 -2
  17. data/lib/pg_trunk/operations/enums/drop_enum.rb +4 -4
  18. data/lib/pg_trunk/operations/enums/rename_enum.rb +1 -1
  19. data/lib/pg_trunk/operations/foreign_keys/add_foreign_key.rb +8 -8
  20. data/lib/pg_trunk/operations/foreign_keys/drop_foreign_key.rb +9 -9
  21. data/lib/pg_trunk/operations/foreign_keys/rename_foreign_key.rb +5 -5
  22. data/lib/pg_trunk/operations/functions/change_function.rb +1 -1
  23. data/lib/pg_trunk/operations/functions/create_function.rb +11 -11
  24. data/lib/pg_trunk/operations/functions/drop_function.rb +12 -12
  25. data/lib/pg_trunk/operations/functions/rename_function.rb +1 -1
  26. data/lib/pg_trunk/operations/materialized_views/change_materialized_view.rb +1 -1
  27. data/lib/pg_trunk/operations/materialized_views/create_materialized_view.rb +6 -6
  28. data/lib/pg_trunk/operations/materialized_views/drop_materialized_view.rb +7 -7
  29. data/lib/pg_trunk/operations/materialized_views/refresh_materialized_view.rb +2 -2
  30. data/lib/pg_trunk/operations/materialized_views/rename_materialized_view.rb +2 -2
  31. data/lib/pg_trunk/operations/procedures/change_procedure.rb +1 -1
  32. data/lib/pg_trunk/operations/procedures/create_procedure.rb +5 -5
  33. data/lib/pg_trunk/operations/procedures/drop_procedure.rb +5 -5
  34. data/lib/pg_trunk/operations/procedures/rename_procedure.rb +1 -1
  35. data/lib/pg_trunk/operations/rules/base.rb +77 -0
  36. data/lib/pg_trunk/operations/rules/create_rule.rb +155 -0
  37. data/lib/pg_trunk/operations/rules/drop_rule.rb +94 -0
  38. data/lib/pg_trunk/operations/rules/rename_rule.rb +62 -0
  39. data/lib/pg_trunk/operations/rules.rb +13 -0
  40. data/lib/pg_trunk/operations/statistics/create_statistics.rb +4 -4
  41. data/lib/pg_trunk/operations/statistics/drop_statistics.rb +5 -5
  42. data/lib/pg_trunk/operations/statistics/rename_statistics.rb +1 -1
  43. data/lib/pg_trunk/operations/triggers/change_trigger.rb +1 -1
  44. data/lib/pg_trunk/operations/triggers/create_trigger.rb +9 -9
  45. data/lib/pg_trunk/operations/triggers/drop_trigger.rb +9 -9
  46. data/lib/pg_trunk/operations/triggers/rename_trigger.rb +6 -6
  47. data/lib/pg_trunk/operations/views/change_view.rb +1 -1
  48. data/lib/pg_trunk/operations/views/create_view.rb +5 -5
  49. data/lib/pg_trunk/operations/views/drop_view.rb +7 -7
  50. data/lib/pg_trunk/operations/views/rename_view.rb +2 -2
  51. data/lib/pg_trunk/operations.rb +1 -0
  52. data/lib/pg_trunk/version.rb +1 -1
  53. data/pg_trunk.gemspec +0 -1
  54. data/spec/operations/rules/create_rule_spec.rb +119 -0
  55. data/spec/operations/rules/drop_rule_spec.rb +117 -0
  56. data/spec/operations/rules/rename_rule_spec.rb +148 -0
  57. metadata +12 -68
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe ActiveRecord::Migration, "#create_rule" do
4
+ before_all do
5
+ run_migration <<~RUBY
6
+ create_table :users do |t|
7
+ t.string :name
8
+ end
9
+
10
+ create_table :user_updates do |t|
11
+ t.timestamps
12
+ end
13
+ RUBY
14
+ end
15
+
16
+ context "with a minimal definition" do
17
+ let(:migration) do
18
+ <<~RUBY
19
+ create_rule "users", "prevent_insertion" do |r|
20
+ r.event :insert
21
+ r.kind :instead
22
+ r.comment "Prevent insertion to users"
23
+ end
24
+ RUBY
25
+ end
26
+
27
+ its(:execution) { is_expected.to insert(migration).into_schema }
28
+ its(:inversion) { is_expected.not_to change_schema }
29
+ end
30
+
31
+ context "with a commands definition" do
32
+ let(:migration) do
33
+ <<~RUBY
34
+ create_rule "users", "count_updates" do |r|
35
+ r.event :update
36
+ r.command <<~Q.chomp
37
+ INSERT INTO user_updates (created_at) VALUES (now())
38
+ Q
39
+ r.comment "Count updates of users"
40
+ end
41
+ RUBY
42
+ end
43
+
44
+ its(:execution) { is_expected.to insert(migration).into_schema }
45
+ its(:inversion) { is_expected.not_to change_schema }
46
+ end
47
+
48
+ context "without an explicit name of the rule" do
49
+ let(:migration) do
50
+ <<~RUBY
51
+ create_rule "users" do |r|
52
+ r.event :update
53
+ r.command <<~Q.chomp
54
+ INSERT INTO user_updates (created_at) VALUES (now())
55
+ Q
56
+ r.comment "Count updates of users"
57
+ end
58
+ RUBY
59
+ end
60
+
61
+ its(:execution) { is_expected.to insert(migration).into_schema }
62
+ its(:inversion) { is_expected.not_to change_schema }
63
+ end
64
+
65
+ context "with the `replace_existing: true` option" do
66
+ let(:migration) do
67
+ <<~RUBY
68
+ create_rule "users", replace_existing: true do |r|
69
+ r.event :update
70
+ r.command <<~Q.chomp
71
+ INSERT INTO user_updates (created_at) VALUES (now());
72
+ Q
73
+ r.comment "Count updates of users"
74
+ end
75
+ RUBY
76
+ end
77
+ let(:snippet) do
78
+ <<~RUBY
79
+ create_rule "users" do |r|
80
+ r.event :update
81
+ r.command <<~Q.chomp
82
+ INSERT INTO user_updates (created_at) VALUES (now())
83
+ Q
84
+ r.comment "Count updates of users"
85
+ end
86
+ RUBY
87
+ end
88
+
89
+ its(:execution) { is_expected.to insert(snippet).into_schema }
90
+ it { is_expected.to be_irreversible.because_of(/replace_existing: true/i) }
91
+ end
92
+
93
+ context "without an event" do
94
+ let(:migration) do
95
+ <<~RUBY
96
+ create_rule "users" do |r|
97
+ r.kind :instead
98
+ r.comment "Prevent insertion to users"
99
+ end
100
+ RUBY
101
+ end
102
+
103
+ it { is_expected.to fail_validation.because(/event can't be blank/i) }
104
+ end
105
+
106
+ context "without a table" do
107
+ let(:migration) do
108
+ <<~RUBY
109
+ create_rule do |r|
110
+ r.event :insert
111
+ r.kind :instead
112
+ r.comment "Prevent insertion to users"
113
+ end
114
+ RUBY
115
+ end
116
+
117
+ it { is_expected.to fail_validation.because(/table can't be blank/i) }
118
+ end
119
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe ActiveRecord::Migration, "#drop_rule" do
4
+ before_all do
5
+ run_migration <<~RUBY
6
+ create_table :users do |t|
7
+ t.string :name
8
+ end
9
+ RUBY
10
+ end
11
+ before { run_migration(snippet) }
12
+
13
+ context "when a rule was named" do
14
+ let(:snippet) do
15
+ <<~RUBY
16
+ create_rule "users", "prevent_insertion" do |r|
17
+ r.event :insert
18
+ r.kind :instead
19
+ r.comment "Prevent insertion to users"
20
+ end
21
+ RUBY
22
+ end
23
+
24
+ context "with a full definition" do
25
+ let(:migration) do
26
+ <<~RUBY
27
+ drop_rule "users", "prevent_insertion" do |r|
28
+ r.event :insert
29
+ r.kind :instead
30
+ r.comment "Prevent insertion to users"
31
+ end
32
+ RUBY
33
+ end
34
+
35
+ its(:execution) { is_expected.to remove(snippet).from_schema }
36
+ its(:inversion) { is_expected.not_to change_schema }
37
+ end
38
+
39
+ context "with a name only" do
40
+ let(:migration) do
41
+ <<~RUBY
42
+ drop_rule "users", "prevent_insertion"
43
+ RUBY
44
+ end
45
+
46
+ its(:execution) { is_expected.to remove(snippet).from_schema }
47
+ it { is_expected.to be_irreversible.because(/event can't be blank/i) }
48
+ end
49
+
50
+ context "with if_exists: true option" do
51
+ let(:migration) do
52
+ <<~RUBY
53
+ drop_rule "users", "prevent_insertion", if_exists: true do |r|
54
+ r.event :insert
55
+ r.kind :instead
56
+ r.comment "Prevent insertion to users"
57
+ end
58
+ RUBY
59
+ end
60
+
61
+ its(:execution) { is_expected.to remove(snippet).from_schema }
62
+ it { is_expected.to be_irreversible.because_of(/if_exists: true/i) }
63
+ end
64
+
65
+ context "with force: :cascade option" do
66
+ let(:migration) do
67
+ <<~RUBY
68
+ drop_rule "users", "prevent_insertion", force: :cascade do |r|
69
+ r.event :insert
70
+ r.kind :instead
71
+ r.comment "Prevent insertion to users"
72
+ end
73
+ RUBY
74
+ end
75
+
76
+ its(:execution) { is_expected.to remove(snippet).from_schema }
77
+ it { is_expected.to be_irreversible.because_of(/force: :cascade/i) }
78
+ end
79
+ end
80
+
81
+ context "when a rule was anonymous" do
82
+ let(:snippet) do
83
+ <<~RUBY
84
+ create_rule "users" do |r|
85
+ r.event :insert
86
+ r.kind :instead
87
+ r.comment "Prevent insertion to users"
88
+ end
89
+ RUBY
90
+ end
91
+
92
+ context "with a full definition" do
93
+ let(:migration) do
94
+ <<~RUBY
95
+ drop_rule "users" do |r|
96
+ r.event :insert
97
+ r.kind :instead
98
+ r.comment "Prevent insertion to users"
99
+ end
100
+ RUBY
101
+ end
102
+
103
+ its(:execution) { is_expected.to remove(snippet).from_schema }
104
+ its(:inversion) { is_expected.not_to change_schema }
105
+ end
106
+
107
+ context "with a table only" do
108
+ let(:migration) do
109
+ <<~RUBY
110
+ drop_rule "users"
111
+ RUBY
112
+ end
113
+
114
+ it { is_expected.to fail_validation.because(/name can't be blank/i) }
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe ActiveRecord::Migration, "#rename_rule" do
4
+ before_all do
5
+ run_migration <<~RUBY
6
+ create_table :users do |t|
7
+ t.string :name
8
+ end
9
+ RUBY
10
+ end
11
+ before { run_migration(old_snippet) }
12
+
13
+ context "when a rule was named" do
14
+ let(:old_snippet) do
15
+ <<~RUBY
16
+ create_rule "users", "prevent_insertion" do |r|
17
+ r.event :insert
18
+ r.kind :instead
19
+ r.comment "Prevent insertion to users"
20
+ end
21
+ RUBY
22
+ end
23
+
24
+ context "with a new name" do
25
+ let(:migration) do
26
+ <<~RUBY
27
+ rename_rule "users", "prevent_insertion", to: "do_nothing"
28
+ RUBY
29
+ end
30
+ let(:new_snippet) do
31
+ <<~RUBY
32
+ create_rule "users", "do_nothing" do |r|
33
+ r.event :insert
34
+ r.kind :instead
35
+ r.comment "Prevent insertion to users"
36
+ end
37
+ RUBY
38
+ end
39
+
40
+ its(:execution) { is_expected.to remove(old_snippet).from_schema }
41
+ its(:execution) { is_expected.to insert(new_snippet).into_schema }
42
+ its(:inversion) { is_expected.not_to change_schema }
43
+ end
44
+
45
+ context "with the same name" do
46
+ let(:migration) do
47
+ <<~RUBY
48
+ rename_rule "users", "prevent_insertion", to: "prevent_insertion"
49
+ RUBY
50
+ end
51
+
52
+ it { is_expected.to fail_validation.because(/new name must be different/i) }
53
+ end
54
+
55
+ context "without new name" do
56
+ let(:migration) do
57
+ <<~RUBY
58
+ rename_rule "users", "prevent_insertion" do |r|
59
+ r.event :insert
60
+ r.kind :instead
61
+ end
62
+ RUBY
63
+ end
64
+ let(:new_snippet) do
65
+ <<~RUBY
66
+ create_rule "users" do |r|
67
+ r.event :insert
68
+ r.kind :instead
69
+ r.comment "Prevent insertion to users"
70
+ end
71
+ RUBY
72
+ end
73
+
74
+ its(:execution) { is_expected.to remove(old_snippet).from_schema }
75
+ its(:execution) { is_expected.to insert(new_snippet).into_schema }
76
+ its(:inversion) { is_expected.not_to change_schema }
77
+ end
78
+
79
+ context "when absent name can't be generated from kind/event" do
80
+ let(:migration) do
81
+ <<~RUBY
82
+ rename_rule "users", "prevent_insertion"
83
+ RUBY
84
+ end
85
+
86
+ it { is_expected.to fail_validation.because(/new name can't be blank/i) }
87
+ end
88
+ end
89
+
90
+ context "when a rule was anonymous" do
91
+ let(:old_snippet) do
92
+ <<~RUBY
93
+ create_rule "users" do |r|
94
+ r.event :insert
95
+ r.kind :instead
96
+ r.comment "Prevent insertion to users"
97
+ end
98
+ RUBY
99
+ end
100
+
101
+ context "with a new name" do
102
+ let(:migration) do
103
+ <<~RUBY
104
+ rename_rule "users", to: "do_nothing" do |r|
105
+ r.event :insert
106
+ r.kind :instead
107
+ end
108
+ RUBY
109
+ end
110
+ let(:new_snippet) do
111
+ <<~RUBY
112
+ create_rule "users", "do_nothing" do |r|
113
+ r.event :insert
114
+ r.kind :instead
115
+ r.comment "Prevent insertion to users"
116
+ end
117
+ RUBY
118
+ end
119
+
120
+ its(:execution) { is_expected.to remove(old_snippet).from_schema }
121
+ its(:execution) { is_expected.to insert(new_snippet).into_schema }
122
+ its(:inversion) { is_expected.not_to change_schema }
123
+ end
124
+
125
+ context "without new name" do
126
+ let(:migration) do
127
+ <<~RUBY
128
+ rename_rule "users" do |r|
129
+ r.event :insert
130
+ r.kind :instead
131
+ end
132
+ RUBY
133
+ end
134
+
135
+ it { is_expected.to fail_validation.because(/new name must be different/i) }
136
+ end
137
+
138
+ context "when absent name can't be generated" do
139
+ let(:migration) do
140
+ <<~RUBY
141
+ rename_rule "users", to: "do_nothing"
142
+ RUBY
143
+ end
144
+
145
+ it { is_expected.to fail_validation.because(/name can't be blank/i) }
146
+ end
147
+ end
148
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_trunk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kozin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-16 00:00:00.000000000 Z
11
+ date: 2022-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -169,6 +169,11 @@ files:
169
169
  - lib/pg_trunk/operations/procedures/create_procedure.rb
170
170
  - lib/pg_trunk/operations/procedures/drop_procedure.rb
171
171
  - lib/pg_trunk/operations/procedures/rename_procedure.rb
172
+ - lib/pg_trunk/operations/rules.rb
173
+ - lib/pg_trunk/operations/rules/base.rb
174
+ - lib/pg_trunk/operations/rules/create_rule.rb
175
+ - lib/pg_trunk/operations/rules/drop_rule.rb
176
+ - lib/pg_trunk/operations/rules/rename_rule.rb
172
177
  - lib/pg_trunk/operations/statistics.rb
173
178
  - lib/pg_trunk/operations/statistics/base.rb
174
179
  - lib/pg_trunk/operations/statistics/create_statistics.rb
@@ -238,6 +243,9 @@ files:
238
243
  - spec/operations/procedures/create_procedure_spec.rb
239
244
  - spec/operations/procedures/drop_procedure_spec.rb
240
245
  - spec/operations/procedures/rename_procedure_spec.rb
246
+ - spec/operations/rules/create_rule_spec.rb
247
+ - spec/operations/rules/drop_rule_spec.rb
248
+ - spec/operations/rules/rename_rule_spec.rb
241
249
  - spec/operations/statistics/create_statistics_spec.rb
242
250
  - spec/operations/statistics/drop_statistics_spec.rb
243
251
  - spec/operations/statistics/rename_statistics_spec.rb
@@ -277,72 +285,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
277
285
  - !ruby/object:Gem::Version
278
286
  version: '0'
279
287
  requirements: []
280
- rubygems_version: 3.3.3
288
+ rubygems_version: 3.1.6
281
289
  signing_key:
282
290
  specification_version: 4
283
291
  summary: Empower PostgreSQL migrations in Rails app
284
- test_files:
285
- - spec/dummy/.gitignore
286
- - spec/dummy/Rakefile
287
- - spec/dummy/bin/bundle
288
- - spec/dummy/bin/rails
289
- - spec/dummy/bin/rake
290
- - spec/dummy/config.ru
291
- - spec/dummy/config/application.rb
292
- - spec/dummy/config/boot.rb
293
- - spec/dummy/config/database.yml
294
- - spec/dummy/config/environment.rb
295
- - spec/dummy/db/materialized_views/admin_users_v01.sql
296
- - spec/dummy/db/migrate/.keep
297
- - spec/dummy/db/schema.rb
298
- - spec/dummy/db/views/admin_users_v01.sql
299
- - spec/dummy/db/views/admin_users_v02.sql
300
- - spec/operations/check_constraints/add_check_constraint_spec.rb
301
- - spec/operations/check_constraints/drop_check_constraint_spec.rb
302
- - spec/operations/check_constraints/rename_check_constraint_spec.rb
303
- - spec/operations/composite_types/change_composite_type_spec.rb
304
- - spec/operations/composite_types/create_composite_type_spec.rb
305
- - spec/operations/composite_types/drop_composite_type_spec.rb
306
- - spec/operations/composite_types/rename_composite_type_spec.rb
307
- - spec/operations/dependency_resolver_spec.rb
308
- - spec/operations/domains/change_domain_spec.rb
309
- - spec/operations/domains/create_domain_spec.rb
310
- - spec/operations/domains/drop_domain_spec.rb
311
- - spec/operations/domains/rename_domain_spec.rb
312
- - spec/operations/enums/change_enum_spec.rb
313
- - spec/operations/enums/create_enum_spec.rb
314
- - spec/operations/enums/drop_enum_spec.rb
315
- - spec/operations/enums/rename_enum_spec.rb
316
- - spec/operations/foreign_keys/add_foreign_key_spec.rb
317
- - spec/operations/foreign_keys/drop_foreign_key_spec.rb
318
- - spec/operations/foreign_keys/rename_foreign_key_spec.rb
319
- - spec/operations/functions/change_function_spec.rb
320
- - spec/operations/functions/create_function_spec.rb
321
- - spec/operations/functions/drop_function_spec.rb
322
- - spec/operations/functions/rename_function_spec.rb
323
- - spec/operations/indexes/add_index_spec.rb
324
- - spec/operations/materialized_views/change_materialized_view_spec.rb
325
- - spec/operations/materialized_views/create_materialized_view_spec.rb
326
- - spec/operations/materialized_views/drop_materialized_view_spec.rb
327
- - spec/operations/materialized_views/refresh_materialized_view_spec.rb
328
- - spec/operations/materialized_views/rename_materialized_view_spec.rb
329
- - spec/operations/procedures/change_procedure_spec.rb
330
- - spec/operations/procedures/create_procedure_spec.rb
331
- - spec/operations/procedures/drop_procedure_spec.rb
332
- - spec/operations/procedures/rename_procedure_spec.rb
333
- - spec/operations/statistics/create_statistics_spec.rb
334
- - spec/operations/statistics/drop_statistics_spec.rb
335
- - spec/operations/statistics/rename_statistics_spec.rb
336
- - spec/operations/tables/create_table_spec.rb
337
- - spec/operations/tables/rename_table_spec.rb
338
- - spec/operations/triggers/change_trigger_spec.rb
339
- - spec/operations/triggers/create_trigger_spec.rb
340
- - spec/operations/triggers/drop_trigger_spec.rb
341
- - spec/operations/triggers/rename_trigger_spec.rb
342
- - spec/operations/views/change_view_spec.rb
343
- - spec/operations/views/create_view_spec.rb
344
- - spec/operations/views/drop_view_spec.rb
345
- - spec/operations/views/rename_view_spec.rb
346
- - spec/pg_trunk/dependencies_resolver_spec.rb
347
- - spec/spec_helper.rb
348
- - spec/support/migrations_helper.rb
292
+ test_files: []