Class: Mysql2psql::Converter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader, writer, options) ⇒ Converter

Returns a new instance of Converter.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mysql2psql/converter.rb', line 7

def initialize(reader, writer, options)
  @reader = reader
  @writer = writer
  @options = options
  @exclude_tables = options.exclude_tables([])
  @only_tables = options.only_tables(nil)
  @suppress_data = options.suppress_data(false)
  @suppress_ddl = options.suppress_ddl(false)
  @supress_sequence_update = options.supress_sequence_update(false)
  @suppress_indexes = options.suppress_indexes(false)
  @force_truncate = options.force_truncate(false)
  @use_timezones = options.use_timezones(false)
end

Instance Attribute Details

#exclude_tablesObject (readonly)

Returns the value of attribute exclude_tables.



5
6
7
# File 'lib/mysql2psql/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/mysql2psql/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/mysql2psql/converter.rb', line 5

def only_tables
  @only_tables
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/mysql2psql/converter.rb', line 4

def options
  @options
end

#readerObject (readonly)

Returns the value of attribute reader.



4
5
6
# File 'lib/mysql2psql/converter.rb', line 4

def reader
  @reader
end

#suppress_dataObject (readonly)

Returns the value of attribute suppress_data.



5
6
7
# File 'lib/mysql2psql/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/mysql2psql/converter.rb', line 5

def suppress_ddl
  @suppress_ddl
end

#suppress_indexesObject (readonly)

Returns the value of attribute suppress_indexes.



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

def suppress_indexes
  @suppress_indexes
end

#supress_sequence_updateObject (readonly)

Returns the value of attribute supress_sequence_update.



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

def supress_sequence_update
  @supress_sequence_update
end

#writerObject (readonly)

Returns the value of attribute writer.



4
5
6
# File 'lib/mysql2psql/converter.rb', line 4

def writer
  @writer
end

Instance Method Details

#convertObject



21
22
23
24
25
26
27
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
# File 'lib/mysql2psql/converter.rb', line 21

def convert
  _time1 = Time.now

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

  tables.each do |table|
    writer.write_sequence_update(table, options)
  end if !(@suppress_sequence_update && @suppress_ddl)

  tables.each do |table|
    writer.write_table(table, {:use_timezones => @use_timezones})
  end unless @suppress_ddl
 
  _time2 = Time.now
  tables.each do |table|
    writer.truncate(table) if force_truncate && !suppress_ddl
    writer.write_contents(table, reader)
  end unless @suppress_data

  _time3 = Time.now
  tables.each do |table|
    writer.write_indexes(table) unless @suppress_indexes
  end unless @suppress_ddl
  tables.each do |table|
    writer.write_constraints(table)
  end unless @suppress_ddl


  writer.close
  _time4 = Time.now
  puts "Table creation #{((_time2 - _time1) / 60).round} min, loading #{((_time3 - _time2) / 60).round} min, indexing #{((_time4 - _time3) / 60).round} min, total #{((_time4 - _time1) / 60).round} min"
  return 0
rescue => e
  $stderr.puts "Mysql2psql: conversion failed: #{e.to_s}"
  $stderr.puts e.backtrace
  return -1
end