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 << "-- MySQL 2 PostgreSQL dump\\n\nSET client_encoding = 'UTF8';\nSET standard_conforming_strings = off;\nSET check_function_bodies = false;\nSET client_min_messages = warning;\n \n"
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 << "-- TRUNCATE \#{table.name};\nTRUNCATE \#{PGconn.quote_ident(table.name)} CASCADE;\n\n"
if serial_key
@f << "SELECT pg_catalog.setval(pg_get_serial_sequence('\#{table.name}', '\#{serial_key}'), \#{maxval}, true);\n"
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 << "--\n-- Data for Name: \#{table.name}; Type: TABLE DATA; Schema: public\n--\n\nCOPY \"\#{table.name}\" (\#{table.columns.map {|column| PGconn.quote_ident(column[:name])}.join(\", \")}) FROM stdin;\n"
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 << "--\n-- Name: \#{table.name}_\#{serial_key}_seq; Type: SEQUENCE; Schema: public\n--\n \nDROP SEQUENCE IF EXISTS \#{table.name}_\#{serial_key}_seq CASCADE;\n \nCREATE SEQUENCE \#{table.name}_\#{serial_key}_seq\n INCREMENT BY 1\n NO MAXVALUE\n NO MINVALUE\n CACHE 1;\n \n \nSELECT pg_catalog.setval('\#{table.name}_\#{serial_key}_seq', \#{maxval}, true);\n \n EOF\n end\n \n @f << <<-EOF\n-- Table: \#{table.name}\n \n-- DROP TABLE \#{table.name};\nDROP TABLE IF EXISTS \#{PGconn.quote_ident(table.name)} CASCADE;\n \nCREATE TABLE \#{PGconn.quote_ident(table.name)} (\n"
@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 << "\\n)\nWITHOUT OIDS;\n"
table.indexes.each do |index|
next if index[:primary]
unique = index[:unique] ? "UNIQUE " : nil
@f << "DROP INDEX IF EXISTS \#{PGconn.quote_ident(index[:name])} CASCADE;\nCREATE \#{unique}INDEX \#{PGconn.quote_ident(index[:name])} ON \#{PGconn.quote_ident(table.name)} (\#{index[:columns].map {|col| PGconn.quote_ident(col)}.join(\", \")});\n"
end
end
|