migrate.py: Add function for conditionally creating an index

(imported from commit f0ca7cc83e334d4210d21d33afa2e0196172176e)
This commit is contained in:
Zev Benjamin
2014-03-01 12:32:42 -05:00
parent 230802ee22
commit 0b29a2f53a

View File

@@ -65,3 +65,15 @@ def add_bool_columns(db, table, cols):
stmt = ('ALTER TABLE %s ' % (table,)) \
+ ', '.join(['ALTER %s SET NOT NULL' % (col,) for col in cols])
timed_ddl(db, stmt)
def create_index_if_nonexistant(db, table, col, index):
validate(table)
validate(col)
validate(index)
test = """SELECT relname FROM pg_class
WHERE relname = %s"""
if len(db.execute(test, params=[index])) != 0:
print "Not creating index '%s' because it already exists" % (index,)
else:
stmt = "CREATE INDEX %s ON %s (%s)" % (index, table, col)
timed_ddl(db, stmt)