Skip to content

Commit d15b852

Browse files
michaelchuclaude
andauthored
Fix noisy Postgres ALTER TABLE errors in DB init (#236)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 737bfd3 commit d15b852

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

optopsy/ui/app.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,25 @@ def _init_db_sync() -> None:
276276
# transaction as failed after any error). Using separate
277277
# transactions avoids SAVEPOINTs, which are unreliable with
278278
# pysqlite's default transaction handling.
279+
is_pg = sync_url.startswith("postgresql")
279280
for col, definition in [
280281
("defaultOpen", "INTEGER DEFAULT 0"),
281282
("waitForAnswer", "INTEGER"),
282283
]:
283284
try:
284285
with engine.begin() as conn:
285-
conn.execute(
286-
text(f'ALTER TABLE steps ADD COLUMN "{col}" {definition}')
287-
)
286+
if is_pg:
287+
conn.execute(
288+
text(
289+
f'ALTER TABLE steps ADD COLUMN IF NOT EXISTS "{col}" {definition}'
290+
)
291+
)
292+
else:
293+
conn.execute(
294+
text(
295+
f'ALTER TABLE steps ADD COLUMN "{col}" {definition}'
296+
)
297+
)
288298
except (OperationalError, ProgrammingError):
289299
pass # column already exists
290300

0 commit comments

Comments
 (0)