@@ -77,8 +77,46 @@ func main() {
7777 }
7878 defer authDb .Close ()
7979
80- // Open .e2b/config.json
81- // Delete existing user and recreate (simpler for seeding)
80+ // Clean up existing data for idempotent re-seeding.
81+ // Delete child rows that have ON DELETE NO ACTION constraints
82+ // before deleting the team and user.
83+ err = authDb .TestsRawSQL (ctx , `
84+ DELETE FROM envs WHERE team_id IN (SELECT id FROM teams WHERE email = $1)
85+ ` , email )
86+ if err != nil {
87+ panic (err )
88+ }
89+
90+ err = authDb .TestsRawSQL (ctx , `
91+ DELETE FROM snapshots WHERE team_id IN (SELECT id FROM teams WHERE email = $1)
92+ ` , email )
93+ if err != nil {
94+ panic (err )
95+ }
96+
97+ err = authDb .TestsRawSQL (ctx , `
98+ DELETE FROM volumes WHERE team_id IN (SELECT id FROM teams WHERE email = $1)
99+ ` , email )
100+ if err != nil {
101+ panic (err )
102+ }
103+
104+ err = authDb .TestsRawSQL (ctx , `
105+ DELETE FROM addons WHERE added_by IN (SELECT id FROM auth.users WHERE email = $1)
106+ ` , email )
107+ if err != nil {
108+ panic (err )
109+ }
110+
111+ // Now safe to delete team (team_api_keys, users_teams cascade automatically)
112+ err = authDb .TestsRawSQL (ctx , `
113+ DELETE FROM teams WHERE email = $1
114+ ` , email )
115+ if err != nil {
116+ panic (err )
117+ }
118+
119+ // Now safe to delete user (access_tokens cascade automatically)
82120 err = authDb .TestsRawSQL (ctx , `
83121DELETE FROM auth.users WHERE email = $1
84122` , email )
@@ -87,6 +125,10 @@ DELETE FROM auth.users WHERE email = $1
87125 }
88126
89127 // Create the user
128+ // NOTE: Inserting into auth.users fires the sync_inserts_to_public_users trigger,
129+ // which inserts into public.users, which fires the post_user_signup trigger,
130+ // which auto-creates a default team + users_teams row. We delete that
131+ // trigger-created team below so we can create our own with a known ID/name.
90132 userID := uuid .New ()
91133 err = authDb .TestsRawSQL (ctx , `
92134INSERT INTO auth.users (id, email)
@@ -96,7 +138,8 @@ VALUES ($1, $2)
96138 panic (err )
97139 }
98140
99- // Delete team
141+ // Remove the team auto-created by the post_user_signup trigger so we can
142+ // create our own seed team with a deterministic ID and custom name/slug.
100143 err = authDb .TestsRawSQL (ctx , `
101144DELETE FROM teams WHERE email = $1
102145` , email )
0 commit comments