Skip to content
Permalink
Browse files

Add --wait-timeout option

With a huge Gratanfile which has many users, `gratan apply` sometimes
would fail because of `Mysql2::Error::ConnectionError)`. More pricisely,
in a case where exporting grants takes a long time, a MySQL session
could be disconnected by wait-timeout option.

So I would like to add a new option to control wait-timeout as MySQL
session variable. I think there would be a case that controlling
wait-timeout by the session variable may be better rather than by
the global variable in a MySQL host.
  • Loading branch information...
itkq committed Apr 19, 2019
1 parent e72beed commit 8b131072d62e86566859bf159da634733dc30321
Showing with 35 additions and 27 deletions.
  1. +28 −27 bin/gratan
  2. +1 −0 lib/gratan/client.rb
  3. +6 −0 lib/gratan/driver.rb
@@ -35,33 +35,34 @@ options = {

ARGV.options do |opt|
begin
opt.on('' , '--host HOST') {|v| mysql_options[:host] = v }
opt.on('' , '--port PORT', Integer) {|v| mysql_options[:port] = v }
opt.on('' , '--socket SOCKET') {|v| mysql_options[:socket] = v }
opt.on('' , '--username USERNAME') {|v| mysql_options[:username] = v }
opt.on('' , '--password PASSWORD') {|v| mysql_options[:password] = v }
opt.on('' , '--database DATABASE') {|v| mysql_options[:database] = v }
opt.on('-a', '--apply') { mode = :apply }
opt.on('-f', '--file FILE') {|v| file = v }
opt.on('' , '--dry-run') { options[:dry_run] = true }
opt.on('-e', '--export') { mode = :export }
opt.on('' , '--with-identifier') { options[:with_identifier] = true }
opt.on('' , '--split') { split = true }
opt.on('' , '--chunk-by-user') { options[:chunk_by_user] = true }
opt.on('-o', '--output FILE') {|v| output_file = v }
opt.on('' , '--ignore-user REGEXP') {|v| options[:ignore_user] = Regexp.new(v) }
opt.on('' , '--target-user REGEXP') {|v| options[:target_user] = Regexp.new(v) }
opt.on('' , '--ignore-object REGEXP') {|v| options[:ignore_object] = Regexp.new(v) }
opt.on('' , '--enable-expired') { options[:enable_expired] = true }
opt.on('' , '--ignore-not-exist') {|v| options[:ignore_not_exist] = true }
opt.on('' , '--ignore-password-secret') {|v| options[:ignore_password_secret] = true }
opt.on('' , '--skip-disable-log-bin') { options[:skip_disable_log_bin] = true }
opt.on('' , '--override-sql-mode') { options[:override_sql_mode] = true }
opt.on('' , '--use-show-create-user') { options[:use_show_create_user] = true }
opt.on('' , '--no-color') { options[:color] = false }
opt.on('' , '--debug') { options[:debug] = true }
opt.on('' , '--auto-identify OUTPUT') {|v| options[:identifier] = Gratan::Identifier::Auto.new(v, options) }
opt.on('' , '--csv-identify CSV') {|v| options[:identifier] = Gratan::Identifier::CSV.new(v, options) }
opt.on('' , '--host HOST') {|v| mysql_options[:host] = v }
opt.on('' , '--port PORT', Integer) {|v| mysql_options[:port] = v }
opt.on('' , '--socket SOCKET') {|v| mysql_options[:socket] = v }
opt.on('' , '--username USERNAME') {|v| mysql_options[:username] = v }
opt.on('' , '--password PASSWORD') {|v| mysql_options[:password] = v }
opt.on('' , '--database DATABASE') {|v| mysql_options[:database] = v }
opt.on('-a', '--apply') { mode = :apply }
opt.on('-f', '--file FILE') {|v| file = v }
opt.on('' , '--dry-run') { options[:dry_run] = true }
opt.on('-e', '--export') { mode = :export }
opt.on('' , '--with-identifier') { options[:with_identifier] = true }
opt.on('' , '--split') { split = true }
opt.on('' , '--chunk-by-user') { options[:chunk_by_user] = true }
opt.on('-o', '--output FILE') {|v| output_file = v }
opt.on('' , '--ignore-user REGEXP') {|v| options[:ignore_user] = Regexp.new(v) }
opt.on('' , '--target-user REGEXP') {|v| options[:target_user] = Regexp.new(v) }
opt.on('' , '--ignore-object REGEXP') {|v| options[:ignore_object] = Regexp.new(v) }
opt.on('' , '--enable-expired') { options[:enable_expired] = true }
opt.on('' , '--ignore-not-exist') {|v| options[:ignore_not_exist] = true }
opt.on('' , '--ignore-password-secret') {|v| options[:ignore_password_secret] = true }
opt.on('' , '--skip-disable-log-bin') { options[:skip_disable_log_bin] = true }
opt.on('' , '--override-sql-mode') { options[:override_sql_mode] = true }
opt.on('' , '--use-show-create-user') { options[:use_show_create_user] = true }
opt.on('' , '--no-color') { options[:color] = false }
opt.on('' , '--debug') { options[:debug] = true }
opt.on('' , '--wait-timeout SECOND', Integer) {|v| options[:wait_timeout] = v }
opt.on('' , '--auto-identify OUTPUT') {|v| options[:identifier] = Gratan::Identifier::Auto.new(v, options) }
opt.on('' , '--csv-identify CSV') {|v| options[:identifier] = Gratan::Identifier::CSV.new(v, options) }

opt.on('' , '--mysql2-options JSON') do |json|
json = JSON.parse(json)
@@ -246,6 +246,7 @@ def in_progress
begin
@driver.disable_log_bin_local
@driver.override_sql_mode
@driver.set_wait_timeout
@updated = false
yield
updated = @updated
@@ -215,6 +215,12 @@ def override_sql_mode
end
end

def set_wait_timeout
if @options[:wait_timeout]
query("SET @@wait_timeout = #{@options[:wait_timeout]}")
end
end

private

def query(sql)

0 comments on commit 8b13107

Please sign in to comment.
You can’t perform that action at this time.