Class: Mysql2psql::PostgresFileWriter

Inherits:
PostgresWriter show all
Defined in:
lib/mysql2psql/postgres_file_writer.rb

Instance Method Summary collapse

Methods inherited from PostgresWriter

#column_default, #column_description, #column_type, #column_type_info, #process_row, #sqlfor_reset_serial_sequence, #sqlfor_set_serial_sequence

Constructor Details

#initialize(file) ⇒ PostgresFileWriter

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 << <<-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

#closeObject



147
148
149
# File 'lib/mysql2psql/postgres_file_writer.rb', line 147

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 << <<-EOF
-- TRUNCATE #{table.name};
TRUNCATE #{PGconn.quote_ident(table.name)} CASCADE;

EOF
  if serial_key
  @f << <<-EOF
#{sqlfor_reset_serial_sequence(table,serial_key,maxval)}
EOF
  end
end

#write_constraints(table) ⇒ Object



123
124
125
126
127
# File 'lib/mysql2psql/postgres_file_writer.rb', line 123

def write_constraints(table)
  table.foreign_keys.each do |key|
    @f << "ALTER TABLE #{PGconn.quote_ident(table.name)} ADD FOREIGN KEY (#{key[:column].map{|c|PGconn.quote_ident(c)}.join(', ')}) REFERENCES #{PGconn.quote_ident(key[:ref_table])}(#{key[:ref_column].map{|c|PGconn.quote_ident(c)}.join(', ')}) ON UPDATE #{key[:on_update]} ON DELETE #{key[:on_delete]};\n"
  end
end

#write_contents(table, reader) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/mysql2psql/postgres_file_writer.rb', line 130

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|
    process_row(table, row)
    @f << row.join("\t") + "\n"
  end
  @f << "\\.\n\n"
  #@f << "VACUUM FULL ANALYZE #{PGconn.quote_ident(table.name)};\n\n"
end

#write_indexes(table) ⇒ Object



120
121
# File 'lib/mysql2psql/postgres_file_writer.rb', line 120

def write_indexes(table)
end

#write_sequence_update(table, options) ⇒ 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
# File 'lib/mysql2psql/postgres_file_writer.rb', line 41

def write_sequence_update(table, options)
  serial_key_column = table.columns.detect do |column|
    column[:auto_increment]
  end
  
  if serial_key_column
    serial_key = serial_key_column[:name]
    serial_key_seq = "#{table.name}_#{serial_key}_seq"
    max_value = serial_key_column[:maxval].to_i < 1 ? 1 : serial_key_column[:maxval] + 1
    
    @f << <<-EOF
--
-- Name: #{serial_key_seq}; Type: SEQUENCE; Schema: public
--
EOF

    if !options.supress_ddl
      @f << <<-EOF
DROP SEQUENCE IF EXISTS #{serial_key_seq} CASCADE;
 
CREATE SEQUENCE #{serial_key_seq}
  INCREMENT BY 1
  NO MAXVALUE
  NO MINVALUE
  CACHE 1;
EOF
    end
    
    if !options.supress_sequence_update
      @f << <<-EOF
#{sqlfor_set_serial_sequence(table, serial_key_seq, max_value)}
EOF
    end
  end
end

#write_table(table, options) ⇒ Object



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
112
113
114
115
116
117
118
# File 'lib/mysql2psql/postgres_file_writer.rb', line 77

def write_table(table, options)
  primary_keys = []
  serial_key = nil
  maxval = nil
  
  columns = table.columns.map do |column|
    if column[:primary_key]
      primary_keys << column[:name]
    end
    "  " + column_description(column, options)
  end.join(",\n")
  
  @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