From: Max Lapshin Date: Sat, 5 Sep 2009 08:45:09 +0000 (+0400) Subject: merge with antage X-Git-Url: http://git.neszt.hu/?a=commitdiff_plain;h=a2aa66d8e306a141015ea898c56ddb7c08d5d9ec;p=mysql2postgres merge with antage --- a2aa66d8e306a141015ea898c56ddb7c08d5d9ec diff --cc mysql2psql index d26663b,9a2a9d5..6a2f2ac --- a/mysql2psql +++ b/mysql2psql @@@ -214,22 -214,10 +214,22 @@@ class PostgresWriter < Write "boolean" when "blob" "bytea" + when "mediumtext" + "text" + when "longtext" + "text" when "text" "text" + when "double" + default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default]}" if default + "double precision" + when /^enum/ + default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default]}" if default + enum = column[:type].gsub(/enum|\(|\)/, '') + max_enum_size = enum.split(',').map{ |check| check.size() -2}.sort[-1] + "character varying(#{max_enum_size}), check( #{column[:name]} in (#{enum}))" when "float" - default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default]}" if default + default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default].to_f}" if default "real" when "decimal" default = " DEFAULT #{column[:default].nil? ? 'NULL' : column[:default]}" if default @@@ -317,8 -299,8 +317,7 @@@ EO @f << <<-EOF \n) - WITH (OIDS=FALSE); - + WITHOUT OIDS; - EOF table.indexes.each do |index| @@@ -377,18 -359,17 +376,18 @@@ EO @f.close end end - + class PostgresDbWriter < PostgresWriter - def connection(hostname, login, password, database) - require 'postgres' - @conn = PGconn.open('host' => hostname, 'user' => login, 'password' => password, 'dbname' => database) + def connection(hostname, login, password, database, port) + database, schema = database.split(":") + @conn = PGconn.open('host' => hostname, 'user' => login, 'password' => password, 'dbname' => database, 'port' => port.to_s) + @conn.exec("SET search_path TO #{PGconn.quote_ident(schema)}") if schema end - def initialize(hostname, login, password, database) - @conn = connection(hostname, login, password, database) + def initialize(hostname, login, password, database, port = 5432) + connection(hostname, login, password, database, port) @conn.exec("SET client_encoding = 'UTF8'") - @conn.exec("SET standard_conforming_strings = off") + @conn.exec("SET standard_conforming_strings = off") if @conn.server_version >= 80200 @conn.exec("SET check_function_bodies = false") @conn.exec("SET client_min_messages = warning") end @@@ -422,16 -413,14 +431,20 @@@ @conn.exec "SELECT pg_catalog.setval('#{table.name}_#{primary_key}_seq', #{maxval}, true)" end - @conn.exec "DROP TABLE IF EXISTS #{PGconn.quote_ident(table.name)} CASCADE;" - create_sql = "CREATE TABLE #{PGconn.quote_ident(table.name)} (\n" + columns + "\n)\nWITH (OIDS=FALSE);" + if @conn.server_version < 80200 + @conn.exec "DROP TABLE #{PGconn.quote_ident(table.name)} CASCADE;" if exists?(table.name) + else + @conn.exec "DROP TABLE IF EXISTS #{PGconn.quote_ident(table.name)} CASCADE;" + end - @conn.exec("CREATE TABLE #{PGconn.quote_ident(table.name)} (\n" + columns + "\n)\nWITHOUT OIDS;") ++ create_sql = "CREATE TABLE #{PGconn.quote_ident(table.name)} (\n" + columns + "\n)\nWITHOUT OIDS;" + begin + @conn.exec(create_sql) + rescue Exception => e + puts "Error: \n#{create_sql}" + raise + end puts "Created table #{table.name}" - + end def write_indexes(table)