Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Fix PostgreSQL boolean defaults in migrations

Murtaza Nasircd0010372081
diff --git a/src/init_db.py b/src/init_db.py
index 3849b8e6..2f454971 100644
--- a/src/init_db.py
+++ b/src/init_db.py
@@ -363,8 +363,9 @@ def initialize_database(app):
# Rename is_starred to is_highlighted by copying data
try:
with engine.connect() as conn:
- # Add is_highlighted column
- conn.execute(text('ALTER TABLE shared_recording_state ADD COLUMN is_highlighted BOOLEAN DEFAULT 0'))
+ # Add is_highlighted column (PostgreSQL needs FALSE, SQLite uses 0)
+ default_val = 'FALSE' if engine.name == 'postgresql' else '0'
+ conn.execute(text(f'ALTER TABLE shared_recording_state ADD COLUMN is_highlighted BOOLEAN DEFAULT {default_val}'))
# Copy data from is_starred to is_highlighted
conn.execute(text('UPDATE shared_recording_state SET is_highlighted = is_starred'))
conn.commit()
diff --git a/src/utils/database.py b/src/utils/database.py
index 7c88c00b..7a5a4e35 100644
--- a/src/utils/database.py
+++ b/src/utils/database.py
@@ -22,6 +22,11 @@ def add_column_if_not_exists(engine, table_name, column_name, column_type):
columns = [col['name'] for col in inspector.get_columns(table_name)]
if column_name not in columns:
+ # PostgreSQL requires TRUE/FALSE for boolean defaults, not 0/1
+ if engine.name == 'postgresql' and 'BOOLEAN' in column_type.upper():
+ column_type = column_type.replace('DEFAULT 0', 'DEFAULT FALSE')
+ column_type = column_type.replace('DEFAULT 1', 'DEFAULT TRUE')
+
with engine.connect() as conn:
# Quote identifiers to handle reserved keywords (e.g., "user" in PostgreSQL)
# MySQL uses backticks, PostgreSQL/SQLite use double quotes