Content-Length: 469221 | pFad | http://github.com/stacklok/codegate/commit/d56d451d26e300834d7323c17fe18636a723e07c

51 Introduce ON_DELTE_CASCADE condition to all FKs (#675) · stacklok/codegate@d56d451 · GitHub
Skip to content

Commit d56d451

Browse files
Introduce ON_DELTE_CASCADE condition to all FKs (#675)
* Introduce ON_DELTE_CASCADE condition to all FKs Until now we had ON_DELETE_NO_ACTION since it's the default * formatting changes
1 parent 781de22 commit d56d451

File tree

1 file changed

+114
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/stacklok/codegate/commit/d56d451d26e300834d7323c17fe18636a723e07c

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy