Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude non-insert queies from clearing #14

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions lib/mysql_rewinder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was surprised. Set#merge is destructive!

File.write(
@inserted_table_record_dir.join("#{@initialized_pid}.#{Process.pid}.inserted_tables").to_s,
@inserted_tables.to_a.join(',')
Expand Down Expand Up @@ -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