From afe528dcc16a84ddd6f615c2acd6efb7adec523f Mon Sep 17 00:00:00 2001 From: meri025 Date: Wed, 17 Jul 2024 02:10:29 +0900 Subject: [PATCH 1/3] add log --- lib/mysql_rewinder.rb | 5 ++++- lib/mysql_rewinder/cleaner.rb | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/mysql_rewinder.rb b/lib/mysql_rewinder.rb index ae9196c..4e86a71 100644 --- a/lib/mysql_rewinder.rb +++ b/lib/mysql_rewinder.rb @@ -56,7 +56,10 @@ def reset_inserted_tables end @inserted_tables = Set.new - FileUtils.rm(Dir.glob(@inserted_table_record_dir.join("#{@initialized_pid}.*.inserted_tables").to_s)) + files = Dir.glob(@inserted_table_record_dir.join("#{@initialized_pid}.*.inserted_tables").to_s) + + FileUtils.rm(files) + @logger&.debug "[MysqlRewinder] removed files: #{files.join(', ')}" if files.any? end def calculate_inserted_tables diff --git a/lib/mysql_rewinder/cleaner.rb b/lib/mysql_rewinder/cleaner.rb index e9a449a..fde0025 100644 --- a/lib/mysql_rewinder/cleaner.rb +++ b/lib/mysql_rewinder/cleaner.rb @@ -19,7 +19,11 @@ def clean_all def clean(tables:) target_tables = (tables - @except_tables) & all_tables - return if target_tables.empty? + + if target_tables.empty? + @logger&.debug "[MysqlRewinder][#{@db_config[:database]}] the table to be deleted is empty." + return + end log_and_execute("SET FOREIGN_KEY_CHECKS = 0;") log_and_execute(target_tables.map { |table| "DELETE FROM #{table}" }.join(';')) @@ -42,7 +46,7 @@ def log_and_execute(sql) res = @client.execute(sql) duration = (Time.now - start_ts) * 1000 - name = "[MysqlRewinder] Cleaner SQL (#{duration.round(1)}ms)" + name = "[MysqlRewinder][#{@db_config[:database]}] Cleaner SQL (#{duration.round(1)}ms)" msg = "\e[1m\e[30m#{name}\e[0m \e[34m#{sql}\e[0m" @logger.debug msg res From 76333a579ddf983bdd146cf3f0dfaa8deadcf3b2 Mon Sep 17 00:00:00 2001 From: meri025 Date: Wed, 17 Jul 2024 13:33:06 +0900 Subject: [PATCH 2/3] Fix for optimization Co-authored-by: Yusuke Sangenya --- lib/mysql_rewinder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mysql_rewinder.rb b/lib/mysql_rewinder.rb index 4e86a71..b5f61c8 100644 --- a/lib/mysql_rewinder.rb +++ b/lib/mysql_rewinder.rb @@ -59,7 +59,7 @@ def reset_inserted_tables files = Dir.glob(@inserted_table_record_dir.join("#{@initialized_pid}.*.inserted_tables").to_s) FileUtils.rm(files) - @logger&.debug "[MysqlRewinder] removed files: #{files.join(', ')}" if files.any? + @logger&.debug { "[MysqlRewinder] removed files: #{files.join(', ')}" } if files.any? end def calculate_inserted_tables From 4829ccec3cf27f8021d218e9372ba5608285f919 Mon Sep 17 00:00:00 2001 From: meri025 Date: Wed, 17 Jul 2024 13:33:44 +0900 Subject: [PATCH 3/3] update skip DELETE query message Co-authored-by: Yusuke Sangenya --- lib/mysql_rewinder/cleaner.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mysql_rewinder/cleaner.rb b/lib/mysql_rewinder/cleaner.rb index fde0025..aec37a1 100644 --- a/lib/mysql_rewinder/cleaner.rb +++ b/lib/mysql_rewinder/cleaner.rb @@ -21,7 +21,7 @@ def clean(tables:) target_tables = (tables - @except_tables) & all_tables if target_tables.empty? - @logger&.debug "[MysqlRewinder][#{@db_config[:database]}] the table to be deleted is empty." + @logger&.debug { "[MysqlRewinder][#{@db_config[:database]}] Skip DELETE query because target_table is empty." } return end