Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Fix double-quoted SQL string defaults breaking PostgreSQL, add pre-commit hook
Murtaza Nasira57611e68b7d
diff --git a/scripts/pre-commit b/scripts/pre-commitnew file mode 100755index 00000000..692ddeeb--- /dev/null+++ b/scripts/pre-commit@@ -0,0 +1,21 @@+#!/bin/sh+#+# Pre-commit hook: runs PostgreSQL migration compatibility tests+# when migration-related files are staged.+#+# Install: ln -sf ../../scripts/pre-commit .git/hooks/pre-commit++STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)++# Only run if migration-related files are staged+if echo "$STAGED_FILES" | grep -qE '^(src/init_db\.py|src/utils/database\.py)$'; then+ echo "Migration files changed — running PostgreSQL compatibility checks..."+ python tests/test_migration_compatibility.py+ if [ $? -ne 0 ]; then+ echo ""+ echo "Pre-commit hook FAILED: PostgreSQL migration compatibility issues found."+ echo "Fix the issues above before committing."+ exit 1+ fi+ echo "All migration compatibility checks passed."+fidiff --git a/src/init_db.py b/src/init_db.pyindex d52dcc91..59f448cf 100644--- a/src/init_db.py+++ b/src/init_db.py@@ -71,7 +71,7 @@ def initialize_database(app):app.logger.info("Added company column to user table")if add_column_if_not_exists(engine, 'user', 'diarize', 'BOOLEAN'):app.logger.info("Added diarize column to user table")- if add_column_if_not_exists(engine, 'user', 'ui_language', 'VARCHAR(10) DEFAULT "en"'):+ if add_column_if_not_exists(engine, 'user', 'ui_language', "VARCHAR(10) DEFAULT 'en'"):app.logger.info("Added ui_language column to user table")if add_column_if_not_exists(engine, 'user', 'sso_provider', 'VARCHAR(100)'):app.logger.info("Added sso_provider column to user table")@@ -158,7 +158,7 @@ def initialize_database(app):app.logger.info("Added transcription_duration_seconds column to recording table")if add_column_if_not_exists(engine, 'recording', 'summarization_duration_seconds', 'INTEGER'):app.logger.info("Added summarization_duration_seconds column to recording table")- if add_column_if_not_exists(engine, 'recording', 'processing_source', 'VARCHAR(50) DEFAULT "upload"'):+ if add_column_if_not_exists(engine, 'recording', 'processing_source', "VARCHAR(50) DEFAULT 'upload'"):app.logger.info("Added processing_source column to recording table")if add_column_if_not_exists(engine, 'recording', 'error_message', 'TEXT'):app.logger.info("Added error_message column to recording table")@@ -272,7 +272,7 @@ def initialize_database(app):# Auto speaker labelling settingsif add_column_if_not_exists(engine, 'user', 'auto_speaker_labelling', 'BOOLEAN DEFAULT 0'):app.logger.info("Added auto_speaker_labelling column to user table")- if add_column_if_not_exists(engine, 'user', 'auto_speaker_labelling_threshold', 'VARCHAR(10) DEFAULT "medium"'):+ if add_column_if_not_exists(engine, 'user', 'auto_speaker_labelling_threshold', "VARCHAR(10) DEFAULT 'medium'"):app.logger.info("Added auto_speaker_labelling_threshold column to user table")# Auto summarization setting (per-user, default enabled)@@ -297,7 +297,7 @@ def initialize_database(app):app.logger.info("Added export_template_id column to folder table")# Add source tracking columns to internal_share table- if add_column_if_not_exists(engine, 'internal_share', 'source_type', 'VARCHAR(20) DEFAULT "manual"'):+ if add_column_if_not_exists(engine, 'internal_share', 'source_type', "VARCHAR(20) DEFAULT 'manual'"):app.logger.info("Added source_type column to internal_share table")if add_column_if_not_exists(engine, 'internal_share', 'source_tag_id', 'INTEGER'):diff --git a/src/utils/database.py b/src/utils/database.pyindex 8b847592..a0ac0a31 100644--- a/src/utils/database.py+++ b/src/utils/database.py@@ -42,6 +42,10 @@ def add_column_if_not_exists(engine, table_name, column_name, column_type):# PostgreSQL uses BYTEA, not BLOBcolumn_type = re.sub(r'\bBLOB\b', 'BYTEA', column_type, flags=re.IGNORECASE)+ # PostgreSQL interprets double-quoted strings as identifiers, not literals+ # Convert DEFAULT "value" to DEFAULT 'value'+ column_type = re.sub(r'''DEFAULT\s+"([^"]*)"''', r"DEFAULT '\1'", column_type, flags=re.IGNORECASE)+with engine.connect() as conn:# Quote identifiers to handle reserved keywords (e.g., "user" in PostgreSQL)# MySQL uses backticks, PostgreSQL/SQLite use double quotesdiff --git a/tests/test_migration_compatibility.py b/tests/test_migration_compatibility.pyindex e40320c6..e8a43b52 100644--- a/tests/test_migration_compatibility.py+++ b/tests/test_migration_compatibility.py@@ -188,6 +188,33 @@ class TestMigrationCompatibility(unittest.TestCase):matches = re.findall(pattern, self.content, re.IGNORECASE)# Just informational - these are fine because the utility converts them+ def test_no_double_quoted_string_defaults(self):+ """+ Ensure no SQL DEFAULT values use double-quoted strings.++ In SQL, double quotes denote identifiers (column/table names), not string+ literals. SQLite tolerates this, but PostgreSQL will interpret DEFAULT "en"+ as a reference to a column named "en" and fail with 'column "en" does not exist'.++ String defaults must use single quotes: DEFAULT 'en'+ """+ # Match DEFAULT followed by a double-quoted string value+ pattern = r'DEFAULT\s+"[^"]*"'++ lines = self.content.splitlines()+ problematic = []+ for i, line in enumerate(lines, 1):+ if re.search(pattern, line, re.IGNORECASE):+ problematic.append(f" Line {i}: {line.strip()}")++ self.assertEqual(+ len(problematic), 0,+ f"Found double-quoted string defaults in init_db.py. "+ f"PostgreSQL interprets double quotes as column identifiers, not string literals. "+ f"Use single quotes instead (e.g., DEFAULT 'en' not DEFAULT \"en\"):\n" ++ "\n".join(problematic)+ )+def test_create_index_uses_utility_for_user_table(self):"""Ensure index creation on 'user' table uses create_index_if_not_exists().