alterity 1.1.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e235d55d20337f7ff9bf2b4eed178c3585c4ff682bda637a34339c507644438
4
- data.tar.gz: b41b2d051afdbb66a42f5ce00e2daff2b8e9b4f8935fa3c9f01e64ab0148d9e4
3
+ metadata.gz: bf1aafe98960046d356a7f82baa19dca7a2160ec58fa48a93da12ebd12794d16
4
+ data.tar.gz: 4793526689c37e33673aab4e0a8b570445fd78b369c0edbe382cf9e9aa078a97
5
5
  SHA512:
6
- metadata.gz: 749cd02d5f7b2b6feb3e5e3b63c52274f701946cc03fd067b51f5c4214d7a2c73fab365ca329323058d1fb7c09999d3c1c7a8608bcb9574faa406bc244ad08cd
7
- data.tar.gz: a0234815613fe77cf4db1950b8ef72de5c0def70b6abe45394c4efa1d5c7b1154d99454f81d9babcd8f00f58cbdf63edcd2f9cb181c22102e08cbaf7aa15691e
6
+ metadata.gz: 0dfad0d6f235e3fb8da9cc0074015298261532d9f1da903cc47d73fb776d13c6efe77c7847f59f4be2286292240a2473165b8e7b756c41a1935691b13e8db10a
7
+ data.tar.gz: b931b6805769dcc0ae2c9fbeddb25036e3aca3cb8bb1ad194a3b5265fb7ae9fc895f43223b3b5409e7367db69ff40f07dff5bc881bc294733e220f66abe9ddad
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/i } ||
16
+ updates.split(",").all? { |s| s =~ /^\s*add\s+constraint/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
@@ -24,6 +35,7 @@ class Alterity
24
35
 
25
36
  # hooks
26
37
  def before_running_migrations
38
+ require "open3"
27
39
  state.migrating = true
28
40
  set_database_config
29
41
  prepare_replicas_dsns_table
@@ -40,7 +52,21 @@ class Alterity
40
52
  alter_argument = %("#{updates.gsub('"', '\\"').gsub('`', '\\\`')}")
41
53
  prepared_command = config.command.call(altered_table, alter_argument).to_s.gsub(/\n/, "\\\n")
42
54
  puts "[Alterity] Will execute: #{prepared_command}"
43
- system(prepared_command) || raise("[Alterity] Command failed")
55
+ config.before_command&.call(prepared_command)
56
+
57
+ result_str = +""
58
+ exit_status = nil
59
+ Open3.popen2e(prepared_command) do |_stdin, stdout_and_stderr, wait_thr|
60
+ stdout_and_stderr.each do |line|
61
+ puts line
62
+ result_str << line
63
+ config.on_command_output&.call(line)
64
+ end
65
+ exit_status = wait_thr.value
66
+ config.after_command&.call(wait_thr.value.to_i)
67
+ end
68
+
69
+ raise("[Alterity] Command failed. Full output: #{result_str}") unless exit_status.success?
44
70
  end
45
71
 
46
72
  def set_database_config
@@ -4,7 +4,10 @@ class Alterity
4
4
  Configuration = Struct.new(
5
5
  :command,
6
6
  :host, :port, :database, :username, :password,
7
- :replicas_dsns_database, :replicas_dsns_table, :replicas_dsns
7
+ :replicas_dsns_database, :replicas_dsns_table, :replicas_dsns,
8
+ :before_command,
9
+ :on_command_output,
10
+ :after_command
8
11
  )
9
12
  CurrentState = Struct.new(:migrating, :disabled)
10
13
  cattr_accessor :state
@@ -36,10 +39,6 @@ class Alterity
36
39
  yield config
37
40
  end
38
41
 
39
- def command=(new_command)
40
- config.command = new_command
41
- end
42
-
43
42
  def disable
44
43
  state.disabled = true
45
44
  yield
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Alterity
4
- VERSION = "1.1.0"
4
+ VERSION = "1.4.0"
5
5
  end
@@ -10,7 +10,7 @@ RSpec.describe Alterity do
10
10
  ["CREATE UNIQUE INDEX `idx_users_on_col` ON `users` (col)", "`users`", "ADD UNIQUE INDEX `idx_users_on_col` (col)"],
11
11
  ["DROP INDEX `idx_users_on_col` ON `users`", "`users`", "DROP INDEX `idx_users_on_col`"],
12
12
  ["alter table users drop col", "users", "drop col"],
13
- [" ALTER TABLE\n users\n DROP col", "users", "DROP col"]
13
+ [" ALTER TABLE\n users\n DROP col", "users", "DROP col"],
14
14
  ].each do |(query, expected_table, expected_updates)|
