percona_migrator 0.1.0.rc.2 → 0.1.0.rc.3

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
  SHA1:
3
- metadata.gz: 37474e0d339fdbbf66627f96d152780234101e1b
4
- data.tar.gz: 399919fee87faf206d71f650e0bc60ff45773d63
3
+ metadata.gz: 7fe6a75f0b0d56a2d50aab13a8a633347e6aaa3a
4
+ data.tar.gz: 961dccefa1160b2da61286331390161f6a5a0f65
5
5
  SHA512:
6
- metadata.gz: d7cbf7b595b676a62582ccf7442aecaadc18ed3378779bd8c0db981b63b8f49a244392be30907c68a2c8a7d8ff93e8ab524f6ad1f12c97155bfaf44eb895225f
7
- data.tar.gz: 6add69682f7e4cdea9a3cd92b5693413bf54d58302a69600929bc6da7065cb414123bf2c6b4938e93428c049272b93d1034598e2b58de32f6340b7a18d3cf086
6
+ metadata.gz: 123bdaea1db477acf87c5fbd4f6a48a8a9466efa21fb993f70fc701323d651ebd87a26c595dedebd9b71b01bb8a06fdd8631e146d0c34c2b1a5fa6e6e643bf18
7
+ data.tar.gz: 2b84763f317b1f980dc0604bbf44417034b95ae5331ae5bb0636deee98dabdc8f956c9b6090b45922fae15dc27d39d1f327fc2946842a7d4cc8f56ee0c281605
@@ -5,9 +5,19 @@ This project adheres to [Semantic Versioning](http://semver.org/).
5
5
  Please follow the format in [Keep a Changelog](http://keepachangelog.com/)
6
6
 
7
7
  ## [Unreleased]
8
+
9
+ ## [0.1.0.rc.3] - 2016-03-10
10
+
11
+ ### Added
12
+
13
+ - Support #execute. Allows to execute raw SQL from the migration
14
+
15
+ ## [0.1.0.rc.2] - 2016-03-09
16
+
8
17
  ### Added
9
18
 
10
19
  - VERBOSE env var in tests. Specially useful for integration tests.
20
+ - Fix #create_table migration method. Now it does create the table.
11
21
 
12
22
  ### Changed
13
23
 
@@ -130,6 +130,20 @@ module ActiveRecord
130
130
  true
131
131
  end
132
132
 
133
+ # Executes the passed statement through pt-online-schema-change if it's
134
+ # an alter statement, or through the mysql adapter otherwise
135
+ #
136
+ # @param sql [String]
137
+ # @param name [String]
138
+ def percona_execute(sql, name)
139
+ if alter_statement?(sql)
140
+ command = cli_generator.parse_statement(sql)
141
+ runner.execute(command)
142
+ else
143
+ mysql_adapter.execute(sql, name)
144
+ end
145
+ end
146
+
133
147
  # This abstract method leaves up to the connection adapter freeing the
134
148
  # result, if it needs to. Check out: https://github.com/rails/rails/blob/330c6af05c8b188eb072afa56c07d5fe15767c3c/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L247
135
149
  #
@@ -142,6 +156,14 @@ module ActiveRecord
142
156
  private
143
157
 
144
158
  attr_reader :mysql_adapter, :logger, :runner, :cli_generator
159
+
160
+ # Checks whether the sql statement is an ALTER TABLE
161
+ #
162
+ # @param sql [String]
163
+ # @return [Boolean]
164
+ def alter_statement?(sql)
165
+ sql =~ /alter table/i
166
+ end
145
167
  end
146
168
  end
147
169
  end
@@ -3,6 +3,7 @@ module PerconaMigrator
3
3
  # Represents the '--alter' argument of Percona's pt-online-schema-change
4
4
  # See https://www.percona.com/doc/percona-toolkit/2.0/pt-online-schema-change.html
5
5
  class AlterArgument
6
+ ALTER_TABLE_REGEX = /ALTER TABLE `(\w+)` /
6
7
 
7
8
  # Constructor
8
9
  #
@@ -17,6 +18,13 @@ module PerconaMigrator
17
18
  "--alter \"#{parsed_statement}\""
18
19
  end
19
20
 
21
+ # Returns the name of the table the alter statement refers to
22
+ #
23
+ # @return [String]
24
+ def table_name
25
+ statement.match(ALTER_TABLE_REGEX).captures[0]
26
+ end
27
+
20
28
  private
21
29
 
22
30
  attr_reader :statement
@@ -26,7 +34,7 @@ module PerconaMigrator
26
34
  # @return [String]
27
35
  def parsed_statement
28
36
  @parsed_statement ||= statement
29
- .gsub(/ALTER TABLE `(\w+)` /, '')
37
+ .gsub(ALTER_TABLE_REGEX, '')
30
38
  .gsub('`','\\\`')
31
39
  end
32
40
  end
@@ -56,8 +56,23 @@ module PerconaMigrator
56
56
  # @param statement [String] MySQL statement
57
57
  # @return [String]
58
58
  def generate(table_name, statement)
59
+ alter_argument = AlterArgument.new(statement)
59
60
  dsn = DSN.new(database, table_name)
61
+
62
+ "#{to_s} #{dsn} #{alter_argument}"
63
+ end
64
+
65
+ # Generates the percona command for a raw MySQL statement. Fills all the
66
+ # connection credentials from the current AR connection, but that can
67
+ # amended via ENV-vars: PERCONA_DB_HOST, PERCONA_DB_USER,
68
+ # PERCONA_DB_PASSWORD, PERCONA_DB_NAME Table name can't not be amended, it
69
+ # populates automatically from the migration data
70
+ #
71
+ # @param statement [String] MySQL statement
72
+ # @return [String]
73
+ def parse_statement(statement)
60
74
  alter_argument = AlterArgument.new(statement)
75
+ dsn = DSN.new(database, alter_argument.table_name)
61
76
 
62
77
  "#{to_s} #{dsn} #{alter_argument}"
63
78
  end
@@ -79,6 +94,8 @@ module PerconaMigrator
79
94
  end
80
95
 
81
96
  # Returns the command as a string that can be executed in a shell
97
+ #
98
+ # @return [String]
82
99
  def to_s
83
100
  @command.join(' ')
84
101
  end
@@ -37,6 +37,18 @@ module PerconaMigrator
37
37
  connection_config.merge(adapter: 'percona')
38
38
  )
39
39
  end
40
+
41
+ # It executes the passed statement through the PerconaAdapter if it's
42
+ # an alter statement. It uses the mysql adapter otherwise.
43
+ #
44
+ # This is because +pt-online-schema-change+ is intended for alter
45
+ # statements only.
46
+ #
47
+ # @param sql [String]
48
+ # @param name [String] optional
49
+ def execute(sql, name = nil)
50
+ percona_execute(sql, name)
51
+ end
40
52
  end
41
53
  end
42
54
  end
@@ -1,3 +1,3 @@
1
1
  module PerconaMigrator
2
- VERSION = '0.1.0.rc.2'.freeze
2
+ VERSION = '0.1.0.rc.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: percona_migrator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.rc.2
4
+ version: 0.1.0.rc.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Zayats
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-03-09 00:00:00.000000000 Z
13
+ date: 2016-03-10 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails