Class: Mysql2postgres::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql2postgres/converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader, writer, options) ⇒ Converter

Returns a new instance of Converter.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mysql2postgres/converter.rb', line 16

def initialize(reader, writer, options)
  @reader = reader
  @writer = writer
  @exclude_tables = options[:exclude_tables] || []
  @only_tables = options[:tables]
  @suppress_data = options[:suppress_data] || false
  @suppress_ddl = options[:suppress_ddl] || false
  @force_truncate = options[:force_truncate] || false
  @preserve_order = options[:preserve_order] || false
  @clear_schema = options[:clear_schema] || false
end

Instance Attribute Details

#clear_schemaObject (readonly)

Returns the value of attribute clear_schema.



5
6
7
# File 'lib/mysql2postgres/converter.rb', line 5

def clear_schema
  @clear_schema
end

#exclude_tablesObject (readonly)

Returns the value of attribute exclude_tables.



5
6
7
# File 'lib/mysql2postgres/converter.rb', line 5

def exclude_tables
  @exclude_tables
end

#force_truncateObject (readonly)

Returns the value of attribute force_truncate.



5
6
7
# File 'lib/mysql2postgres/converter.rb', line 5

def force_truncate
  @force_truncate
end

#only_tablesObject (readonly)

Returns the value of attribute only_tables.



5
6
7
# File 'lib/mysql2postgres/converter.rb', line 5

def only_tables
  @only_tables
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/mysql2postgres/converter.rb', line 5

def options
  @options
end

#preserve_orderObject (readonly)

Returns the value of attribute preserve_order.



5
6
7
# File 'lib/mysql2postgres/converter.rb', line 5

def preserve_order
  @preserve_order
end

#readerObject (readonly)

Returns the value of attribute reader.



5
6
7
# File 'lib/mysql2postgres/converter.rb', line 5

def reader
  @reader
end

#suppress_dataObject (readonly)

Returns the value of attribute suppress_data.



5
6
7
# File 'lib/mysql2postgres/converter.rb', line 5

def suppress_data
  @suppress_data
end

#suppress_ddlObject (readonly)

Returns the value of attribute suppress_ddl.



5
6
7
# File 'lib/mysql2postgres/converter.rb', line 5

def suppress_ddl
  @suppress_ddl
end

#writerObject (readonly)

Returns the value of attribute writer.



5
6
7
# File 'lib/mysql2postgres/converter.rb', line 5

def writer
  @writer
end

Instance Method Details

#convertObject



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

def convert
  tables = reader.tables
  tables.reject! { |table| exclude_tables.include? table.name }
  tables.select! { |table| only_tables ? only_tables.include?(table.name) : true }

  # preserve order only works, if only_tables are specified
  if preserve_order && only_tables
    reordered_tables = []

    only_tables.each do |only_table|
      idx = tables.index { |table| table.name == only_table }
      if idx.nil?
        warn "Specified source table '#{only_table}' does not exist, skiped by migration"
      else
        reordered_tables << tables[idx]
      end
    end

    tables = reordered_tables
  end

  unless suppress_ddl
    tables.each do |table|
      puts "Writing DDL for #{table.name}"
      writer.write_table table
    end
  end

  unless suppress_data
    if force_truncate && suppress_ddl
      tables.each do |table|
        puts "Truncate table #{table.name}"
        writer.truncate table
      end
    end

    tables.each do |table|
      puts "Writing data for #{table.name}"
      writer.write_contents table, reader
    end
  end

  puts 'Writing indices and constraints'
  unless suppress_ddl
    tables.each do |table|
      writer.write_indexes table
    end
  end

  unless suppress_ddl
    tables.each do |table|
      writer.write_constraints table
    end
  end

  writer.close
  writer.clear_schema if clear_schema
  writer.inload
  0
rescue StandardError => e
  warn "mysql2postgres: Conversion failed: #{e}"
  warn e
  warn e.backtrace[0, 3].join("\n")
  -1
end