Commit 10e4f50
authored
fix(db): make seed-db script idempotent by cleaning up FK dependencies (#2383)
The seed script failed on repeated runs with a foreign key violation:
"envs_teams_envs" on table "envs" (SQLSTATE 23503)
This happened because DELETE FROM teams was attempted while rows in
envs, snapshots, and volumes still referenced the team via ON DELETE
NO ACTION constraints. Similarly, deleting auth.users could be blocked
by addons.added_by (also ON DELETE NO ACTION).
Fix the deletion order to explicitly remove blocking child rows before
deleting the parent team and user:
1. DELETE envs for the team
2. DELETE snapshots for the team
3. DELETE volumes for the team
4. DELETE addons for the user
5. DELETE teams (team_api_keys, users_teams cascade automatically)
6. DELETE auth.users (access_tokens cascade automatically)
7. Re-create user, team, and associated credentials
Tables with ON DELETE CASCADE/SET NULL (team_api_keys, users_teams,
access_tokens, env_aliases, env_builds, etc.) are cleaned up
automatically and do not need explicit deletes.
Additionally, inserting into auth.users fires a trigger chain
(sync_inserts_to_public_users -> post_user_signup) that auto-creates
a default team. Delete that trigger-created team before inserting the
seed team to avoid leaving duplicate teams for the same user.1 parent 93261e6 commit 10e4f50
1 file changed
Lines changed: 46 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
81 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
82 | 120 | | |
83 | 121 | | |
84 | 122 | | |
| |||
87 | 125 | | |
88 | 126 | | |
89 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
90 | 132 | | |
91 | 133 | | |
92 | 134 | | |
| |||
96 | 138 | | |
97 | 139 | | |
98 | 140 | | |
99 | | - | |
| 141 | + | |
| 142 | + | |
100 | 143 | | |
101 | 144 | | |
102 | 145 | | |
| |||
0 commit comments