Skip to content

Commit b97fad1

Browse files
committed
Restore conflicting index name migration
* merges intent of ef9951e by Aaron Peckham with preious technique for duplicate detection for PostgreSQL < 9
1 parent bf67dd1 commit b97fad1

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

README.rdoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ e.g.
121121

122122
== Notes, Limitations, Outstanding Issues..
123123

124-
* index migration doesn't handle cases where the index name is the same as a table name (this is ok on MYSQL by not PostgreSQL). Workaround: manual migration required.
125124
* more test coverage, as always...
126125

127126

lib/mysql2psql/postgres_db_writer.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,26 @@ def write_indexes(table)
102102
table.indexes.each do |index|
103103
next if index[:primary]
104104
unique = index[:unique] ? "UNIQUE " : nil
105-
index_sql = "CREATE #{unique}INDEX #{PGconn.quote_ident(index[:name])} ON #{PGconn.quote_ident(table.name)} (#{index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")});"
105+
106+
# MySQL allows an index name which could be equal to a table name, Postgres doesn't
107+
indexname = index[:name]
108+
indexname_quoted = ''
109+
110+
if indexname.eql?(table.name)
111+
indexname = (@conn.server_version < 90000) ? "#{indexname}_index" : nil
112+
puts "WARNING: index \"#{index[:name]}\" equals table name. This is not allowed in PostgreSQL and will be renamed."
113+
end
114+
115+
if indexname
116+
indexname_quoted = PGconn.quote_ident(indexname)
117+
if @conn.server_version < 80200
118+
@conn.exec("DROP INDEX #{PGconn.quote_ident(indexname)} CASCADE;") if exists?(indexname)
119+
else
120+
@conn.exec("DROP INDEX IF EXISTS #{PGconn.quote_ident(indexname)} CASCADE;")
121+
end
122+
end
123+
124+
index_sql = "CREATE #{unique}INDEX #{indexname_quoted} ON #{PGconn.quote_ident(table.name)} (#{index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")});"
106125
@conn.exec(index_sql)
107126
end
108127

0 commit comments

Comments
 (0)