Class: Xmysql2psql::PostgresFileWriter
Instance Method Summary
collapse
#column_description, #column_type, #column_type_info, #escape_bytea, #process_row
Constructor Details
Returns a new instance of PostgresFileWriter.
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/xmysql2psql/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
141
142
143
|
# File 'lib/xmysql2psql/postgres_file_writer.rb', line 141
def close
@f.close
end
|
#db_writer? ⇒ Boolean
18
19
20
|
# File 'lib/xmysql2psql/postgres_file_writer.rb', line 18
def db_writer?
false
end
|
#truncate(table) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/xmysql2psql/postgres_file_writer.rb', line 22
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
116
117
118
119
120
|
# File 'lib/xmysql2psql/postgres_file_writer.rb', line 116
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/xmysql2psql/postgres_file_writer.rb', line 123
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
113
114
|
# File 'lib/xmysql2psql/postgres_file_writer.rb', line 113
def write_indexes(table)
end
|
#write_table(table) ⇒ Object
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
108
109
110
111
|
# File 'lib/xmysql2psql/postgres_file_writer.rb', line 45
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
|