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
# 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)
  @supress_data = options.supress_data(false)
  @supress_ddl = options.supress_ddl(false)
  @force_truncate = options.force_truncate(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

#supress_dataObject (readonly)

Returns the value of attribute supress_data.



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

def supress_data
  @supress_data
end

#supress_ddlObject (readonly)

Returns the value of attribute supress_ddl.



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

def supress_ddl
  @supress_ddl
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



18
19
20
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
# File 'lib/mysql2psql/converter.rb', line 18

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_table(table)
  end unless @supress_ddl
 
  _time2 = Time.now
  tables.each do |table|
    writer.truncate(table) if force_truncate && supress_ddl
    writer.write_contents(table, reader)
  end unless @supress_data
 
  _time3 = Time.now
  tables.each do |table|
    writer.write_indexes(table)
  end unless @supress_ddl
  tables.each do |table|
    writer.write_constraints(table)
  end unless @supress_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}"
  return -1
end