migrations: 0028 — backfill ON DELETE clauses on legacy FKs

Retrofits the ON DELETE pattern learned after v1 shipped (CASCADE for
tenant-scoped children, SET NULL for historical references, RESTRICT where
the domain type is non-Option) onto tables from 0001-0006 and 0026. Two
deliberate exceptions kept as NO ACTION: audit_log.workspace_id (audit is
append-only and must outlive workspace deletes) and thread_messages.from_agent
(history stays attributable via agents.deleted_at). Two stay NOT NULL as
RESTRICT (agents.managed_by, installed_skills.installed_by) because the
Rust domain type is UserId, not Option<UserId>.

Also includes the previously-orphan .sqlx cache for the usage_events query
in cm-runtime/tests/run_loop.rs, which needed re-recording after the FK
changes touched the metadata.
This commit is contained in:
Omar Sobh
2026-07-05 18:48:48 -07:00
parent 51d501e36e
commit e5e049921f
2 changed files with 235 additions and 0 deletions
@@ -0,0 +1,34 @@
{
"db_name": "PostgreSQL",
"query": "SELECT tokens_in, tokens_out, credits FROM usage_events\n WHERE workspace_id = $1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "tokens_in",
"type_info": "Int8"
},
{
"ordinal": 1,
"name": "tokens_out",
"type_info": "Int8"
},
{
"ordinal": 2,
"name": "credits",
"type_info": "Numeric"
}
],
"parameters": {
"Left": [
"Uuid"
]
},
"nullable": [
false,
false,
false
]
},
"hash": "47c1cee8591250819d22e87058c6011bebb77eaf93c1cfa2535db42221d8ecf3"
}