sql: catch ProgrammingError to handle Postgresql < 9, missing IF EXISTS

This commit is contained in:
Frédéric Péters 2013-08-07 11:01:48 +02:00
parent 8163d7e267
commit 5d25719ce2
1 changed files with 16 additions and 2 deletions

View File

@ -160,7 +160,14 @@ def do_formdef_tables(formdef):
# delete obsolete fields
for field in (existing_fields - needed_fields):
cur.execute('''ALTER TABLE %s DROP COLUMN IF EXISTS %s''' % (table_name, field))
try:
cur.execute('''ALTER TABLE %s DROP COLUMN IF EXISTS %s''' % (table_name, field))
except psycopg2.ProgrammingError:
# Postgreql < 9 doesn't have support for "IF EXISTS"
try:
cur.execute('''ALTER TABLE %s DROP COLUMN %s''' % (table_name, field))
except psycopg2.ProgrammingError:
pass
conn.commit()
cur.close()
@ -213,7 +220,14 @@ def do_user_table():
# delete obsolete fields
for field in (existing_fields - needed_fields):
cur.execute('''ALTER TABLE %s DROP COLUMN IF EXISTS %s''' % (table_name, field))
try:
cur.execute('''ALTER TABLE %s DROP COLUMN IF EXISTS %s''' % (table_name, field))
except psycopg2.ProgrammingError:
# Postgreql < 9 doesn't have support for "IF EXISTS"
try:
cur.execute('''ALTER TABLE %s DROP COLUMN %s''' % (table_name, field))
except psycopg2.ProgrammingError:
pass
conn.commit()
cur.close()