[migration/3.21] Make index and table creation idempotent
authorRémi Cardona <remi.cardona@logilab.fr>
Thu, 02 Jul 2015 12:19:49 +0200
changeset 10483 c8dbb845b465
parent 10482 88119421a09c
child 10484 7f15552b7463
[migration/3.21] Make index and table creation idempotent
misc/migration/3.21.0_Any.py
--- a/misc/migration/3.21.0_Any.py	Thu Jul 02 15:44:49 2015 +0200
+++ b/misc/migration/3.21.0_Any.py	Thu Jul 02 12:19:49 2015 +0200
@@ -62,19 +62,21 @@
 
 add_foreign_keys()
 
+cu = session.cnxset.cu
 helper = repo.system_source.dbhelper
-sql('DROP INDEX entities_extid_idx')
-sql(helper.sql_create_index('entities', 'extid', True))
+
+helper.drop_index(cu, 'entities', 'extid', False)
+helper.create_index(cu, 'entities', 'extid', True)
 
-sql('''
-CREATE TABLE moved_entities (
-  eid INTEGER PRIMARY KEY NOT NULL,
-  extid VARCHAR(256) UNIQUE
-)
-''')
+if 'moved_entities' not in helper.list_tables(cu):
+    sql('''
+    CREATE TABLE moved_entities (
+      eid INTEGER PRIMARY KEY NOT NULL,
+      extid VARCHAR(256) UNIQUE
+    )
+    ''')
 
 moved_entities = sql('SELECT -eid, extid FROM entities WHERE eid < 0')
-cu = session.cnxset.cu
 cu.executemany('INSERT INTO moved_entities (eid, extid) VALUES (%s, %s)',
                moved_entities)
 sql('DELETE FROM entities WHERE eid < 0')