Skip to content

Commit 69dd5fa

Browse files
committed
fix migrations
1 parent 51ad349 commit 69dd5fa

2 files changed

Lines changed: 52 additions & 25 deletions

File tree

cabotage/server/models/audit.py

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,70 +60,84 @@ class AuditLog(Model):
6060
AUDIT_LOG_VIEW_SQL = """\
6161
CREATE OR REPLACE VIEW audit_log AS
6262
63-
-- Configuration
64-
SELECT a.id, t.issued_at AS timestamp,
65-
a.verb, a.data->>'action' AS detail, a.object_type, a.object_id, cfg.name AS object_name,
66-
cfg.application_id, cfg.application_environment_id,
63+
-- Configuration (join version table so deletes are visible)
64+
SELECT a.id, t.issued_at AS timestamp, a.object_tx_id, a.transaction_id,
65+
a.verb, a.data->>'action' AS detail, a.object_type, a.object_id, cfgv.name AS object_name,
66+
cfgv.application_id, cfgv.application_environment_id, COALESCE(cfg_app.project_id, cfg_appv.project_id) AS project_id, COALESCE(cfg_proj.organization_id, cfg_projv.organization_id) AS organization_id,
67+
COALESCE(cfg_app.name, cfg_appv.name) AS app_name, COALESCE(cfg_proj.name, cfg_projv.name) AS project_name,
6768
COALESCE(u.username, tx_u.username) AS actor_username, COALESCE(u.email, tx_u.email) AS actor_email, t.remote_addr,
68-
cfg.secret AS config_secret, cfg.buildtime AS config_buildtime, cfg.version_id AS config_version,
69+
cfgv.secret AS config_secret, cfgv.buildtime AS config_buildtime, cfgv.version_id AS config_version,
6970
NULL::text AS image_ref, NULL::text AS image_sha,
7071
NULL::integer AS deploy_release_version,
7172
a.data AS raw_data
7273
FROM activity a
73-
JOIN project_app_configurations cfg ON a.object_type = 'Configuration' AND cfg.id = a.object_id
74+
JOIN project_app_configurations_version cfgv ON a.object_type = 'Configuration' AND cfgv.id = a.object_id AND cfgv.transaction_id = a.object_tx_id
75+
LEFT JOIN project_applications cfg_app ON cfg_app.id = cfgv.application_id
76+
LEFT JOIN projects cfg_proj ON cfg_proj.id = cfg_app.project_id
77+
LEFT JOIN project_applications_version cfg_appv ON cfg_appv.id = cfgv.application_id AND cfg_appv.transaction_id = (SELECT max(transaction_id) FROM project_applications_version WHERE id = cfgv.application_id)
78+
LEFT JOIN projects_version cfg_projv ON cfg_projv.id = cfg_appv.project_id AND cfg_projv.transaction_id = (SELECT max(transaction_id) FROM projects_version WHERE id = cfg_appv.project_id)
7479
LEFT JOIN transaction t ON t.id = a.transaction_id
7580
LEFT JOIN users u ON u.id::text = a.data->>'user_id'
7681
LEFT JOIN users tx_u ON tx_u.id = t.user_id
7782
7883
UNION ALL
7984
80-
-- Image (exclude complete/error status changes)
81-
SELECT a.id, t.issued_at,
85+
-- Image
86+
SELECT a.id, t.issued_at, a.object_tx_id, a.transaction_id,
8287
a.verb, a.data->>'action', a.object_type, a.object_id, CONCAT('#', img.version),
83-
img.application_id, img.application_environment_id,
88+
img.application_id, img.application_environment_id, img_app.project_id, img_proj.organization_id,
89+
img_app.name, img_proj.name,
8490
COALESCE(u.username, tx_u.username), COALESCE(u.email, tx_u.email), t.remote_addr,
8591
NULL, NULL, NULL,
8692
img.build_ref, img.image_metadata->>'sha',
8793
NULL,
8894
a.data
8995
FROM activity a
9096
JOIN project_app_images img ON a.object_type = 'Image' AND img.id = a.object_id
97+
LEFT JOIN project_applications img_app ON img_app.id = img.application_id
98+
LEFT JOIN projects img_proj ON img_proj.id = img_app.project_id
9199
LEFT JOIN transaction t ON t.id = a.transaction_id
92100
LEFT JOIN users u ON u.id::text = a.data->>'user_id'
93101
LEFT JOIN users tx_u ON tx_u.id = t.user_id
94102
WHERE a.verb NOT IN ('complete', 'error')
95103
96104
UNION ALL
97105
98-
-- Release (exclude complete/error status changes)
99-
SELECT a.id, t.issued_at,
106+
-- Release
107+
SELECT a.id, t.issued_at, a.object_tx_id, a.transaction_id,
100108
a.verb, a.data->>'action', a.object_type, a.object_id, CONCAT('v', rel.version),
101-
rel.application_id, rel.application_environment_id,
109+
rel.application_id, rel.application_environment_id, rel_app.project_id, rel_proj.organization_id,
110+
rel_app.name, rel_proj.name,
102111
COALESCE(u.username, tx_u.username), COALESCE(u.email, tx_u.email), t.remote_addr,
103112
NULL, NULL, NULL,
104113
NULL, NULL,
105114
NULL,
106115
a.data
107116
FROM activity a
108117
JOIN project_app_releases rel ON a.object_type = 'Release' AND rel.id = a.object_id
118+
LEFT JOIN project_applications rel_app ON rel_app.id = rel.application_id
119+
LEFT JOIN projects rel_proj ON rel_proj.id = rel_app.project_id
109120
LEFT JOIN transaction t ON t.id = a.transaction_id
110121
LEFT JOIN users u ON u.id::text = a.data->>'user_id'
111122
LEFT JOIN users tx_u ON tx_u.id = t.user_id
112123
WHERE a.verb NOT IN ('complete', 'error')
113124
114125
UNION ALL
115126
116-
-- Deployment (exclude complete/error)
117-
SELECT a.id, t.issued_at,
127+
-- Deployment
128+
SELECT a.id, t.issued_at, a.object_tx_id, a.transaction_id,
118129
a.verb, a.data->>'action', a.object_type, a.object_id, NULL,
119-
dep.application_id, dep.application_environment_id,
130+
dep.application_id, dep.application_environment_id, dep_app.project_id, dep_proj.organization_id,
131+
dep_app.name, dep_proj.name,
120132
COALESCE(u.username, tx_u.username), COALESCE(u.email, tx_u.email), t.remote_addr,
121133
NULL, NULL, NULL,
122134
NULL, NULL,
123135
dep_rel.version,
124136
a.data
125137
FROM activity a
126138
JOIN deployments dep ON a.object_type = 'Deployment' AND dep.id = a.object_id
139+
LEFT JOIN project_applications dep_app ON dep_app.id = dep.application_id
140+
LEFT JOIN projects dep_proj ON dep_proj.id = dep_app.project_id
127141
LEFT JOIN project_app_releases dep_rel ON dep_rel.id::text = dep.release->>'id'
128142
LEFT JOIN transaction t ON t.id = a.transaction_id
129143
LEFT JOIN users u ON u.id::text = a.data->>'user_id'
@@ -133,9 +147,10 @@ class AuditLog(Model):
133147
UNION ALL
134148
135149
-- Ingress
136-
SELECT a.id, t.issued_at,
150+
SELECT a.id, t.issued_at, a.object_tx_id, a.transaction_id,
137151
a.verb, a.data->>'action', a.object_type, a.object_id, ing.name,
138-
ing_ae.application_id, ing.application_environment_id,
152+
ing_ae.application_id, ing.application_environment_id, ing_app.project_id, ing_proj.organization_id,
153+
ing_app.name, ing_proj.name,
139154
COALESCE(u.username, tx_u.username), COALESCE(u.email, tx_u.email), t.remote_addr,
140155
NULL, NULL, NULL,
141156
NULL, NULL,
@@ -144,51 +159,61 @@ class AuditLog(Model):
144159
FROM activity a
145160
JOIN ingresses ing ON a.object_type = 'Ingress' AND ing.id = a.object_id
146161
LEFT JOIN application_environments ing_ae ON ing.application_environment_id = ing_ae.id
162+
LEFT JOIN project_applications ing_app ON ing_app.id = ing_ae.application_id
163+
LEFT JOIN projects ing_proj ON ing_proj.id = ing_app.project_id
147164
LEFT JOIN transaction t ON t.id = a.transaction_id
148165
LEFT JOIN users u ON u.id::text = a.data->>'user_id'
149166
LEFT JOIN users tx_u ON tx_u.id = t.user_id
150167
151168
UNION ALL
152169
153170
-- Application
154-
SELECT a.id, t.issued_at,
171+
SELECT a.id, t.issued_at, a.object_tx_id, a.transaction_id,
155172
a.verb, a.data->>'action', a.object_type, a.object_id, app.name,
156-
app.id, NULL::uuid,
173+
app.id, NULL::uuid, app.project_id, proj.organization_id,
174+
app.name, proj.name,
157175
COALESCE(u.username, tx_u.username), COALESCE(u.email, tx_u.email), t.remote_addr,
158176
NULL, NULL, NULL,
159177
NULL, NULL,
160178
NULL,
161179
a.data
162180
FROM activity a
163181
JOIN project_applications app ON a.object_type = 'Application' AND app.id = a.object_id
182+
LEFT JOIN projects proj ON proj.id = app.project_id
164183
LEFT JOIN transaction t ON t.id = a.transaction_id
165184
LEFT JOIN users u ON u.id::text = a.data->>'user_id'
166185
LEFT JOIN users tx_u ON tx_u.id = t.user_id
167186
168187
UNION ALL
169188
170189
-- Alert (firing, resolved, etc.)
171-
SELECT a.id, t.issued_at,
190+
SELECT a.id, t.issued_at, a.object_tx_id, a.transaction_id,
172191
a.verb, a.data->>'action', a.object_type, a.object_id, a.data->>'alertname',
173-
alert.application_id, alert.application_environment_id,
192+
alert.application_id, alert.application_environment_id, alert_app.project_id, alert_proj.organization_id,
193+
alert_app.name, alert_proj.name,
174194
COALESCE(u.username, tx_u.username), COALESCE(u.email, tx_u.email), t.remote_addr,
175195
NULL, NULL, NULL,
176196
NULL, NULL,
177197
NULL,
178198
a.data
179199
FROM activity a
180200
JOIN alerts alert ON a.object_type = 'Alert' AND alert.id = a.object_id
201+
LEFT JOIN project_applications alert_app ON alert_app.id = alert.application_id
202+
LEFT JOIN projects alert_proj ON alert_proj.id = alert_app.project_id
181203
LEFT JOIN transaction t ON t.id = a.transaction_id
182204
LEFT JOIN users u ON u.id::text = a.data->>'user_id'
183205
LEFT JOIN users tx_u ON tx_u.id = t.user_id
184206
185207
UNION ALL
186208
187209
-- Other (ApplicationEnvironment, Organization, Environment, User, Project)
188-
SELECT a.id, t.issued_at,
210+
SELECT a.id, t.issued_at, a.object_tx_id, a.transaction_id,
189211
a.verb, a.data->>'action', a.object_type, a.object_id,
190-
COALESCE(ae_app.name, org.name, env.name, au.username),
212+
COALESCE(ae_app.name, org.name, env.name, au.username, proj_direct.name),
191213
ae.application_id, ae.id,
214+
COALESCE(ae_app.project_id, env.project_id, proj_direct.id),
215+
COALESCE(ae_proj.organization_id, proj_direct.organization_id, org.id),
216+
ae_app.name, COALESCE(ae_proj.name, proj_direct.name),
192217
COALESCE(u.username, tx_u.username), COALESCE(u.email, tx_u.email), t.remote_addr,
193218
NULL, NULL, NULL,
194219
NULL, NULL,
@@ -197,8 +222,10 @@ class AuditLog(Model):
197222
FROM activity a
198223
LEFT JOIN application_environments ae ON a.object_type = 'ApplicationEnvironment' AND ae.id = a.object_id
199224
LEFT JOIN project_applications ae_app ON ae.application_id = ae_app.id
225+
LEFT JOIN projects ae_proj ON ae_proj.id = ae_app.project_id
200226
LEFT JOIN organizations org ON a.object_type = 'Organization' AND org.id = a.object_id
201227
LEFT JOIN project_environments env ON a.object_type = 'Environment' AND env.id = a.object_id
228+
LEFT JOIN projects proj_direct ON a.object_type = 'Project' AND proj_direct.id = a.object_id
202229
LEFT JOIN users au ON a.object_type = 'User' AND au.id = a.object_id
203230
LEFT JOIN transaction t ON t.id = a.transaction_id
204231
LEFT JOIN users u ON u.id::text = a.data->>'user_id'

migrations/versions/5056d1ae059f_add_alerts_table_with_versioning.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""add alerts table with versioning
22
33
Revision ID: 5056d1ae059f
4-
Revises: 1f913a84ab35
4+
Revises: 16eacbf52ea4
55
Create Date: 2026-03-30 21:11:44.838069
66
77
"""
@@ -12,7 +12,7 @@
1212

1313
# revision identifiers, used by Alembic.
1414
revision = "5056d1ae059f"
15-
down_revision = "1f913a84ab35"
15+
down_revision = "16eacbf52ea4"
1616
branch_labels = None
1717
depends_on = None
1818

0 commit comments

Comments
 (0)