Class: Mysql2postgres::PostgresFileWriter

Inherits:
PostgresWriter show all
Defined in:
lib/mysql2postgres/postgres_file_writer.rb

Direct Known Subclasses

PostgresDbWriter

Instance Attribute Summary

Attributes inherited from PostgresWriter

#destination, #filename

Instance Method Summary collapse

Methods inherited from PostgresWriter

#column_description, #column_type, #column_type_info, #process_row

Constructor Details

#initialize(file, destination) ⇒ PostgresFileWriter

Returns a new instance of PostgresFileWriter.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mysql2postgres/postgres_file_writer.rb', line 8

def initialize(file, destination)
  super()

  @filename = file
  @destination = destination

  @f = File.open file, 'w+:UTF-8'
  @f << <<~SQL_HEADER
    -- MySQL 2 PostgreSQL dump\n
    SET client_encoding = 'UTF8';
    SET standard_conforming_strings = off;
    SET check_function_bodies = false;
    SET client_min_messages = warning;

  SQL_HEADER
end

Instance Method Details

#closeObject



143
144
145
# File 'lib/mysql2postgres/postgres_file_writer.rb', line 143

def close
  @f.close
end

#inloadObject



147
148
149
# File 'lib/mysql2postgres/postgres_file_writer.rb', line 147

def inload
  puts "\nSkip import to PostgreSQL DB. SQL file created successfully."
end

#truncate(table) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mysql2postgres/postgres_file_writer.rb', line 25

def truncate(table)
  serial_key = nil
  maxval = nil

  table.columns.map do |column|
    if column[:auto_increment]
      serial_key = column[:name]
      maxval = column[:maxval].to_i < 1 ? 1 : column[:maxval] + 1
    end
  end

  @f << <<~SQL_TRUNCATE
    -- TRUNCATE #{table.name};
    TRUNCATE #{PG::Connection.quote_ident table.name} CASCADE;

  SQL_TRUNCATE

  return unless serial_key

  @f << <<~SQL_SERIAL
    SELECT pg_catalog.setval(pg_get_serial_sequence('#{table.name}', '#{serial_key}'), #{maxval}, true);
  SQL_SERIAL
end

#write_constraints(table) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/mysql2postgres/postgres_file_writer.rb', line 117

def write_constraints(table)
  table.foreign_keys.each do |key|
    @f << "ALTER TABLE #{PG::Connection.quote_ident table.name} " \
          "ADD FOREIGN KEY (#{quoted_list key[:column]}) " \
          "REFERENCES #{PG::Connection.quote_ident key[:ref_table]}(#{quoted_list key[:ref_column]}) " \
          "ON UPDATE #{key[:on_update]} ON DELETE #{key[:on_delete]};\n"
  end
end

#write_contents(table, reader) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mysql2postgres/postgres_file_writer.rb', line 126

def write_contents(table, reader)
  @f << <<~SQL_COPY
    --
    -- Data for Name: #{table.name}; Type: TABLE DATA; Schema: public
    --

    COPY "#{table.name}" (#{quoted_list(table.columns.map { |m| m[:name] })}) FROM stdin;
  SQL_COPY

  reader.paginated_read table, 1000 do |row, _counter|
    process_row table, row
    @f << row.join("\t")
    @f << "\n"
  end
  @f << "\\.\n\n"
end

#write_indexes(_table) ⇒ Object



115
# File 'lib/mysql2postgres/postgres_file_writer.rb', line 115

def write_indexes(_table); end

#write_table(table) ⇒ Object



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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mysql2postgres/postgres_file_writer.rb', line 49

def write_table(table)
  primary_keys = []
  serial_key = nil
  maxval = nil

  columns = table.columns.map do |column|
    if column[:auto_increment]
      serial_key = column[:name]
      maxval = column[:maxval].to_i < 1 ? 1 : column[:maxval] + 1
    end
    primary_keys << column[:name] if column[:primary_key]
    "  #{column_description column}"
  end.join(",\n")

  if serial_key
    @f << <<~SQL_SEQUENCE
      --
      -- Name: #{table.name}_#{serial_key}_seq; Type: SEQUENCE; Schema: public
      --

      DROP SEQUENCE IF EXISTS #{table.name}_#{serial_key}_seq CASCADE;

      CREATE SEQUENCE #{table.name}_#{serial_key}_seq
          INCREMENT BY 1
          NO MAXVALUE
          NO MINVALUE
          CACHE 1;


      SELECT pg_catalog.setval('#{table.name}_#{serial_key}_seq', #{maxval}, true);

    SQL_SEQUENCE
  end

  @f << <<~SQL_TABLE
    -- Table: #{table.name}

    -- DROP TABLE #{table.name};
    DROP TABLE IF EXISTS #{PG::Connection.quote_ident table.name} CASCADE;

    CREATE TABLE #{PG::Connection.quote_ident table.name} (
  SQL_TABLE

  @f << columns

  if (primary_index = table.indexes.find { |index| index[:primary] })
    @f << ",\n  CONSTRAINT #{table.name}_pkey PRIMARY KEY(#{quoted_list primary_index[:columns]})"
  end

  @f << <<~SQL_OIDS
    \n)
    WITHOUT OIDS;
  SQL_OIDS

  table.indexes.each do |index|
    next if index[:primary]

    unique = index[:unique] ? 'UNIQUE ' : nil
    @f << <<~SQL_INDEX
      DROP INDEX IF EXISTS #{PG::Connection.quote_ident index[:name]} CASCADE;
      CREATE #{unique}INDEX #{PG::Connection.quote_ident index[:name]}
      ON #{PG::Connection.quote_ident table.name} (#{quoted_list index[:columns]});
    SQL_INDEX
  end
end