Skip to content

Commit

Permalink
Merge pull request #9 from meri025/add-log
Browse files Browse the repository at this point in the history
Add Debug Logs
  • Loading branch information
genya0407 authored Jul 17, 2024
2 parents a6b8bf9 + 4829cce commit 3772833
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/mysql_rewinder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/mysql_rewinder/cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]}] Skip DELETE query because target_table is empty." }
return
end

log_and_execute("SET FOREIGN_KEY_CHECKS = 0;")
log_and_execute(target_tables.map { |table| "DELETE FROM #{table}" }.join(';'))
Expand All @@ -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
Expand Down

0 comments on commit 3772833

Please sign in to comment.