28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/flydata/source_mysql/table_ddl.rb', line 28
def self.migrate_to_v2(tables, mysql_opts, sync_fm, position_file, context)
database = mysql_opts[:database]
mysql_tabledefs = nil
original_binlog_file = nil
binlog_pos = nil
event_size = 0
tables.each do |table|
contents = sync_fm.load_generated_ddl([table])
version = contents.first.to_i
next if version >= V2_TARGET_VERSION
if mysql_tabledefs.nil?
FlydataCore::Mysql::CommandGenerator.each_mysql_tabledef(tables, mysql_opts) do |mysql_tabledef, error|
raise error if error
mysql_tabledefs ||= {}
mysql_tabledefs[mysql_tabledef.table_name] = mysql_tabledef
end
end
mysql_tabledef = mysql_tabledefs[table]
if binlog_pos.nil?
binlog_pos = FlydataCore::Mysql::BinlogPos.new(File.open(position_file){|f| f.read })
original_binlog_file = context.current_binlog_file
context.current_binlog_file = binlog_pos.filename
end
charset = mysql_tabledef.default_charset_mysql
column_event = nil
at_subquery = mysql_tabledef.column_def.select{|col, coldef| /CHARACTER SET/.match(coldef) }.collect{|col, coldef| CHANGE_COLUMN_SQL % [col, coldef]}.join(",")
unless at_subquery.empty?
column_query = ALTER_TABLE_SQL % [database, table, at_subquery]
column_event = QueryEvent.new(EVENT_TYPE, database, table,
binlog_pos.pos, event_size, column_query,
Time.now.to_i)
yield column_event
end
table_query = ALTER_TABLE_CHARSET_SQL % [database, table, charset]
table_event = QueryEvent.new(EVENT_TYPE, database, table, binlog_pos.pos,
event_size, table_query, Time.now.to_i)
yield table_event
$log.info "migrating table `#{table}` from version #{version} to version #{V2_TARGET_VERSION}. Table event #{table_event} Column event #{column_event}"
sync_fm.save_generated_ddl([table], V2_TARGET_VERSION.to_s)
end
context.current_binlog_file = original_binlog_file if binlog_pos
end
|