Class: Flydata::Mysql::TableDdl

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

Constant Summary collapse

VERSION0 =
0
VERSION1 =

the version where no .generated_ddl file was generated. Therefore, this version never shows up in anywhere.

1
VERSION2 =

the version which doesn't handle server side encoding support.

2
VERSION3 =

the version with server side encoding support, migrated from the previous versions. Format/functionality-wise, it's the same as Version 3.

3
VERSION4 =

the version with server side encoding support, generated by sync:generated_table_ddl command.

4
VERSION =

the version with server side encoding support, generated by the auto-generated CREATE TABLE event.

VERSION3

Class Method Summary collapse

Class Method Details

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



20
21
22
23
# File 'lib/flydata/mysql/table_ddl.rb', line 20

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



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
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/flydata/mysql/table_ddl.rb', line 39

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 = BinLogPosition.new(File.open(position_file){|f| f.read })
      original_binlog_file = context.current_binlog_file
      context.current_binlog_file = binlog_pos.file
    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