Skip to content

Commit 2823a04

Browse files
authored
Try/except all DB operations in migration (#2728)
1 parent f9abc8c commit 2823a04

1 file changed

Lines changed: 38 additions & 20 deletions

File tree

koku/api/migrations/0039_create_hive_db.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ def create_hive_db(apps, schema_editor):
5757
LOG.info(e)
5858
LOG.info(f"Role {rolname} exists.")
5959

60-
LOG.info(f"""Granting role "{rolname}" membership to "{kokudbuser}".""")
61-
cur.execute(f"""grant "{rolname}" to "{kokudbuser}"; """)
60+
try:
61+
LOG.info(f"""Granting role "{rolname}" membership to "{kokudbuser}".""")
62+
cur.execute(f"""grant "{rolname}" to "{kokudbuser}"; """)
63+
except (ProgrammingError, InsufficientPrivilege) as e:
64+
LOG.info(e)
6265

6366
try:
6467
LOG.info(f"Creating database {rolname}.")
@@ -68,31 +71,46 @@ def create_hive_db(apps, schema_editor):
6871
LOG.info(f"Database {rolname} exists.")
6972

7073
# Revoke access to koku db from public
71-
cur.execute(db_access_check_sql, ("public", kokudb))
72-
if cur.fetchone()[0]:
73-
LOG.info(f"Revoking public access to {kokudb}.")
74-
cur.execute(role_public_revoke_sql.format(kokudb))
74+
try:
75+
cur.execute(db_access_check_sql, ("public", kokudb))
76+
if cur.fetchone()[0]:
77+
LOG.info(f"Revoking public access to {kokudb}.")
78+
cur.execute(role_public_revoke_sql.format(kokudb))
79+
except (ProgrammingError, InsufficientPrivilege) as e:
80+
LOG.info(e)
7581

7682
# Revoke access to hive db from public
77-
cur.execute(db_access_check_sql, ("public", datname))
78-
if cur.fetchone()[0]:
79-
LOG.info(f"Revoking public access to {datname}.")
80-
cur.execute(role_public_revoke_sql.format(datname))
83+
try:
84+
cur.execute(db_access_check_sql, ("public", datname))
85+
if cur.fetchone()[0]:
86+
LOG.info(f"Revoking public access to {datname}.")
87+
cur.execute(role_public_revoke_sql.format(datname))
88+
except (ProgrammingError, InsufficientPrivilege) as e:
89+
LOG.info(e)
8190

8291
# Revoke access to koku db from hive user
83-
cur.execute(db_access_check_sql, (rolname, kokudb))
84-
if cur.fetchone()[0]:
85-
LOG.info(f"Revoking {rolname} access to {kokudb}.")
86-
cur.execute(role_revoke_sql)
92+
try:
93+
cur.execute(db_access_check_sql, (rolname, kokudb))
94+
if cur.fetchone()[0]:
95+
LOG.info(f"Revoking {rolname} access to {kokudb}.")
96+
cur.execute(role_revoke_sql)
97+
except (ProgrammingError, InsufficientPrivilege) as e:
98+
LOG.info(e)
8799

88100
# Grant access to hive db from koku user
89-
cur.execute(db_access_check_sql, (kokudbuser, datname))
90-
if not cur.fetchone()[0]:
91-
LOG.info(f"Granting {kokudbuser} access to {datname}.")
92-
cur.execute(role_grant_sql)
101+
try:
102+
cur.execute(db_access_check_sql, (kokudbuser, datname))
103+
if not cur.fetchone()[0]:
104+
LOG.info(f"Granting {kokudbuser} access to {datname}.")
105+
cur.execute(role_grant_sql)
106+
except (ProgrammingError, InsufficientPrivilege) as e:
107+
LOG.info(e)
93108

94-
LOG.info(f"""Revoking role "{rolname}" membership from "{kokudbuser}".""")
95-
cur.execute(f"""revoke "{rolname}" from "{kokudbuser}"; """)
109+
try:
110+
LOG.info(f"""Revoking role "{rolname}" membership from "{kokudbuser}".""")
111+
cur.execute(f"""revoke "{rolname}" from "{kokudbuser}"; """)
112+
except (ProgrammingError, InsufficientPrivilege) as e:
113+
LOG.info(e)
96114

97115

98116
class Migration(migrations.Migration):

0 commit comments

Comments
 (0)