Class: Mysql2postgres::PostgresWriter

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

Direct Known Subclasses

PostgresFileWriter

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



7
8
9
# File 'lib/mysql2postgres/postgres_writer.rb', line 7

def destination
  @destination
end

#filenameObject (readonly)

Returns the value of attribute filename.



7
8
9
# File 'lib/mysql2postgres/postgres_writer.rb', line 7

def filename
  @filename
end

Instance Method Details

#column_description(column) ⇒ Object



9
10
11
# File 'lib/mysql2postgres/postgres_writer.rb', line 9

def column_description(column)
  "#{PG::Connection.quote_ident column[:name]} #{column_type_info column}"
end

#column_type(column) ⇒ Object



13
14
15
# File 'lib/mysql2postgres/postgres_writer.rb', line 13

def column_type(column)
  column_type_info(column).split.first
end

#column_type_info(column) ⇒ Object



17
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
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
# File 'lib/mysql2postgres/postgres_writer.rb', line 17

def column_type_info(column)
  return "integer DEFAULT nextval('#{column[:table_name]}_#{column[:name]}_seq'::regclass) NOT NULL" if column[:auto_increment]

  default = if column[:default]
              " DEFAULT #{column[:default].nil? ? 'NULL' : "'#{PG::Connection.escape column[:default]}'"}"
            end
  null = column[:null] ? '' : ' NOT NULL'
  type = case column[:type]
         # String types
         when 'char'
           default += '::char' if default
           "character(#{column[:length]})"
         when 'varchar'
           default += '::character varying' if default
           "character varying(#{column[:length]})"
         # Integer and numeric types
         when 'integer'
           default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_i}" if default
           'integer'
         when 'bigint'
           default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_i}" if default
           'bigint'
         when 'tinyint'
           default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_i}" if default
           'smallint'
         when 'boolean'
           default = " DEFAULT #{column[:default].to_i == 1 ? 'true' : 'false'}" if default
           'boolean'
         when 'float', 'float unsigned'
           default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_f}" if default
           'real'
         when 'decimal'
           default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default]}" if default
           "numeric(#{column[:length] || 10}, #{column[:decimals] || 0})"
         when 'double precision'
           default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default]}" if default
           'double precision'
         when 'datetime', 'datetime(6)'
           default = nil
           'timestamp without time zone'
         when 'date'
           default = nil
           'date'
         when 'timestamp'
           case column[:default]
           when 'CURRENT_TIMESTAMP'
             default = ' DEFAULT CURRENT_TIMESTAMP'
           when datetime_zero
             default = " DEFAULT '#{datetime_zero_fix}'"
           when datetime_zero(with_seconds: true) # rubocop: disable Style/MethodCallWithArgsParentheses
             default = " DEFAULT '#{datetime_zero_fix with_seconds: true}'"
           end
           'timestamp without time zone'
         when 'time'
           default = ' DEFAULT NOW()' if default
           'time without time zone'
         when 'blob', 'longblob', 'mediumblob', 'tinyblob', 'varbinary'
           'bytea'
         when 'text', 'tinytext', 'mediumtext', 'longtext'
           'text'
         when /^enum/
           default += '::character varying' if default
           enum = column[:type].gsub(/enum|\(|\)/, '')
           max_enum_size = enum.split(',').map { |check| check.size - 2 }.max
           "character varying(#{max_enum_size}) check( \"#{column[:name]}\" in (#{enum}))"
         when 'geometry', 'multipolygon'
           'geometry'
         else
           puts "Unknown #{column.inspect}"
           column[:type].inspect
           return ''
         end

  "#{type}#{default}#{null}"
end

#inloadObject



120
121
122
# File 'lib/mysql2postgres/postgres_writer.rb', line 120

def inload
  raise "Method 'inload' needs to be overridden..."
end

#process_row(table, row) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mysql2postgres/postgres_writer.rb', line 93

def process_row(table, row)
  table.columns.each_with_index do |column, index|
    row[index] = Time.at(row[index]).utc.strftime('%H:%M:%S') if column[:type] == 'time' && row[index]

    if row[index].is_a? Time
      row[index] = row[index].to_s.gsub datetime_zero, datetime_zero_fix
      row[index] = row[index].to_s.gsub datetime_zero(with_seconds: true), datetime_zero_fix(with_seconds: true)
    end

    if column_type(column) == 'boolean'
      row[index] = if row[index] == 1
                     't'
                   elsif row[index]&.zero?
                     'f'
                   else
                     row[index]
                   end
    end

    row[index] = string_data table, row, index, column if row[index].is_a? String

    row[index] = '\N' unless row[index]
  end
end

#truncate(_table) ⇒ Object



118
# File 'lib/mysql2postgres/postgres_writer.rb', line 118

def truncate(_table) end