diff --git a/lib/mysql_rewinder.rb b/lib/mysql_rewinder.rb index b5f61c8..e71b0dd 100644 --- a/lib/mysql_rewinder.rb +++ b/lib/mysql_rewinder.rb @@ -37,13 +37,11 @@ def record_inserted_table(sql) return unless @initialized_pid @inserted_tables ||= Set.new - sql.split(';').each do |statement| - match = statement.match(/\A\s*INSERT(?:\s+IGNORE)?(?:\s+INTO)?\s+(?:\.*[`"]?([^.\s`"(]+)[`"]?)*/i) - next unless match + new_inserted_tables = extract_inserted_tables(sql) - table = match[1] - @inserted_tables << table if table - end + return if new_inserted_tables.empty? + + @inserted_tables.merge(new_inserted_tables) File.write( @inserted_table_record_dir.join("#{@initialized_pid}.#{Process.pid}.inserted_tables").to_s, @inserted_tables.to_a.join(',') @@ -82,4 +80,19 @@ def clean @cleaners.each { |c| c.clean(tables: aggregated_inserted_tables) } reset_inserted_tables end + + private + + def extract_inserted_tables(sql) + insert_tables = Set.new + sql.split(';').each do |statement| + match = statement.match(/\A\s*INSERT(?:\s+IGNORE)?(?:\s+INTO)?\s+(?:\.*[`"]?([^.\s`"(]+)[`"]?)*/i) + next unless match + + table = match[1] + insert_tables << table if table + end + + insert_tables + end end