Class: MysqlRewinder
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/mysql_rewinder.rb,
lib/mysql_rewinder/cleaner.rb,
lib/mysql_rewinder/version.rb,
lib/mysql_rewinder/ext/trilogy.rb,
lib/mysql_rewinder/cleaner/adapter.rb,
lib/mysql_rewinder/ext/mysql2_client.rb,
lib/mysql_rewinder/cleaner/mysql2_adapter.rb,
lib/mysql_rewinder/cleaner/trilogy_adapter.rb
Defined Under Namespace
Modules: Ext
Classes: Cleaner
Constant Summary
collapse
- VERSION =
"0.1.4"
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(db_configs:, except_tables:, adapter:, logger:) ⇒ MysqlRewinder
Returns a new instance of MysqlRewinder.
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/mysql_rewinder.rb', line 21
def initialize(db_configs:, except_tables:, adapter:, logger:)
@initialized_pid = Process.pid
@inserted_table_record_dir = Pathname(Dir.tmpdir)
@cleaners = db_configs.map do |db_config|
Cleaner.new(
db_config.transform_keys(&:to_sym),
except_tables: except_tables,
adapter: adapter,
logger: logger
)
end
@logger = logger
reset_inserted_tables
end
|
Class Method Details
.setup(db_configs, except_tables: [], adapter: :trilogy, logger: nil) ⇒ Object
16
17
18
|
# File 'lib/mysql_rewinder.rb', line 16
def setup(db_configs, except_tables: [], adapter: :trilogy, logger: nil)
@instance = new(db_configs: db_configs, except_tables: except_tables, adapter: adapter, logger: logger)
end
|
Instance Method Details
#calculate_inserted_tables ⇒ Object
65
66
67
68
69
70
71
72
73
|
# File 'lib/mysql_rewinder.rb', line 65
def calculate_inserted_tables
unless @initialized_pid == Process.pid
raise "MysqlRewinder is initialize in process #{@initialized_pid}, but calculate_inserted_tables is called in process #{Process.pid}"
end
Dir.glob(@inserted_table_record_dir.join("#{@initialized_pid}.*.inserted_tables").to_s).flat_map do |fname|
File.read(fname).strip.split(',')
end.uniq
end
|
#clean ⇒ Object
80
81
82
83
84
|
# File 'lib/mysql_rewinder.rb', line 80
def clean
aggregated_inserted_tables = calculate_inserted_tables
@cleaners.each { |c| c.clean(tables: aggregated_inserted_tables) }
reset_inserted_tables
end
|
#clean_all ⇒ Object
75
76
77
78
|
# File 'lib/mysql_rewinder.rb', line 75
def clean_all
@cleaners.each(&:clean_all)
reset_inserted_tables
end
|
#record_inserted_table(sql) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/mysql_rewinder.rb', line 36
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
table = match[1]
@inserted_tables << table if table
end
File.write(
@inserted_table_record_dir.join("#{@initialized_pid}.#{Process.pid}.inserted_tables").to_s,
@inserted_tables.to_a.join(',')
)
end
|
#reset_inserted_tables ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/mysql_rewinder.rb', line 53
def reset_inserted_tables
unless @initialized_pid == Process.pid
raise "MysqlRewinder is initialize in process #{@initialized_pid}, but reset_inserted_tables is called in process #{Process.pid}"
end
@inserted_tables = Set.new
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
|