15
15
  puts query.inspect
16
16
  expected_block = proc {}
@@ -29,7 +29,11 @@ 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`)",
36
+ "ALTER TABLE `installment_events` ADD CONSTRAINT `fk_rails_0cb5590091` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE",
33
37
  ].each do |query|
34
38
  expected_block = proc {}
35
39
  expect(expected_block).to receive(:call)
@@ -37,5 +41,19 @@ RSpec.describe Alterity do
37
41
  Alterity.process_sql_query(query, &expected_block)
38
42
  end
39
43
  end
44
+
45
+ it "raises an error if mixing FK change and other things" do
46
+ expected_block = proc {}
47
+ expect(expected_block).not_to receive(:call)
48
+ expect(Alterity).not_to receive(:execute_alter)
49
+ query = "ALTER TABLE `installment_events` ADD `col` VARCHAR(255), DROP FOREIGN KEY _fk_rails_0123456789"
50
+ expect do
51
+ Alterity.process_sql_query(query, &expected_block)
52
+ end.to raise_error(/FK/)
53
+ query = "ALTER TABLE `installment_events` ADD `col` VARCHAR(255), ADD CONSTRAINT `fk_rails_0cb5590091` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)"
54
+ expect do
55
+ Alterity.process_sql_query(query, &expected_block)
56
+ end.to raise_error(/FK/)
57
+ end
40
58
  end
41
59
  end
@@ -8,6 +8,7 @@ Alterity.configure do |config|
8
8
  system("echo '#{string}' > /tmp/custom_command_result.txt")
9
9
  system("echo '#{altered_table}' >> /tmp/custom_command_result.txt")
10
10
  system("echo '#{alter_argument}' >> /tmp/custom_command_result.txt")
11
+ "ls /"
11
12
  }
12
13
 
13
14
  config.replicas(
@@ -23,4 +24,16 @@ Alterity.configure do |config|
23
24
  "h=#{ENV['MYSQL_HOST']}"
24
25
  ]
25
26
  )
27
+
28
+ config.before_command = lambda do |command|
29
+ File.new("/tmp/before_command.txt", "w").syswrite(command)
30
+ end
31
+
32
+ config.on_command_output = lambda do |output|
33
+ File.new("/tmp/on_command_output.txt", "w+").syswrite(output)
34
+ end
35
+
36
+ config.after_command = lambda do |exit_status|
37
+ File.new("/tmp/after_command.txt", "w").syswrite(exit_status)
38
+ end
26
39
  end
@@ -15,6 +15,25 @@ puts result
15
15
  p result.chars.map(&:hex)
16
16
 
17
17
  if result != expected_result
18
- puts "=> mismatch"
18
+ puts "=> mismatched result"
19
+ exit(1)
20
+ end
21
+
22
+
23
+ result = File.read("/tmp/before_command.txt")
24
+ if result != "ls /"
25
+ puts "=> mismatched before_command"
26
+ exit(1)
27
+ end
28
+
29
+ result = File.read("/tmp/on_command_output.txt")
30
+ if !result.include?("var")
31
+ puts "=> mismatched on_command_output"
32
+ exit(1)
33
+ end
34
+
35
+ result = File.read("/tmp/after_command.txt")
36
+ if result.strip != "0"
37
+ puts "=> mismatched after_command"
19
38
  exit(1)
20
39
  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.4.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-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2