alterity 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/alterity.rb +12 -1
- data/lib/alterity/version.rb +1 -1
- data/spec/alterity_spec.rb +18 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6283968b3d028f736da1517832ee5ebf8410aa4281aea4a2fa7af9e868a7ef89
|
4
|
+
data.tar.gz: c4e6ae366bea0f1065aa303824c0f64f7dc684f7566e600ede7ece16c4e58b60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
data/lib/alterity/version.rb
CHANGED
data/spec/alterity_spec.rb
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2021-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mysql2
|