fix: replace f-string SQL with psycopg2.sql for safe identifier handling#32
fix: replace f-string SQL with psycopg2.sql for safe identifier handling#32wicky-zipstack wants to merge 2 commits intomainfrom
Conversation
Replace all raw f-string SQL interpolation in sample_project.py with psycopg2.sql.SQL, sql.Identifier, and sql.Literal for proper escaping: - CREATE/DROP DATABASE, CREATE/DROP USER — sql.Identifier for names - GRANT, ALTER SCHEMA — sql.Identifier for schema and user names - WHERE datname = ... — sql.Literal for value parameters - CREATE USER ... PASSWORD — sql.Literal for password value - Schema DDL (DROP/CREATE SCHEMA) — sql.Identifier for schema names
|
| Filename | Overview |
|---|---|
| backend/backend/application/sample_project/sample_project.py | All f-string SQL interpolation replaced with psycopg2.sql composables; dead code (unused user_check_query) removed; all 15 SQL construction sites are correctly migrated with appropriate use of sql.Identifier for identifiers and sql.Literal for values. |
Sequence Diagram
sequenceDiagram
participant Caller
participant SampleProject
participant psycopg2_sql as psycopg2.sql
participant AdminDB as Admin PostgreSQL DB
participant NewDB as New Sample DB
Caller->>SampleProject: load_sample_project()
SampleProject->>SampleProject: create_new_database()
SampleProject->>psycopg2_sql: sql.SQL("CREATE DATABASE {} TEMPLATE {}").format(sql.Identifier(db), sql.Identifier(template))
psycopg2_sql-->>SampleProject: Composable (safe, double-quoted identifiers)
SampleProject->>AdminDB: execute_sql_queries([create_db, create_user, grant])
AdminDB-->>SampleProject: OK
SampleProject->>NewDB: _grant_schema_permissions_on_new_db()
Note over SampleProject,NewDB: Direct connection to new DB
SampleProject->>psycopg2_sql: sql.SQL("GRANT USAGE ON SCHEMA {} TO {}").format(sql.Identifier(schema), sql.Identifier(user))
psycopg2_sql-->>SampleProject: Composable
SampleProject->>NewDB: cursor.execute(composable)
NewDB-->>SampleProject: OK
SampleProject->>SampleProject: create_project_connection()
Note over SampleProject: Updates sample_connection to new DB creds, closes admin connection
SampleProject->>SampleProject: create_schemas() [non-clone path only]
SampleProject->>psycopg2_sql: sql.SQL("DROP SCHEMA IF EXISTS {} CASCADE").format(sql.Identifier(schema))
psycopg2_sql-->>SampleProject: Composable
SampleProject->>NewDB: execute_sql_queries([drop/create schemas])
NewDB-->>SampleProject: OK
Reviews (2): Last reviewed commit: "fix: use proper type annotation for sql...." | Re-trigger Greptile
Address Greptile P2 — type hint was weakened from list[str] to bare list. Now correctly annotated as list[sql.Composable].
What
sample_project.pywithpsycopg2.sql.SQL,sql.Identifier, andsql.LiteralWhy
re.sub, proper parameterization is the industry standardHow
sql.Identifierfor database namessql.Identifierfor user names,sql.Literalfor passwordssql.Identifierfor schema and user namessql.Literalfor value parameterssql.Identifierfor schema names in DROP/CREATESampleProject— covers all 4 sample projects (Jaffle Shop Starter/Final, DVD Rental Starter/Final)Can this PR break any existing features. If yes, please list possible items. If no, please explain why. (PS: Admins do not merge the PR without this section filled)
psycopg2.sqlgenerates identical SQL output but with proper quoting. The only difference is identifiers are now double-quoted (e.g.,"my_database"instead ofmy_database), which is valid PostgreSQL and handles reserved words correctly. Sample project creation should work identically.Database Migrations
Env Config
Relevant Docs
Related Issues or PRs
Dependencies Versions
Notes on Testing
Screenshots
N/A
Checklist