internal/store/migrations/0033_owner_guards.up.sql
14 lines · 677 bytes
1-- repos.owner_id is polymorphic over users and orgs, so no foreign key
2-- can hold it; DeleteUser and DeleteOrg refuse while repositories remain.
3-- These triggers make that refusal structural: a direct or buggy delete
4-- cannot orphan a repository either.
5CREATE TRIGGER users_owning_repos BEFORE DELETE ON users
6WHEN EXISTS (SELECT 1 FROM repos WHERE owner_kind = 'user' AND owner_id = OLD.id)
7BEGIN
8 SELECT RAISE(ABORT, 'user still owns repositories');
9END;
10CREATE TRIGGER orgs_owning_repos BEFORE DELETE ON orgs
11WHEN EXISTS (SELECT 1 FROM repos WHERE owner_kind = 'org' AND owner_id = OLD.id)
12BEGIN
13 SELECT RAISE(ABORT, 'organization still owns repositories');
14END;