"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
@f << <<-EOF
\n)
- WITH (OIDS=FALSE);
-
+ WITHOUT OIDS;
-
EOF
table.indexes.each do |index|
@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
@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)