tenseleyflow/shithub / edda2d6

Browse files

M: move rename.go raw SQL to sqlc (DeleteRedirectByUserOwnerOldName)

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
edda2d6a8b723e35caad2cde87acfdee593d6bee
Parents
20ffd30
Tree
bc86176

4 changed files

StatusFile+-
M internal/repos/lifecycle/rename.go 5 4
M internal/repos/queries/lifecycle.sql 7 0
M internal/repos/sqlc/lifecycle.sql.go 19 0
M internal/repos/sqlc/querier.go 4 0
internal/repos/lifecycle/rename.gomodified
@@ -128,10 +128,11 @@ func compensateRename(ctx context.Context, deps Deps, repoID int64, oldName stri
128
 	}
128
 	}
129
 	// Drop the redirect row we just wrote — it now points at a name
129
 	// Drop the redirect row we just wrote — it now points at a name
130
 	// that's about to come back to its old self.
130
 	// that's about to come back to its old self.
131
-	_, err := deps.Pool.Exec(ctx,
131
+	if err := rq.DeleteRedirectByUserOwnerOldName(ctx, deps.Pool, reposdb.DeleteRedirectByUserOwnerOldNameParams{
132
-		`DELETE FROM repo_redirects WHERE repo_id = $1 AND old_owner_user_id = $2 AND old_name = $3`,
132
+		RepoID:         repoID,
133
-		repoID, ownerUserID, oldName)
133
+		OldOwnerUserID: pgtype.Int8{Int64: ownerUserID, Valid: ownerUserID != 0},
134
-	if err != nil && deps.Logger != nil {
134
+		OldName:        oldName,
135
+	}); err != nil && deps.Logger != nil {
135
 		deps.Logger.WarnContext(ctx, "rename: compensating redirect-delete failed", "repo_id", repoID, "error", err)
136
 		deps.Logger.WarnContext(ctx, "rename: compensating redirect-delete failed", "repo_id", repoID, "error", err)
136
 	}
137
 	}
137
 	_ = newName // kept in signature for symmetry / future logging
138
 	_ = newName // kept in signature for symmetry / future logging
internal/repos/queries/lifecycle.sqlmodified
@@ -63,6 +63,13 @@ WHERE old_owner_user_id = $1 AND old_name = $2;
63
 -- CASCADE would handle it, but explicit is auditable).
63
 -- CASCADE would handle it, but explicit is auditable).
64
 DELETE FROM repo_redirects WHERE repo_id = $1;
64
 DELETE FROM repo_redirects WHERE repo_id = $1;
65
 
65
 
66
+-- name: DeleteRedirectByUserOwnerOldName :exec
67
+-- Used by the rename compensator: drop a single redirect row when
68
+-- the rename has to be rolled back due to a filesystem failure. We
69
+-- avoided raw SQL here at the audit's request (S00-S25, M).
70
+DELETE FROM repo_redirects
71
+WHERE repo_id = $1 AND old_owner_user_id = $2 AND old_name = $3;
72
+
66
 
73
 
67
 -- ─── transfer requests ─────────────────────────────────────────────────
74
 -- ─── transfer requests ─────────────────────────────────────────────────
68
 
75
 
internal/repos/sqlc/lifecycle.sql.gomodified
@@ -71,6 +71,25 @@ func (q *Queries) DeclineTransferRequest(ctx context.Context, db DBTX, id int64)
71
 	return err
71
 	return err
72
 }
72
 }
73
 
73
 
74
+const deleteRedirectByUserOwnerOldName = `-- name: DeleteRedirectByUserOwnerOldName :exec
75
+DELETE FROM repo_redirects
76
+WHERE repo_id = $1 AND old_owner_user_id = $2 AND old_name = $3
77
+`
78
+
79
+type DeleteRedirectByUserOwnerOldNameParams struct {
80
+	RepoID         int64
81
+	OldOwnerUserID pgtype.Int8
82
+	OldName        string
83
+}
84
+
85
+// Used by the rename compensator: drop a single redirect row when
86
+// the rename has to be rolled back due to a filesystem failure. We
87
+// avoided raw SQL here at the audit's request (S00-S25, M).
88
+func (q *Queries) DeleteRedirectByUserOwnerOldName(ctx context.Context, db DBTX, arg DeleteRedirectByUserOwnerOldNameParams) error {
89
+	_, err := db.Exec(ctx, deleteRedirectByUserOwnerOldName, arg.RepoID, arg.OldOwnerUserID, arg.OldName)
90
+	return err
91
+}
92
+
74
 const deleteRedirectsForRepo = `-- name: DeleteRedirectsForRepo :exec
93
 const deleteRedirectsForRepo = `-- name: DeleteRedirectsForRepo :exec
75
 DELETE FROM repo_redirects WHERE repo_id = $1
94
 DELETE FROM repo_redirects WHERE repo_id = $1
76
 `
95
 `
internal/repos/sqlc/querier.gomodified
@@ -24,6 +24,10 @@ type Querier interface {
24
 	CreateRepo(ctx context.Context, db DBTX, arg CreateRepoParams) (Repo, error)
24
 	CreateRepo(ctx context.Context, db DBTX, arg CreateRepoParams) (Repo, error)
25
 	DeclineTransferRequest(ctx context.Context, db DBTX, id int64) error
25
 	DeclineTransferRequest(ctx context.Context, db DBTX, id int64) error
26
 	DeleteBranchProtectionRule(ctx context.Context, db DBTX, id int64) error
26
 	DeleteBranchProtectionRule(ctx context.Context, db DBTX, id int64) error
27
+	// Used by the rename compensator: drop a single redirect row when
28
+	// the rename has to be rolled back due to a filesystem failure. We
29
+	// avoided raw SQL here at the audit's request (S00-S25, M).
30
+	DeleteRedirectByUserOwnerOldName(ctx context.Context, db DBTX, arg DeleteRedirectByUserOwnerOldNameParams) error
27
 	// Used by hard-delete: drop the redirect rows pointing at this repo
31
 	// Used by hard-delete: drop the redirect rows pointing at this repo
28
 	// (they would dangle once the repos row is gone; the FK ON DELETE
32
 	// (they would dangle once the repos row is gone; the FK ON DELETE
29
 	// CASCADE would handle it, but explicit is auditable).
33
 	// CASCADE would handle it, but explicit is auditable).