· 8 years ago · Jul 14, 2018, 03:42 PM
1mysql2psql(main):181:2> end
2mysql2psql(main):182:1>
3mysql2psql(main):183:1* def column_type(column)
4mysql2psql(main):184:2> column_type_info(column).split(" ").first
5mysql2psql(main):185:2> end
6mysql2psql(main):186:1>
7mysql2psql(main):187:1* def column_type_info(column)
8mysql2psql(main):188:2> if column[:primary_key] && column[:name] == "id"
9mysql2psql(main):189:3> return "integer DEFAULT nextval('#{column[:table_name]}_#{column[:name]}_seq'::regclass) NOT NULL"
10mysql2psql(main):190:3> end
11mysql2psql(main):191:2>
12mysql2psql(main):192:2* default = column[:default] ? " DEFAULT #{column[:default] == nil ? 'NULL' : "'"+PGconn.escape(column[:default])+"'"}" : nil
13mysql2psql(main):193:2> null = column[:null] ? "" : " NOT NULL"
14mysql2psql(main):194:2> type =
15mysql2psql(main):195:2* case column[:type]
16mysql2psql(main):196:3> when "varchar"
17mysql2psql(main):197:3> default = default + "::character varying" if default
18mysql2psql(main):198:3> # puts "VARCHAR: #{column.inspect}"
19mysql2psql(main):199:3* "character varying(#{column[:length]})"
20mysql2psql(main):200:3> when "integer"
21mysql2psql(main):201:3> default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_i}" if default
22mysql2psql(main):202:3> "integer"
23mysql2psql(main):203:3> when "tinyint"
24mysql2psql(main):204:3> default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_i}" if default
25mysql2psql(main):205:3> "smallint"
26mysql2psql(main):206:3> when "datetime"
27mysql2psql(main):207:3> default = nil
28mysql2psql(main):208:3> "timestamp without time zone"
29mysql2psql(main):209:3> when "date"
30mysql2psql(main):210:3> default = nil
31mysql2psql(main):211:3> "date"
32mysql2psql(main):212:3> when "boolean"
33mysql2psql(main):213:3> default = " DEFAULT #{column[:default].to_i == 1 ? 'true' : 'false'}" if default
34mysql2psql(main):214:3> "boolean"
35mysql2psql(main):215:3> when "blob"
36mysql2psql(main):216:3> "bytea"
37mysql2psql(main):217:3> when "mediumtext"
38mysql2psql(main):218:3> "text"
39mysql2psql(main):219:3> when "longtext"
40mysql2psql(main):220:3> "text"
41mysql2psql(main):221:3> when "text"
42mysql2psql(main):222:3> "text"
43mysql2psql(main):223:3> when "double"
44mysql2psql(main):224:3> default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default]}" if default
45mysql2psql(main):225:3> "double precision"
46mysql2psql(main):226:3> when /^enum/
47mysql2psql(main):227:3> default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default]}" if default
48mysql2psql(main):228:3> enum = column[:type].gsub(/enum|\(|\)/, '')
49mysql2psql(main):229:3> max_enum_size = enum.split(',').map{ |check| check.size() -2}.sort[-1]
50mysql2psql(main):230:3> "character varying(#{max_enum_size}), check( #{column[:name]} in (#{enum}))"
51mysql2psql(main):231:3> when "float"
52mysql2psql(main):232:3> default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_f}" if default
53mysql2psql(main):233:3> "real"
54mysql2psql(main):234:3> when "decimal"
55mysql2psql(main):235:3> default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default]}" if default
56mysql2psql(main):236:3> "numeric(#{column[:length] || 10}, #{column[:decimals] || 0})"
57mysql2psql(main):237:3> when "timestamp"
58mysql2psql(main):238:3> default = " DEFAULT CURRENT_TIMESTAMP" if column[:default] == "CURRENT_TIMESTAMP"
59mysql2psql(main):239:3> "timestamp without time zone"
60mysql2psql(main):240:3> when "time"
61mysql2psql(main):241:3> default = " DEFAULT now" if default
62mysql2psql(main):242:3> "time without time zone"
63mysql2psql(main):243:3> else
64mysql2psql(main):244:3* puts "Unknown #{column.inspect}"
65mysql2psql(main):245:3> column[:type].inspect
66mysql2psql(main):246:3> return ""
67mysql2psql(main):247:3> end
68mysql2psql(main):248:2> "#{type}#{default}#{null}"
69mysql2psql(main):249:2> end
70mysql2psql(main):250:1>
71mysql2psql(main):251:1* end
72=> nil
73mysql2psql(main):252:0>
74mysql2psql(main):253:0* class PostgresFileWriter < PostgresWriter
75mysql2psql(main):254:1> def initialize(file)
76mysql2psql(main):255:2> @f = File.open(file, "w+")
77mysql2psql(main):256:2> @f << <<-EOF
78mysql2psql(main):257:2" -- MySQL 2 PostgreSQL dump\n
79mysql2psql(main):258:2" SET client_encoding = 'UTF8';
80mysql2psql(main):259:2" SET standard_conforming_strings = off;
81mysql2psql(main):260:2" SET check_function_bodies = false;
82mysql2psql(main):261:2" SET client_min_messages = warning;
83mysql2psql(main):262:2"
84mysql2psql(main):263:2" EOF
85mysql2psql(main):264:2> end
86mysql2psql(main):265:1>
87mysql2psql(main):266:1* def write_table(table)
88mysql2psql(main):267:2> primary_keys = []
89mysql2psql(main):268:2> primary_key = nil
90mysql2psql(main):269:2> maxval = nil
91mysql2psql(main):270:2>
92mysql2psql(main):271:2* columns = table.columns.map do |column|
93mysql2psql(main):272:3* if column[:primary_key]
94mysql2psql(main):273:4> if column[:name] == "id"
95mysql2psql(main):274:5> primary_key = column[:name]
96mysql2psql(main):275:5> maxval = column[:maxval] < 1 ? 1 : column[:maxval] + 1
97mysql2psql(main):276:5> end
98mysql2psql(main):277:4> primary_keys << column[:name]
99mysql2psql(main):278:4> end
100mysql2psql(main):279:3> " " + column_description(column)
101mysql2psql(main):280:3> end.join(",\n")
102mysql2psql(main):281:2>
103mysql2psql(main):282:2* if primary_key
104mysql2psql(main):283:3>
105mysql2psql(main):284:3* @f << <<-EOF
106mysql2psql(main):285:3" --
107mysql2psql(main):286:3" -- Name: #{table.name}_#{primary_key}_seq; Type: SEQUENCE; Schema: public
108mysql2psql(main):287:3" --
109mysql2psql(main):288:3"
110mysql2psql(main):289:3" DROP SEQUENCE IF EXISTS #{table.name}_#{primary_key}_seq CASCADE;
111mysql2psql(main):290:3"
112mysql2psql(main):291:3" CREATE SEQUENCE #{table.name}_#{primary_key}_seq
113mysql2psql(main):292:3" INCREMENT BY 1
114mysql2psql(main):293:3" NO MAXVALUE
115mysql2psql(main):294:3" NO MINVALUE
116mysql2psql(main):295:3" CACHE 1;
117mysql2psql(main):296:3"
118mysql2psql(main):297:3"
119mysql2psql(main):298:3" SELECT pg_catalog.setval('#{table.name}_#{primary_key}_seq', #{maxval}, true);
120mysql2psql(main):299:3"
121mysql2psql(main):300:3" EOF
122mysql2psql(main):301:3> end
123mysql2psql(main):302:2>
124mysql2psql(main):303:2* @f << <<-EOF
125mysql2psql(main):304:2" -- Table: #{table.name}
126mysql2psql(main):305:2"
127mysql2psql(main):306:2" -- DROP TABLE #{table.name};
128mysql2psql(main):307:2" DROP TABLE IF EXISTS #{PGconn.quote_ident(table.name)} CASCADE;
129mysql2psql(main):308:2"
130mysql2psql(main):309:2" CREATE TABLE #{PGconn.quote_ident(table.name)} (
131mysql2psql(main):310:2" EOF
132mysql2psql(main):311:2>
133mysql2psql(main):312:2* @f << columns
134mysql2psql(main):313:2>
135mysql2psql(main):314:2* if primary_index = table.indexes.find {|index| index[:primary]}
136mysql2psql(main):315:3> @f << ",\n CONSTRAINT #{table.name}_pkey PRIMARY KEY(#{primary_index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")})"
137mysql2psql(main):316:3> end
138mysql2psql(main):317:2>
139mysql2psql(main):318:2* @f << <<-EOF
140mysql2psql(main):319:2" \n)
141mysql2psql(main):320:2" WITHOUT OIDS;
142mysql2psql(main):321:2" EOF
143mysql2psql(main):322:2>
144mysql2psql(main):323:2* table.indexes.each do |index|
145mysql2psql(main):324:3* next if index[:primary]
146mysql2psql(main):325:3> unique = index[:unique] ? "UNIQUE " : nil
147mysql2psql(main):326:3> @f << <<-EOF
148mysql2psql(main):327:3" DROP INDEX IF EXISTS #{PGconn.quote_ident(index[:name])} CASCADE;
149mysql2psql(main):328:3" CREATE #{unique}INDEX #{PGconn.quote_ident(index[:name])} ON #{PGconn.quote_ident(table.name)} (#{index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")});
150mysql2psql(main):329:3" EOF
151mysql2psql(main):330:3> end
152mysql2psql(main):331:2>
153mysql2psql(main):332:2* end
154mysql2psql(main):333:1>
155mysql2psql(main):334:1* def write_indexes(table)
156mysql2psql(main):335:2> end
157mysql2psql(main):336:1>
158mysql2psql(main):337:1* def write_constraints(table)
159mysql2psql(main):338:2> table.foreign_keys.each do |key|
160mysql2psql(main):339:3* @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"
161mysql2psql(main):340:3> end
162mysql2psql(main):341:2> end
163mysql2psql(main):342:1>
164mysql2psql(main):343:1*
165mysql2psql(main):344:1* def write_contents(table, reader)
166mysql2psql(main):345:2> @f << <<-EOF
167mysql2psql(main):346:2" --
168mysql2psql(main):347:2" -- Data for Name: #{table.name}; Type: TABLE DATA; Schema: public
169mysql2psql(main):348:2" --
170mysql2psql(main):349:2"
171mysql2psql(main):350:2" COPY "#{table.name}" (#{table.columns.map {|column| PGconn.quote_ident(column[:name])}.join(", ")}) FROM stdin;
172mysql2psql(main):351:2" EOF
173mysql2psql(main):352:2>
174mysql2psql(main):353:2* reader.paginated_read(table, 1000) do |row, counter|
175mysql2psql(main):354:3* line = []
176mysql2psql(main):355:3> table.columns.each_with_index do |column, index|
177mysql2psql(main):356:4* row[index] = row[index].to_s if row[index].is_a?(Mysql::Time)
178mysql2psql(main):357:4> if column[:type] == "char"
179mysql2psql(main):358:5> row[index] = row[index] == 1 ? 't' : row[index] == 0 ? 'f' : row[index]
180mysql2psql(main):359:5> end
181mysql2psql(main):360:4> if row[index].is_a?(String)
182mysql2psql(main):361:5> if column[:type] == "bytea"
183mysql2psql(main):362:6> row[index] = PGconn.quote(row[index])
184mysql2psql(main):363:6> else
185mysql2psql(main):364:6* row[index] = row[index].gsub(/\\/, '\\\\\\').gsub(/\n/,'\n').gsub(/\t/,'\t').gsub(/\r/,'\r')
186mysql2psql(main):365:6> end
187mysql2psql(main):366:5> end
188mysql2psql(main):367:4> row[index] = '\N' if !row[index]
189mysql2psql(main):368:4> end
190mysql2psql(main):369:3> @f << row.join("\t") + "\n"
191mysql2psql(main):370:3> end
192mysql2psql(main):371:2> @f << "\\.\n\n"
193mysql2psql(main):372:2> @f << "VACUUM FULL ANALYZE #{PGconn.quote_ident(table.name)};\n\n"
194mysql2psql(main):373:2> end
195mysql2psql(main):374:1>
196mysql2psql(main):375:1* def close
197mysql2psql(main):376:2> @f.close
198mysql2psql(main):377:2> end
199mysql2psql(main):378:1> end
200=> nil
201mysql2psql(main):379:0>
202mysql2psql(main):380:0* class PostgresDbWriter < PostgresWriter
203mysql2psql(main):381:1> def connection(hostname, login, password, database, port)
204mysql2psql(main):382:2> database, schema = database.split(":")
205mysql2psql(main):383:2> @conn = PGconn.open('host' => hostname, 'user' => login, 'password' => password, 'dbname' => database, 'port' => port.to_s)
206mysql2psql(main):384:2> @conn.exec("SET search_path TO #{PGconn.quote_ident(schema)}") if schema
207mysql2psql(main):385:2> end
208mysql2psql(main):386:1>
209mysql2psql(main):387:1* def initialize(hostname, login, password, database, port = 5432)
210mysql2psql(main):388:2> connection(hostname, login, password, database, port)
211mysql2psql(main):389:2> @conn.exec("SET client_encoding = 'UTF8'")
212mysql2psql(main):390:2> @conn.exec("SET standard_conforming_strings = off") if @conn.server_version >= 80200
213mysql2psql(main):391:2> @conn.exec("SET check_function_bodies = false")
214mysql2psql(main):392:2> @conn.exec("SET client_min_messages = warning")
215mysql2psql(main):393:2> end
216mysql2psql(main):394:1>
217mysql2psql(main):395:1* def exists?(relname)
218mysql2psql(main):396:2> rc = @conn.select_one("SELECT COUNT(*) FROM pg_class WHERE relname = #{PGconn.quote(relname)}")
219mysql2psql(main):397:2> (!rc.nil?) && (!rc.empty?) && (rc.first.to_i > 0)
220mysql2psql(main):398:2> end
221mysql2psql(main):399:1>
222mysql2psql(main):400:1* def write_table(table)
223mysql2psql(main):401:2> primary_keys = []
224mysql2psql(main):402:2> primary_key = nil
225mysql2psql(main):403:2> maxval = nil
226mysql2psql(main):404:2>
227mysql2psql(main):405:2* columns = table.columns.map do |column|
228mysql2psql(main):406:3* if column[:primary_key]
229mysql2psql(main):407:4> if column[:name] == "id"
230mysql2psql(main):408:5> primary_key = column[:name]
231mysql2psql(main):409:5> maxval = column[:maxval] < 1 ? 1 : column[:maxval] + 1
232mysql2psql(main):410:5> end
233mysql2psql(main):411:4> primary_keys << column[:name]
234mysql2psql(main):412:4> end
235mysql2psql(main):413:3> " " + column_description(column)
236mysql2psql(main):414:3> end.join(",\n")
237mysql2psql(main):415:2>
238mysql2psql(main):416:2* if primary_key
239mysql2psql(main):417:3> if @conn.server_version < 80200
240mysql2psql(main):418:4> primary_key_seq = "#{table.name}_#{primary_key}_seq"
241mysql2psql(main):419:4> @conn.exec("DROP SEQUENCE #{primary_key_seq} CASCADE") if exists?(primary_key_seq)
242mysql2psql(main):420:4> else
243mysql2psql(main):421:4* @conn.exec("DROP SEQUENCE IF EXISTS #{table.name}_#{primary_key}_seq CASCADE")
244mysql2psql(main):422:4> end
245mysql2psql(main):423:3> @conn.exec <<-EOF
246mysql2psql(main):424:3" CREATE SEQUENCE #{table.name}_#{primary_key}_seq
247mysql2psql(main):425:3" INCREMENT BY 1
248mysql2psql(main):426:3" NO MAXVALUE
249mysql2psql(main):427:3" NO MINVALUE
250mysql2psql(main):428:3" CACHE 1
251mysql2psql(main):429:3" EOF
252mysql2psql(main):430:3>
253mysql2psql(main):431:3* @conn.exec "SELECT pg_catalog.setval('#{table.name}_#{primary_key}_seq', #{maxval}, true)"
254mysql2psql(main):432:3> end
255mysql2psql(main):433:2>
256mysql2psql(main):434:2* if @conn.server_version < 80200
257mysql2psql(main):435:3> @conn.exec "DROP TABLE #{PGconn.quote_ident(table.name)} CASCADE;" if exists?(table.name)
258mysql2psql(main):436:3> else
259mysql2psql(main):437:3* @conn.exec "DROP TABLE IF EXISTS #{PGconn.quote_ident(table.name)} CASCADE;"
260mysql2psql(main):438:3> end
261mysql2psql(main):439:2> create_sql = "CREATE TABLE #{PGconn.quote_ident(table.name)} (\n" + columns + "\n)\nWITHOUT OIDS;"
262mysql2psql(main):440:2> begin
263mysql2psql(main):441:3* @conn.exec(create_sql)
264mysql2psql(main):442:3> rescue Exception => e
265mysql2psql(main):443:3> puts "Error: \n#{create_sql}"
266mysql2psql(main):444:3> raise
267mysql2psql(main):445:3> end
268mysql2psql(main):446:2> puts "Created table #{table.name}"
269mysql2psql(main):447:2>
270mysql2psql(main):448:2* end
271mysql2psql(main):449:1>
272mysql2psql(main):450:1* def write_indexes(table)
273mysql2psql(main):451:2> if primary_index = table.indexes.find {|index| index[:primary]}
274mysql2psql(main):452:3> @conn.exec("ALTER TABLE #{PGconn.quote_ident(table.name)} ADD CONSTRAINT #{table.name}_pkey PRIMARY KEY(#{primary_index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")})")
275mysql2psql(main):453:3> end
276mysql2psql(main):454:2>
277mysql2psql(main):455:2* table.indexes.each do |index|
278mysql2psql(main):456:3* next if index[:primary]
279mysql2psql(main):457:3> unique = index[:unique] ? "UNIQUE " : nil
280mysql2psql(main):458:3> if @conn.server_version < 80200
281mysql2psql(main):459:4> @conn.exec("DROP INDEX #{PGconn.quote_ident(index[:name])} CASCADE;") if exists?(index[:name])
282mysql2psql(main):460:4> else
283mysql2psql(main):461:4* @conn.exec("DROP INDEX IF EXISTS #{PGconn.quote_ident(index[:name])} CASCADE;")
284mysql2psql(main):462:4> end
285mysql2psql(main):463:3> @conn.exec("CREATE #{unique}INDEX #{PGconn.quote_ident(index[:name])} ON #{PGconn.quote_ident(table.name)} (#{index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")});")
286mysql2psql(main):464:3> end
287mysql2psql(main):465:2>
288mysql2psql(main):466:2*
289mysql2psql(main):467:2* @conn.exec("VACUUM FULL ANALYZE #{PGconn.quote_ident(table.name)}")
290mysql2psql(main):468:2> puts "Indexed table #{table.name}"
291mysql2psql(main):469:2> rescue Exception => e
292mysql2psql(main):470:2> puts "Couldn't create indexes on #{table} (#{table.indexes.inspect})"
293mysql2psql(main):471:2> puts e
294mysql2psql(main):472:2> puts e.backtrace[0,3].join("\n")
295mysql2psql(main):473:2> end
296mysql2psql(main):474:1>
297mysql2psql(main):475:1* def write_constraints(table)
298mysql2psql(main):476:2> table.foreign_keys.each do |key|
299mysql2psql(main):477:3* key_sql = "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])})"
300mysql2psql(main):478:3> begin
301mysql2psql(main):479:4* @conn.exec(key_sql)
302mysql2psql(main):480:4> rescue Exception => e
303mysql2psql(main):481:4> puts "Error: \n#{key_sql}\n#{e}"
304mysql2psql(main):482:4> end
305mysql2psql(main):483:3> end
306mysql2psql(main):484:2> end
307mysql2psql(main):485:1>
308mysql2psql(main):486:1* def write_contents(table, reader)
309mysql2psql(main):487:2> _time1 = Time.now
310mysql2psql(main):488:2> copy_line = "COPY #{PGconn.quote_ident(table.name)} (#{table.columns.map {|column| PGconn.quote_ident(column[:name])}.join(", ")}) FROM stdin;"
311mysql2psql(main):489:2> @conn.exec(copy_line)
312mysql2psql(main):490:2> print "Loading #{table.name}: "
313mysql2psql(main):491:2> STDOUT.flush
314mysql2psql(main):492:2> _counter = reader.paginated_read(table, 1000) do |row, counter|
315mysql2psql(main):493:3* line = []
316mysql2psql(main):494:3> table.columns.each_with_index do |column, index|
317mysql2psql(main):495:4* if !row[index]
318mysql2psql(main):496:5> row[index] = '\N'
319mysql2psql(main):497:5> next
320mysql2psql(main):498:5> end
321mysql2psql(main):499:4> if column[:type] == "time"
322mysql2psql(main):500:5> row[index] = "%02d:%02d:%02d" % [row[index].hour, row[index].minute, row[index].second]
323mysql2psql(main):501:5> next
324mysql2psql(main):502:5> end
325mysql2psql(main):503:4> if row[index].is_a?(Mysql::Time)
326mysql2psql(main):504:5> row[index] = row[index].to_s.gsub('0000-00-00 00:00', '1970-01-01 00:00')
327mysql2psql(main):505:5> next
328mysql2psql(main):506:5> end
329mysql2psql(main):507:4>
330mysql2psql(main):508:4* if column_type(column) == "boolean"
331mysql2psql(main):509:5> row[index] = row[index] == 1 ? 't' : row[index] == 0 ? 'f' : row[index]
332mysql2psql(main):510:5> next
333mysql2psql(main):511:5> end
334mysql2psql(main):512:4>
335mysql2psql(main):513:4* if row[index].is_a?(String)
336mysql2psql(main):514:5> if column_type(column) == "bytea"
337mysql2psql(main):515:6> row[index] = PGconn.quote(row[index])
338mysql2psql(main):516:6> else
339mysql2psql(main):517:6* row[index] = row[index].gsub(/\\/, '\\\\\\').gsub(/\n/,'\n').gsub(/\t/,'\t').gsub(/\r/,'\r').gsub(/\0/, '')
340mysql2psql(main):518:6> end
341mysql2psql(main):519:5> end
342mysql2psql(main):520:4> end
343mysql2psql(main):521:3> @conn.putline(row.join("\t") + "\n")
344mysql2psql(main):522:3>
345mysql2psql(main):523:3* if counter % 5000 == 0
346mysql2psql(main):524:4> print "*"
347mysql2psql(main):525:4> STDOUT.flush
348mysql2psql(main):526:4> @conn.endcopy
349mysql2psql(main):527:4> @conn.exec(copy_line)
350mysql2psql(main):528:4> elsif counter % 1000 == 0
351mysql2psql(main):529:4> print "."
352mysql2psql(main):530:4> STDOUT.flush
353mysql2psql(main):531:4> end
354mysql2psql(main):532:3> end
355mysql2psql(main):533:2> _time2 = Time.now
356mysql2psql(main):534:2> puts " #{_counter} (#{((_time2 - _time1) / 60).round}min #{((_time2 - _time1) % 60).round}s)"
357mysql2psql(main):535:2> # @conn.putline(".\n")
358mysql2psql(main):536:2* @conn.endcopy
359mysql2psql(main):537:2> end
360mysql2psql(main):538:1>
361mysql2psql(main):539:1* def close
362mysql2psql(main):540:2> @conn.close
363mysql2psql(main):541:2> end
364mysql2psql(main):542:1> end
365=> nil
366mysql2psql(main):543:0>
367mysql2psql(main):544:0*
368mysql2psql(main):545:0* class Converter
369mysql2psql(main):546:1> attr_reader :reader, :writer
370mysql2psql(main):547:1>
371mysql2psql(main):548:1* def initialize(reader, writer, options = {})
372mysql2psql(main):549:2> @reader = reader
373mysql2psql(main):550:2> @writer = writer
374mysql2psql(main):551:2> @exclude_tables = options[:exclude_tables] || []
375mysql2psql(main):552:2> @only_tables = options[:only_tables] ? Array(options[:only_tables]) : nil
376mysql2psql(main):553:2> @supress_data = options[:supress_data]
377mysql2psql(main):554:2> end
378mysql2psql(main):555:1>
379mysql2psql(main):556:1* def convert
380mysql2psql(main):557:2> _time1 = Time.now
381mysql2psql(main):558:2>
382mysql2psql(main):559:2* tables = reader.tables.
383mysql2psql(main):560:2* reject {|table| @exclude_tables.include?(table.name)}.
384mysql2psql(main):561:2* select {|table| @only_tables ? @only_tables.include?(table.name) : true}
385mysql2psql(main):562:2>
386mysql2psql(main):563:2* tables.each do |table|
387mysql2psql(main):564:3* writer.write_table(table)
388mysql2psql(main):565:3> end
389mysql2psql(main):566:2>
390mysql2psql(main):567:2* _time2 = Time.now
391mysql2psql(main):568:2> tables.each do |table|
392mysql2psql(main):569:3* writer.write_contents(table, reader)
393mysql2psql(main):570:3> end unless @supress_data
394mysql2psql(main):571:2>
395mysql2psql(main):572:2* _time3 = Time.now
396mysql2psql(main):573:2> tables.each do |table|
397mysql2psql(main):574:3* writer.write_indexes(table)
398mysql2psql(main):575:3> end
399mysql2psql(main):576:2> tables.each do |table|
400mysql2psql(main):577:3* writer.write_constraints(table)
401mysql2psql(main):578:3> end
402mysql2psql(main):579:2>
403mysql2psql(main):580:2*
404mysql2psql(main):581:2* writer.close
405mysql2psql(main):582:2> _time4 = Time.now
406mysql2psql(main):583:2> puts "Table creation #{((_time2 - _time1) / 60).round} min, loading #{((_time3 - _time2) / 60).round} min, indexing #{((_time4 - _time3) / 60).round} min, total #{((_time4 - _time1) / 60).round} min"
407mysql2psql(main):584:2> end
408mysql2psql(main):585:1> end
409=> nil
410mysql2psql(main):586:0>
411mysql2psql(main):587:0*
412mysql2psql(main):588:0* reader = MysqlReader.new('localhost', 'root', nil, 'prophotos')
413ArgumentError: wrong number of arguments (4 for 0)
414 from mysql2psql:588:in `initialize'
415 from mysql2psql:588:in `new'
416 from mysql2psql:588
417 from /usr/local/lib/site_ruby/1.8/rubygems/version.rb:36
418mysql2psql(main):589:0> #writer = PostgresFileWriter.new($ARGV[2] || "output.sql")
419mysql2psql(main):590:0* writer = PostgresDbWriter.new('localhost', 'prophotos', '123', 'prophotos_development:old')
420NameError: uninitialized constant PostgresDbWriter::PGconn
421 from mysql2psql:383:in `connection'
422 from mysql2psql:388:in `initialize'
423 from mysql2psql:590:in `new'
424 from mysql2psql:590
425 from /usr/local/lib/site_ruby/1.8/rubygems/version.rb:36
426mysql2psql(main):591:0> converter = Converter.new(reader, writer, :only_tables => %w(articles images))
427=> #<Converter:0xb7aa497c @writer=nil, @only_tables=["articles", "images"], @supress_data=nil, @exclude_tables=[], @reader=nil>
428mysql2psql(main):592:0> converter.convert
429NoMethodError: undefined method `tables' for nil:NilClass
430 from mysql2psql:559:in `convert'
431 from mysql2psql:592
432 from /usr/local/lib/site_ruby/1.8/rubygems/version.rb:36
433mysql2psql(main):593:0>
434mysql2psql(main):594:0* mysql2psql(main):594:0> bash: require : commande introuvable