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

Commit bf1112b

Browse files
Introduce ON_DELTE_CASCADE condition to all FKs
Until now we had ON_DELETE_NO_ACTION since it's the default
1 parent 781de22 commit bf1112b

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
"""add_on_delete_cascade
2+
3+
Revision ID: 4dec3e456c9e
4+
Revises: e6227073183d
5+
Create Date: 2025-01-21 08:20:12.221051+00:00
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = '4dec3e456c9e'
16+
down_revision: Union[str, None] = 'e6227073183d'
17+
branch_labels: Union[str, Sequence[str], None] = None
18+
depends_on: Union[str, Sequence[str], None] = None
19+
20+
21+
def upgrade() -> None:
22+
# To add ON DELETE CASCADE to the foreign key constraint, we need to
23+
# rename the table, create a new table with the constraint, and copy
24+
# the data over.
25+
op.execute("ALTER TABLE prompts RENAME TO _prompts_old;")
26+
op.execute(
27+
"""
28+
CREATE TABLE prompts (
29+
id TEXT PRIMARY KEY, -- UUID stored as TEXT
30+
timestamp DATETIME NOT NULL,
31+
provider TEXT, -- VARCHAR(255)
32+
request TEXT NOT NULL, -- Record the full request that arrived to the server
33+
type TEXT NOT NULL, -- VARCHAR(50) (e.g. "fim", "chat")
34+
workspace_id TEXT NOT NULL,
35+
FOREIGN KEY (workspace_id) REFERENCES workspaces(id) ON DELETE CASCADE
36+
);
37+
"""
38+
)
39+
op.execute("INSERT INTO prompts SELECT * FROM _prompts_old;")
40+
op.execute("DROP TABLE _prompts_old;")
41+
42+
# Doing the same for the sessions table
43+
op.execute("ALTER TABLE sessions RENAME TO _sessions_old;")
44+
op.execute(
45+
"""
46+
CREATE TABLE sessions (
47+
id TEXT PRIMARY KEY, -- UUID stored as TEXT
48+
active_workspace_id TEXT NOT NULL,
49+
last_update DATETIME NOT NULL,
50+
FOREIGN KEY (active_workspace_id) REFERENCES workspaces(id) ON DELETE CASCADE
51+
);
52+
"""
53+
)
54+
op.execute("INSERT INTO sessions SELECT * FROM _sessions_old;")
55+
op.execute("DROP TABLE _sessions_old;")
56+
57+
# Doing the same for the output table
58+
op.execute("ALTER TABLE outputs RENAME TO _outputs_old;")
59+
op.execute(
60+
"""
61+
CREATE TABLE outputs (
62+
id TEXT PRIMARY KEY, -- UUID stored as TEXT
63+
prompt_id TEXT NOT NULL,
64+
timestamp DATETIME NOT NULL,
65+
output TEXT NOT NULL, -- Record the full response. If stream will be a list of objects
66+
FOREIGN KEY (prompt_id) REFERENCES prompts(id) ON DELETE CASCADE
67+
);
68+
"""
69+
)
70+
op.execute("INSERT INTO outputs SELECT * FROM _outputs_old;")
71+
op.execute("DROP TABLE _outputs_old;")
72+
73+
# Doing the same for the alerts table
74+
op.execute("ALTER TABLE alerts RENAME TO _alerts_old;")
75+
op.execute(
76+
"""
77+
CREATE TABLE alerts (
78+
id TEXT PRIMARY KEY, -- UUID stored as TEXT
79+
prompt_id TEXT NOT NULL,
80+
code_snippet TEXT,
81+
trigger_string TEXT, -- VARCHAR(255)
82+
trigger_type TEXT NOT NULL, -- VARCHAR(50)
83+
trigger_category TEXT,
84+
timestamp DATETIME NOT NULL,
85+
FOREIGN KEY (prompt_id) REFERENCES prompts(id) ON DELETE CASCADE
86+
);
87+
"""
88+
)
89+
op.execute("INSERT INTO alerts SELECT * FROM _alerts_old;")
90+
op.execute("DROP TABLE _alerts_old;")
91+
92+
# Dropping unused table
93+
op.execute("DROP TABLE settings;")
94+
95+
# Create indexes for foreign keys
96+
op.execute("CREATE INDEX idx_outputs_prompt_id ON outputs(prompt_id);")
97+
op.execute("CREATE INDEX idx_alerts_prompt_id ON alerts(prompt_id);")
98+
op.execute("CREATE INDEX idx_prompts_workspace_id ON prompts (workspace_id);")
99+
op.execute("CREATE INDEX idx_sessions_workspace_id ON sessions (active_workspace_id);")
100+
101+
102+
def downgrade() -> None:
103+
# Settings table
104+
op.execute(
105+
"""
106+
CREATE TABLE settings (
107+
id TEXT PRIMARY KEY, -- UUID stored as TEXT
108+
ip TEXT, -- VARCHAR(45)
109+
port INTEGER,
110+
llm_model TEXT, -- VARCHAR(255)
111+
system_prompt TEXT,
112+
other_settings TEXT -- JSON stored as TEXT
113+
);
114+
"""
115+
)

0 commit comments

Comments
 (0)