internal/store/migrations/0033_owner_guards.up.sql

0666c9f622171df00f8e2c79a13d011522039624
gitbay/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;
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form