alterity 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e235d55d20337f7ff9bf2b4eed178c3585c4ff682bda637a34339c507644438
4
- data.tar.gz: b41b2d051afdbb66a42f5ce00e2daff2b8e9b4f8935fa3c9f01e64ab0148d9e4
3
+ metadata.gz: 6283968b3d028f736da1517832ee5ebf8410aa4281aea4a2fa7af9e868a7ef89
4
+ data.tar.gz: c4e6ae366bea0f1065aa303824c0f64f7dc684f7566e600ede7ece16c4e58b60
5
5
  SHA512:
6
- metadata.gz: 749cd02d5f7b2b6feb3e5e3b63c52274f701946cc03fd067b51f5c4214d7a2c73fab365ca329323058d1fb7c09999d3c1c7a8608bcb9574faa406bc244ad08cd
7
- data.tar.gz: a0234815613fe77cf4db1950b8ef72de5c0def70b6abe45394c4efa1d5c7b1154d99454f81d9babcd8f00f58cbdf63edcd2f9cb181c22102e08cbaf7aa15691e
6
+ metadata.gz: 1bf501f5c7f2ce204f995341c7ee2bf73a0c3d8fc16ed9b32ea710ff4b77251e464891df9866a6847b7740ae9472f1c639cae7b7ac31bb82381616b84d5ea66b
7
+ data.tar.gz: 58fb3f6708f0b610d4129c960f1765e5073d74e0170f46456d549cf3f65ecf8e34da7b139efee3afbb5575d0f0c01d5630170590d51ffba2b6de9ef8d52e4a1c
data/lib/alterity.rb CHANGED
@@ -10,7 +10,18 @@ class Alterity
10
10
  def process_sql_query(sql, &block)
11
11
  case sql.tr("\n", " ").strip
12
12
  when /^alter\s+table\s+(?<table>.+?)\s+(?<updates>.+)/i
13
- execute_alter($~[:table], $~[:updates])
13
+ table = $~[:table]
14
+ updates = $~[:updates]
15
+ if updates.split(",").all? { |s| s =~ /^\s*drop\s+foreign\s+key\s+\w+\s*$/i } ||
16
+ updates.split(",").all? { |s| s =~ /^\s*add\s+constraint\s+`?\w+`?\s+foreign\s+key\s+\(`?\w+`?\)\s+references\s+`?\w+`?\s+\(`?\w+`?\)\s*$/i }
17
+ block.call
18
+ elsif updates =~ /drop\s+foreign\s+key/i || updates =~ /add\s+constraint/i
19
+ # ADD CONSTRAINT / DROP FOREIGN KEY have to go to the original table,
20
+ # other alterations need to got to the new table.
21
+ raise "[Alterity] Can't change a FK and do something else in the same query. Split it."
22
+ else
23
+ execute_alter(table, updates)
24
+ end
14
25
  when /^create\s+index\s+(?<index>.+?)\s+on\s+(?<table>.+?)\s+(?<updates>.+)/i
15
26
  execute_alter($~[:table], "ADD INDEX #{$~[:index]} #{$~[:updates]}")
16
27
  when /^create\s+unique\s+index\s+(?<index>.+?)\s+on\s+(?<table>.+?)\s+(?<updates>.+)/i
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Alterity
4
- VERSION = "1.1.0"
4
+ VERSION = "1.2.0"
5
5
  end
@@ -29,7 +29,10 @@ RSpec.describe Alterity do
29
29
  "SHOW CREATE TABLE `users`",
30
30
  "SHOW TABLE STATUS LIKE `users`",
31
31
  "SHOW KEYS FROM `users`",
32
- "SHOW FULL FIELDS FROM `users`"
32
+ "SHOW FULL FIELDS FROM `users`",
33
+ "ALTER TABLE `installment_events` DROP FOREIGN KEY _fk_rails_0123456789",
34
+ "ALTER TABLE `installment_events` DROP FOREIGN KEY _fk_rails_0123456789, DROP FOREIGN KEY _fk_rails_9876543210",
35
+ "ALTER TABLE `installment_events` ADD CONSTRAINT `fk_rails_0cb5590091` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)"
33
36
  ].each do |query|
34
37
  expected_block = proc {}
35
38
  expect(expected_block).to receive(:call)
@@ -37,5 +40,19 @@ RSpec.describe Alterity do
37
40
  Alterity.process_sql_query(query, &expected_block)
38
41
  end
39
42
  end
43
+
44
+ it "raises an error if mixing FK change and other things" do
45
+ expected_block = proc {}
46
+ expect(expected_block).not_to receive(:call)
47
+ expect(Alterity).not_to receive(:execute_alter)
48
+ query = "ALTER TABLE `installment_events` ADD `col` VARCHAR(255), DROP FOREIGN KEY _fk_rails_0123456789"
49
+ expect do
50
+ Alterity.process_sql_query(query, &expected_block)
51
+ end.to raise_error(/FK/)
52
+ query = "ALTER TABLE `installment_events` ADD `col` VARCHAR(255), ADD CONSTRAINT `fk_rails_0cb5590091` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)"
53
+ expect do
54
+ Alterity.process_sql_query(query, &expected_block)
55
+ end.to raise_error(/FK/)
56
+ end
40
57
  end
41
58
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alterity
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Maximin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-06 00:00:00.000000000 Z
11
+ date: 2021-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2