Skip to content

Commit ef9951e

Browse files
author
Aaron Peckham
committed
Allow Postgres to rename indexes; don't try to preserve MySQL index names because they might not be unique or might collide with table names
1 parent af016ae commit ef9951e

File tree

2 files changed

+2
-15
lines changed

2 files changed

+2
-15
lines changed

lib/mysql2psql/postgres_db_writer.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,7 @@ def write_indexes(table)
9494
next if index[:primary]
9595
unique = index[:unique] ? "UNIQUE " : nil
9696

97-
#MySQL allows an index name which could be equal to a table name, Postgres doesn't
98-
indexname = index[:name]
99-
if indexname.eql?(table.name)
100-
indexnamenew = "#{indexname}_index"
101-
puts "WARNING: index \"#{indexname}\" equals table name. This is not allowed by postgres and will be renamed to \"#{indexnamenew}\""
102-
indexname = indexnamenew
103-
end
104-
105-
if @conn.server_version < 80200
106-
@conn.exec("DROP INDEX #{PGconn.quote_ident(indexname)} CASCADE;") if exists?(indexname)
107-
else
108-
@conn.exec("DROP INDEX IF EXISTS #{PGconn.quote_ident(indexname)} CASCADE;")
109-
end
110-
@conn.exec("CREATE #{unique}INDEX #{PGconn.quote_ident(indexname)} ON #{PGconn.quote_ident(table.name)} (#{index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")});")
97+
@conn.exec("CREATE #{unique}INDEX ON #{PGconn.quote_ident(table.name)} (#{index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")});")
11198
end
11299

113100

test/integration/convert_to_db_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_datetime_defaults
102102

103103
def test_index_conversion
104104
result = exec_sql_on_psql('SELECT pg_get_indexdef(indexrelid) FROM pg_index WHERE indrelid = \'test_index_conversion\'::regclass').first
105-
assert_equal "CREATE UNIQUE INDEX test_index_conversion_index ON test_index_conversion USING btree (column_a)", result["pg_get_indexdef"]
105+
assert_equal "CREATE UNIQUE INDEX test_index_conversion_column_a_idx ON test_index_conversion USING btree (column_a)", result["pg_get_indexdef"]
106106
end
107107

108108
def test_foreign_keys

0 commit comments

Comments
 (0)