This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
Introduce ON_DELTE_CASCADE condition to all FKs #675
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
migrations/versions/2025_01_21_0820-4dec3e456c9e_add_on_delete_cascade.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
"""add_on_delete_cascade | ||
|
||
Revision ID: 4dec3e456c9e | ||
Revises: e6227073183d | ||
Create Date: 2025-01-21 08:20:12.221051+00:00 | ||
|
||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "4dec3e456c9e" | ||
down_revision: Union[str, None] = "e6227073183d" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# To add ON DELETE CASCADE to the foreign key constraint, we need to | ||
# rename the table, create a new table with the constraint, and copy | ||
# the data over. | ||
op.execute("ALTER TABLE prompts RENAME TO _prompts_old;") | ||
op.execute( | ||
""" | ||
CREATE TABLE prompts ( | ||
id TEXT PRIMARY KEY, -- UUID stored as TEXT | ||
timestamp DATETIME NOT NULL, | ||
provider TEXT, -- VARCHAR(255) | ||
request TEXT NOT NULL, -- Record the full request that arrived to the server | ||
type TEXT NOT NULL, -- VARCHAR(50) (e.g. "fim", "chat") | ||
workspace_id TEXT NOT NULL, | ||
FOREIGN KEY (workspace_id) REFERENCES workspaces(id) ON DELETE CASCADE | ||
); | ||
""" | ||
) | ||
op.execute("INSERT INTO prompts SELECT * FROM _prompts_old;") | ||
op.execute("DROP TABLE _prompts_old;") | ||
|
||
# Doing the same for the sessions table | ||
op.execute("ALTER TABLE sessions RENAME TO _sessions_old;") | ||
op.execute( | ||
""" | ||
CREATE TABLE sessions ( | ||
id TEXT PRIMARY KEY, -- UUID stored as TEXT | ||
active_workspace_id TEXT NOT NULL, | ||
last_update DATETIME NOT NULL, | ||
FOREIGN KEY (active_workspace_id) REFERENCES workspaces(id) ON DELETE CASCADE | ||
); | ||
""" | ||
) | ||
op.execute("INSERT INTO sessions SELECT * FROM _sessions_old;") | ||
op.execute("DROP TABLE _sessions_old;") | ||
|
||
# Doing the same for the output table | ||
op.execute("ALTER TABLE outputs RENAME TO _outputs_old;") | ||
op.execute( | ||
""" | ||
CREATE TABLE outputs ( | ||
id TEXT PRIMARY KEY, -- UUID stored as TEXT | ||
prompt_id TEXT NOT NULL, | ||
timestamp DATETIME NOT NULL, | ||
output TEXT NOT NULL, -- Record the full response. If stream will be a list of objects | ||
FOREIGN KEY (prompt_id) REFERENCES prompts(id) ON DELETE CASCADE | ||
); | ||
""" | ||
) | ||
op.execute("INSERT INTO outputs SELECT * FROM _outputs_old;") | ||
op.execute("DROP TABLE _outputs_old;") | ||
|
||
# Doing the same for the alerts table | ||
op.execute("ALTER TABLE alerts RENAME TO _alerts_old;") | ||
op.execute( | ||
""" | ||
CREATE TABLE alerts ( | ||
id TEXT PRIMARY KEY, -- UUID stored as TEXT | ||
prompt_id TEXT NOT NULL, | ||
code_snippet TEXT, | ||
trigger_string TEXT, -- VARCHAR(255) | ||
trigger_type TEXT NOT NULL, -- VARCHAR(50) | ||
trigger_category TEXT, | ||
timestamp DATETIME NOT NULL, | ||
FOREIGN KEY (prompt_id) REFERENCES prompts(id) ON DELETE CASCADE | ||
); | ||
""" | ||
) | ||
op.execute("INSERT INTO alerts SELECT * FROM _alerts_old;") | ||
op.execute("DROP TABLE _alerts_old;") | ||
|
||
# Dropping unused table | ||
op.execute("DROP TABLE settings;") | ||
|
||
# Create indexes for foreign keys | ||
op.execute("CREATE INDEX idx_outputs_prompt_id ON outputs(prompt_id);") | ||
op.execute("CREATE INDEX idx_alerts_prompt_id ON alerts(prompt_id);") | ||
op.execute("CREATE INDEX idx_prompts_workspace_id ON prompts (workspace_id);") | ||
op.execute("CREATE INDEX idx_sessions_workspace_id ON sessions (active_workspace_id);") | ||
|
||
|
||
def downgrade() -> None: | ||
# Settings table | ||
op.execute( | ||
""" | ||
CREATE TABLE settings ( | ||
id TEXT PRIMARY KEY, -- UUID stored as TEXT | ||
ip TEXT, -- VARCHAR(45) | ||
port INTEGER, | ||
llm_model TEXT, -- VARCHAR(255) | ||
system_prompt TEXT, | ||
other_settings TEXT -- JSON stored as TEXT | ||
); | ||
""" | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.