Skip to content

🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!

Fix #187: migrations broke on mixed-case DB_USER

Omega (QA)669314394536
diff --git a/backend/migrations/010_configure_admin_roles.sql b/backend/migrations/010_configure_admin_roles.sql
index 834739d..40e519b 100644
--- a/backend/migrations/010_configure_admin_roles.sql
+++ b/backend/migrations/010_configure_admin_roles.sql
@@ -6,8 +6,8 @@ BEGIN
-- Try to create admin role, but continue if it fails
BEGIN
-- Check if the db_admin_user exists, if not create it
- IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '%(db_admin_user)s') THEN
- CREATE ROLE %(db_admin_user)s WITH LOGIN PASSWORD '%(db_admin_password)s';
+ IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = %(db_admin_user_name)s) THEN
+ CREATE ROLE %(db_admin_user)s WITH LOGIN PASSWORD %(db_admin_password)s;
RAISE NOTICE 'Successfully created admin role %(db_admin_user)s';
END IF;
diff --git a/backend/migrations/apply_migrations.py b/backend/migrations/apply_migrations.py
index 9ff92a2..e87c698 100644
--- a/backend/migrations/apply_migrations.py
+++ b/backend/migrations/apply_migrations.py
@@ -9,6 +9,15 @@ import importlib.util
from psycopg2.extensions import AsIs
+def _quote_ident(name):
+ """Quote a PostgreSQL identifier, preserving case (issue #187).
+
+ Unquoted identifiers are folded to lowercase by PostgreSQL, so a
+ mixed-case DB_USER like 'WarrantyUser' must be double-quoted.
+ Embedded double quotes are escaped by doubling.
+ """
+ return '"' + str(name).replace('"', '""') + '"'
+
# Set up logging
logging.basicConfig(
level=logging.INFO,
@@ -120,10 +129,17 @@ def apply_migrations():
cur.execute(
sql,
{
- "db_name": AsIs(DB_NAME),
- "db_user": AsIs(DB_USER),
- "db_admin_user": AsIs(DB_ADMIN_USER),
- "db_admin_password": AsIs(DB_ADMIN_PASSWORD),
+ # Identifiers are double-quoted so PostgreSQL
+ # preserves their case (issue #187)
+ "db_name": AsIs(_quote_ident(DB_NAME)),
+ "db_user": AsIs(_quote_ident(DB_USER)),
+ "db_admin_user": AsIs(_quote_ident(DB_ADMIN_USER)),
+ # Raw names for string-literal contexts
+ # (e.g. rolname comparisons)
+ "db_user_name": DB_USER,
+ "db_admin_user_name": DB_ADMIN_USER,
+ # Password is a value: let psycopg2 escape it
+ "db_admin_password": DB_ADMIN_PASSWORD,
}
)
else: