Class: Mysql2psql::PostgresFileWriter
Instance Method Summary
collapse
#column_description, #column_type, #column_type_info, #process_row
Constructor Details
Returns a new instance of PostgresFileWriter.
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/mysql2psql/postgres_file_writer.rb', line 6
def initialize(file)
@f = File.open(file, "w+")
@f << <<-EOF
-- MySQL 2 PostgreSQL dump\n
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
EOF
end
|
Instance Method Details
#close ⇒ Object
137
138
139
|
# File 'lib/mysql2psql/postgres_file_writer.rb', line 137
def close
@f.close
end
|
#truncate(table) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/mysql2psql/postgres_file_writer.rb', line 18
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 << <<-EOF
-- TRUNCATE #{table.name};
TRUNCATE #{PGconn.quote_ident(table.name)} CASCADE;
EOF
if serial_key
@f << <<-EOF
SELECT pg_catalog.setval(pg_get_serial_sequence('#{table.name}', '#{serial_key}'), #{maxval}, true);
EOF
end
end
|
#write_constraints(table) ⇒ Object
112
113
114
115
116
|
# File 'lib/mysql2psql/postgres_file_writer.rb', line 112
def write_constraints(table)
table.foreign_keys.each do |key|
@f << "ALTER TABLE #{PGconn.quote_ident(table.name)} ADD FOREIGN KEY (#{PGconn.quote_ident(key[:column])}) REFERENCES #{PGconn.quote_ident(key[:ref_table])}(#{PGconn.quote_ident(key[:ref_column])});\n"
end
end
|
#write_contents(table, reader) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/mysql2psql/postgres_file_writer.rb', line 119
def write_contents(table, reader)
@f << <<-EOF
--
-- Data for Name: #{table.name}; Type: TABLE DATA; Schema: public
--
COPY "#{table.name}" (#{table.columns.map {|column| PGconn.quote_ident(column[:name])}.join(", ")}) FROM stdin;
EOF
reader.paginated_read(table, 1000) do |row, counter|
line = []
process_row(table, row)
@f << row.join("\t") + "\n"
end
@f << "\\.\n\n"
end
|
#write_indexes(table) ⇒ Object
109
110
|
# File 'lib/mysql2psql/postgres_file_writer.rb', line 109
def write_indexes(table)
end
|
#write_table(table) ⇒ Object
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/mysql2psql/postgres_file_writer.rb', line 41
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
if column[:primary_key]
primary_keys << column[:name]
end
" " + column_description(column)
end.join(",\n")
if serial_key
@f << <<-EOF
--
-- 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);
EOF
end
@f << <<-EOF
-- Table: #{table.name}
-- DROP TABLE #{table.name};
DROP TABLE IF EXISTS #{PGconn.quote_ident(table.name)} CASCADE;
CREATE TABLE #{PGconn.quote_ident(table.name)} (
EOF
@f << columns
if primary_index = table.indexes.find {|index| index[:primary]}
@f << ",\n CONSTRAINT #{table.name}_pkey PRIMARY KEY(#{primary_index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")})"
end
@f << <<-EOF
\n)
WITHOUT OIDS;
EOF
table.indexes.each do |index|
next if index[:primary]
unique = index[:unique] ? "UNIQUE " : nil
@f << <<-EOF
DROP INDEX IF EXISTS #{PGconn.quote_ident(index[:name])} CASCADE;
CREATE #{unique}INDEX #{PGconn.quote_ident(index[:name])} ON #{PGconn.quote_ident(table.name)} (#{index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")});
EOF
end
end
|