pt-osc 0.0.2 → 0.0.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.
@@ -1,3 +1,7 @@
1
+ ## 0.0.3
2
+
3
+ - `defaults-file` flag can be specified as either an absolute path (has a leading `/`) or a relative path.
4
+
1
5
  ## 0.0.2
2
6
 
3
7
  - fixed bug when `percona` config is not defined in `database.yml`
data/README.md CHANGED
@@ -35,7 +35,8 @@ environment:
35
35
  recursion-method: "'dsn=D=percona,t=slaves'"
36
36
  ```
37
37
 
38
- Additional options for the `percona` hash include:
38
+ Additional/modified options for the `percona` hash include:
39
+ - `defaults-file`: Can be specified as an absolute path (with leading `/`) or relative (without). Relative paths will be treated as relative to your project's working directory.
39
40
  - `run_mode`: Specify `'execute'` to actually run `pt-online-schema-change` when the migration runs. Specify `'print'` to output the commands to run to STDOUT instead. Default is `'print'`.
40
41
 
41
42
  ## Caveats
@@ -1,12 +1,21 @@
1
1
  module ActiveRecord
2
2
  class PtOscMigration < Migration
3
3
  # @TODO whitelist all valid pt-osc flags
4
- DEFAULT_FLAGS = {
5
- 'defaults-file' => nil,
6
- 'recursion-method' => nil,
7
- 'execute' => false,
4
+ PERCONA_FLAGS = {
5
+ 'defaults-file' => {
6
+ mutator: :make_path_absolute,
7
+ },
8
+ 'recursion-method' => {
9
+ },
10
+ 'execute' => {
11
+ default: false,
12
+ },
8
13
  }.freeze
9
14
 
15
+ def self.percona_flags
16
+ PERCONA_FLAGS
17
+ end
18
+
10
19
  def migrate(direction)
11
20
  return unless respond_to?(direction)
12
21
 
@@ -113,25 +122,26 @@ module ActiveRecord
113
122
 
114
123
  # Whitelist
115
124
  options = HashWithIndifferentAccess.new(options)
116
- options = options.slice(*DEFAULT_FLAGS.keys)
125
+ options = options.slice(*self.class.percona_flags.keys)
117
126
 
118
127
  # Merge config
119
128
  config = percona_config
120
129
  if config
121
- config.slice(*DEFAULT_FLAGS.keys).each do |key, value|
130
+ config.slice(*self.class.percona_flags.keys).each do |key, value|
122
131
  options[key] ||= value
123
132
  end
124
133
  end
125
134
 
126
135
  # Set defaults
127
- DEFAULT_FLAGS.each do |key, value|
128
- options[key] ||= value unless value.nil?
136
+ self.class.percona_flags.each do |flag, flag_config|
137
+ options[flag] ||= flag_config[:default] if flag_config.key?(:default)
129
138
  end
130
139
 
131
140
  # Determine run mode
132
141
  command += options.delete(:execute) ? ' --execute' : ' --dry-run'
133
142
 
134
143
  options.each do |key, value|
144
+ value = send(self.class.percona_flags[key][:mutator], value) if self.class.percona_flags[key].try(:key?, :mutator)
135
145
  command += " --#{key} #{value}"
136
146
  end
137
147
 
@@ -146,5 +156,13 @@ module ActiveRecord
146
156
  def percona_config
147
157
  database_config[:percona] || {}
148
158
  end
159
+
160
+ private
161
+ # Flag mutators
162
+ def make_path_absolute(path)
163
+ return path if path[0] == '/'
164
+ # If path is not already absolute, treat it as relative to the app root
165
+ File.expand_path(path, Dir.getwd)
166
+ end
149
167
  end
150
168
  end
@@ -1,5 +1,5 @@
1
1
  module Pt
2
2
  module Osc
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
5
5
  end
@@ -13,16 +13,46 @@ class PtOscMigrationUnitTest < Test::Unit::TestCase
13
13
  @migration.instance_variable_set(:@connection, ActiveRecord::Base.connection)
14
14
  end
15
15
 
16
- should 'only include flags in DEFAULT_FLAGS' do
17
- flag = ActiveRecord::PtOscMigration::DEFAULT_FLAGS.first
16
+ should 'only include flags in PERCONA_FLAGS' do
17
+ flag = ActiveRecord::PtOscMigration.percona_flags.first
18
18
  begin
19
19
  flag = Faker::Lorem.words.join('-')
20
- end while flag.in? ActiveRecord::PtOscMigration::DEFAULT_FLAGS
20
+ end while flag.in? ActiveRecord::PtOscMigration.percona_flags
21
21
 
22
22
  command = @migration.send(:percona_command, '', '', '', flag => nil)
23
23
  assert_equal false, command.include?(flag), "Flag #{flag} was given but should not have been."
24
24
  end
25
25
 
26
+ context 'with flags having defaults' do
27
+ setup do
28
+ # add some dummy flags
29
+ dummy_flags = 3.times.inject({}) do |hash|
30
+ dummy_flag = Faker::Lorem.words.join('-')
31
+ hash[dummy_flag] = { default: Faker::Lorem.word }
32
+ hash
33
+ end
34
+
35
+ standard_flags = ActiveRecord::PtOscMigration.percona_flags
36
+ ActiveRecord::PtOscMigration.stubs(:percona_flags).returns(standard_flags.merge(dummy_flags))
37
+ end
38
+
39
+ teardown do
40
+ ActiveRecord::PtOscMigration.unstub(:percona_flags)
41
+ end
42
+
43
+ should 'set missing flags to default values' do
44
+ flags_with_defaults = ActiveRecord::PtOscMigration.percona_flags.select do |flag, config|
45
+ config.key?(:default) && flag != 'execute'
46
+ end
47
+
48
+ command = @migration.send(:percona_command, '', '', '')
49
+ flags_with_defaults.each do |flag, config|
50
+ assert command.include?("--#{flag} #{config[:default]}"),
51
+ "Default value #{config[:default]} for flag #{flag} was not present in command: #{command}"
52
+ end
53
+ end
54
+ end
55
+
26
56
  should 'perform a dry run if execute not specified' do
27
57
  command = @migration.send(:percona_command, '', '', '')
28
58
  assert command.include?('--dry-run')
@@ -33,6 +63,40 @@ class PtOscMigrationUnitTest < Test::Unit::TestCase
33
63
  assert_equal false, command.include?('--dry-run')
34
64
  assert command.include?('--execute')
35
65
  end
66
+
67
+ context 'given a defaults-file' do
68
+ setup do
69
+ @path = Faker::Lorem.words.join('/')
70
+ @options = { :'defaults-file' => @path }
71
+ end
72
+
73
+ should 'call #make_path_absolute' do
74
+ @migration.expects(:make_path_absolute).with(@path)
75
+ @migration.send(:percona_command, '', '', '', @options)
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ context '#make_path_absolute' do
82
+ context 'with an absolute path' do
83
+ setup do
84
+ @path = "/#{Faker::Lorem.words.join('/')}"
85
+ end
86
+
87
+ should 'return the path unmodified' do
88
+ assert_equal @path, @migration.send(:make_path_absolute, @path)
89
+ end
90
+ end
91
+
92
+ context 'with a relative path' do
93
+ setup do
94
+ @path = Faker::Lorem.words.join('/')
95
+ end
96
+
97
+ should 'return an absolute path' do
98
+ assert_equal '/', @migration.send(:make_path_absolute, @path)[0]
99
+ end
36
100
  end
37
101
  end
38
102
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pt-osc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -229,7 +229,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
229
229
  version: '0'
230
230
  segments:
231
231
  - 0
232
- hash: 109718066691379397
232
+ hash: 3199925404506680095
233
233
  required_rubygems_version: !ruby/object:Gem::Requirement
234
234
  none: false
235
235
  requirements:
@@ -238,7 +238,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
238
  version: '0'
239
239
  segments:
240
240
  - 0
241
- hash: 109718066691379397
241
+ hash: 3199925404506680095
242
242
  requirements: []
243
243
  rubyforge_project:
244
244
  rubygems_version: 1.8.23