Skip to content

Commit f6f8cd8

Browse files
author
David Matson
committed
Add configuration option to toggle stderr redirection
1 parent 129792d commit f6f8cd8

File tree

7 files changed

+32
-12
lines changed

7 files changed

+32
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
tags
1212
departure_error.log
1313
.ruby-version
14+
.idea/

lib/departure/command.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ class Command
88
# @param command_line [String]
99
# @param error_log_path [String]
1010
# @param logger [#write_no_newline]
11-
def initialize(command_line, error_log_path, logger)
11+
def initialize(command_line, error_log_path, logger, redirect_stderr)
1212
@command_line = command_line
1313
@error_log_path = error_log_path
1414
@logger = logger
15+
@redirect_stderr = redirect_stderr
1516
end
1617

1718
# Executes the command returning its status. It also prints its stdout to
@@ -35,7 +36,7 @@ def run
3536

3637
private
3738

38-
attr_reader :command_line, :error_log_path, :logger, :status
39+
attr_reader :command_line, :error_log_path, :logger, :status, :redirect_stderr
3940

4041
# Runs the command in a separate process, capturing its stdout and
4142
# execution status
@@ -60,7 +61,11 @@ def run_in_process
6061
#
6162
# @return [String]
6263
def full_command
63-
"#{command_line} 2> #{error_log_path}"
64+
if redirect_stderr
65+
"#{command_line} 2> #{error_log_path}"
66+
else
67+
command_line
68+
end
6469
end
6570

6671
# Validates the status of the execution

lib/departure/configuration.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module Departure
22
class Configuration
3-
attr_accessor :tmp_path, :global_percona_args, :enabled_by_default
3+
attr_accessor :tmp_path, :global_percona_args, :enabled_by_default, :redirect_stderr
44

55
def initialize
66
@tmp_path = '.'.freeze
77
@error_log_filename = 'departure_error.log'.freeze
88
@global_percona_args = nil
99
@enabled_by_default = true
10+
@redirect_stderr = true
1011
end
1112

1213
def error_log_path

lib/departure/runner.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def initialize(logger, cli_generator, mysql_adapter, config = Departure.configur
1515
@cli_generator = cli_generator
1616
@mysql_adapter = mysql_adapter
1717
@error_log_path = config.error_log_path
18+
@redirect_stderr = config.redirect_stderr
1819
end
1920

2021
# Executes the passed sql statement using pt-online-schema-change for ALTER
@@ -44,12 +45,12 @@ def affected_rows
4445
# @param command_line [String]
4546
# @return [Boolean]
4647
def execute(command_line)
47-
Command.new(command_line, error_log_path, logger).run
48+
Command.new(command_line, error_log_path, logger, redirect_stderr).run
4849
end
4950

5051
private
5152

52-
attr_reader :logger, :cli_generator, :mysql_adapter, :error_log_path
53+
attr_reader :logger, :cli_generator, :mysql_adapter, :error_log_path, :redirect_stderr
5354

5455
# Checks whether the sql statement is an ALTER TABLE
5556
#

spec/departure/command_spec.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
Departure::Logger, write: true, say: true, write_no_newline: true
1010
)
1111
end
12+
let(:redirect_stderr) { true }
1213

13-
let(:runner) { described_class.new(command, error_log_path, logger) }
14+
let(:runner) { described_class.new(command, error_log_path, logger, redirect_stderr) }
1415

1516
let(:temp_file) do
1617
file = Tempfile.new('faked_stdout')
@@ -62,6 +63,16 @@
6263
expect(logger).to have_received(:write_no_newline).with('hello world\\ntodo roto')
6364
end
6465

66+
context 'when not redirecting stderr' do
67+
let(:expected_command) { command }
68+
let(:redirect_stderr) { false }
69+
70+
it 'executes the expected command' do
71+
runner.run
72+
expect(Open3).to have_received(:popen3).with(expected_command)
73+
end
74+
end
75+
6576
context 'on failure' do
6677
before do
6778
allow(Open3).to(

spec/departure/runner_spec.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
let(:config) do
1212
instance_double(
1313
Departure::Configuration,
14-
error_log_path: 'departure_error.log'
14+
error_log_path: 'departure_error.log',
15+
redirect_stderr: true,
1516
)
1617
end
1718

@@ -39,7 +40,7 @@
3940

4041
before do
4142
allow(Departure::Command)
42-
.to receive(:new).with(command_line, config.error_log_path, logger)
43+
.to receive(:new).with(command_line, config.error_log_path, logger, config.redirect_stderr)
4344
.and_return(cmd)
4445
end
4546

spec/integration_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class Comment < ActiveRecord::Base; end
170170
it 'runs pt-online-schema-change with the specified arguments' do
171171
expect(Departure::Command)
172172
.to receive(:new)
173-
.with(/--chunk-time=1/, anything, anything)
173+
.with(/--chunk-time=1/, anything, anything, anything)
174174
.and_return(command)
175175

176176
ClimateControl.modify PERCONA_ARGS: '--chunk-time=1' do
@@ -183,7 +183,7 @@ class Comment < ActiveRecord::Base; end
183183
it 'runs pt-online-schema-change with the specified arguments' do
184184
expect(Departure::Command)
185185
.to receive(:new)
186-
.with(/--chunk-time=1 --max-lag=2/, anything, anything)
186+
.with(/--chunk-time=1 --max-lag=2/, anything, anything, anything)
187187
.and_return(command)
188188

189189
ClimateControl.modify PERCONA_ARGS: '--chunk-time=1 --max-lag=2' do
@@ -196,7 +196,7 @@ class Comment < ActiveRecord::Base; end
196196
it 'runs pt-online-schema-change with the user specified value' do
197197
expect(Departure::Command)
198198
.to receive(:new)
199-
.with(/--alter-foreign-keys-method=drop_swap/, anything, anything)
199+
.with(/--alter-foreign-keys-method=drop_swap/, anything, anything, anything)
200200
.and_return(command)
201201

202202
ClimateControl.modify PERCONA_ARGS: '--alter-foreign-keys-method=drop_swap' do

0 commit comments

Comments
 (0)