Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 829766a

Browse files
Added pragma on file connections and DROP TABLE IF EXISTS
1 parent 4588a3e commit 829766a

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

migrations/versions/2025_01_21_0820-4dec3e456c9e_add_on_delete_cascade.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def upgrade() -> None:
2121
# To add ON DELETE CASCADE to the foreign key constraint, we need to
2222
# rename the table, create a new table with the constraint, and copy
2323
# the data over.
24+
op.execute("DROP TABLE IF EXISTS _prompts_old;")
2425
op.execute("ALTER TABLE prompts RENAME TO _prompts_old;")
2526
op.execute(
2627
"""
@@ -39,6 +40,7 @@ def upgrade() -> None:
3940
op.execute("DROP TABLE _prompts_old;")
4041

4142
# Doing the same for the sessions table
43+
op.execute("DROP TABLE IF EXISTS _sessions_old;")
4244
op.execute("ALTER TABLE sessions RENAME TO _sessions_old;")
4345
op.execute(
4446
"""
@@ -54,6 +56,7 @@ def upgrade() -> None:
5456
op.execute("DROP TABLE _sessions_old;")
5557

5658
# Doing the same for the output table
59+
op.execute("DROP TABLE IF EXISTS _outputs_old;")
5760
op.execute("ALTER TABLE outputs RENAME TO _outputs_old;")
5861
op.execute(
5962
"""
@@ -70,6 +73,7 @@ def upgrade() -> None:
7073
op.execute("DROP TABLE _outputs_old;")
7174

7275
# Doing the same for the alerts table
76+
op.execute("DROP TABLE IF EXISTS _alerts_old;")
7377
op.execute("ALTER TABLE alerts RENAME TO _alerts_old;")
7478
op.execute(
7579
"""
@@ -89,7 +93,7 @@ def upgrade() -> None:
8993
op.execute("DROP TABLE _alerts_old;")
9094

9195
# Dropping unused table
92-
op.execute("DROP TABLE settings;")
96+
op.execute("DROP TABLE IF EXISTS settings;")
9397

9498
# Create indexes for foreign keys
9599
op.execute("CREATE INDEX idx_outputs_prompt_id ON outputs(prompt_id);")

src/codegate/db/connection.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ def set_sqlite_pragma(dbapi_connection, connection_record):
4545
[SQLite docs](https://www.sqlite.org/foreignkeys.html)
4646
[SO](https://stackoverflow.com/questions/2614984/sqlite-sqlalchemy-how-to-enforce-foreign-keys)
4747
"""
48-
cursor = dbapi_connection.cursor()
49-
cursor.execute("PRAGMA foreign_keys=ON")
50-
cursor.close()
48+
# Check if the connection is the one you want to apply the PRAGMA to
49+
if connection_record.info.get('enable_fks', False):
50+
cursor = dbapi_connection.cursor()
51+
cursor.execute("PRAGMA foreign_keys=ON")
52+
cursor.close()
5153

5254

5355
class DbCodeGate:
@@ -78,6 +80,7 @@ def __init__(self, sqlite_path: Optional[str] = None):
7880
"isolation_level": "AUTOCOMMIT", # Required for SQLite
7981
}
8082
self._async_db_engine = create_async_engine(**engine_dict)
83+
self._async_db_engine.connect().connection.connection_record.info['enable_fks'] = True
8184

8285
def does_db_exist(self):
8386
return self._db_path.is_file()

0 commit comments

Comments
 (0)