Class: Flydata::SourceMysql::TableDdl

Inherits:
Object
  • Object
show all
Defined in:
lib/flydata/source_mysql/table_ddl.rb

Class Method Summary collapse

Class Method Details

.migrate_tables(tables, mysql_opts, sync_fm, position_file, context, &block) ⇒ Object



9
10
11
12
# File 'lib/flydata/source_mysql/table_ddl.rb', line 9

def self.migrate_tables(tables, mysql_opts, sync_fm, position_file, context,
                        &block)
  migrate_to_v2(tables, mysql_opts, sync_fm, position_file, context, &block)
end

.migrate_to_v2(tables, mysql_opts, sync_fm, position_file, context) ⇒ Object



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 # so that the next binlog position becomes the master binlog position
  tables.each do |table|
    # check the current ddl version
    contents = sync_fm.load_generated_ddl([table])
    version = contents.first.to_i
    # return if no need to migrate
    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?
      # get binlog position
      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
    # get charset
    charset = mysql_tabledef.default_charset_mysql
    # construct queries
    # column charset
    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 charset
    table_query = ALTER_TABLE_CHARSET_SQL % [database, table, charset]
    # create events and yield
    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}"

    # update generated_ddl
    sync_fm.save_generated_ddl([table], V2_TARGET_VERSION.to_s)
  end
  context.current_binlog_file = original_binlog_file if binlog_pos
end