Skip to content

Commit

Permalink
fix(endpoints): delete extension on cascade
Browse files Browse the repository at this point in the history
Deleting was impossible after adding extension mapping support since
foreign keys were referencing the endpoint. The database structure was
updated to handle such cases and delete extensions on CASCADE. The
implementation was also changed to use endpoint sid instead of ID.
  • Loading branch information
crazybolillo committed Aug 22, 2024
1 parent 9a9b285 commit be24330
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
14 changes: 14 additions & 0 deletions db/migrations/20240819195341_ery_extension_cascade.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- migrate:up
ALTER TABLE ery_extension DROP CONSTRAINT ery_extension_endpoint_id_fkey;

ALTER TABLE
ery_extension
ADD CONSTRAINT
ry_extension_endpoint_id_fkey
FOREIGN KEY
(endpoint_id)
REFERENCES
ps_endpoints(sid)
ON DELETE CASCADE;

-- migrate:down
9 changes: 4 additions & 5 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ paths:
summary: Create a new endpoint.
tags:
- endpoints
/endpoints/{id}:
/endpoints/{sid}:
delete:
parameters:
- description: ID of the endpoint to be deleted
- description: Sid of the endpoint to be deleted
in: path
name: id
name: sid
required: true
type: string
type: integer
responses:
"204":
description: No Content
Expand All @@ -183,7 +183,6 @@ paths:
summary: Delete an endpoint and its associated resources.
tags:
- endpoints
/endpoints/{sid}:
get:
parameters:
- description: Requested endpoint's sid
Expand Down
13 changes: 7 additions & 6 deletions internal/handler/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (e *Endpoint) Router() chi.Router {
r.Post("/", e.create)
r.Get("/", e.list)
r.Get("/{sid}", e.get)
r.Delete("/{id}", e.delete)
r.Delete("/{sid}", e.delete)

return r
}
Expand Down Expand Up @@ -348,15 +348,16 @@ func (e *Endpoint) create(w http.ResponseWriter, r *http.Request) {
}

// @Summary Delete an endpoint and its associated resources.
// @Param id path string true "ID of the endpoint to be deleted"
// @Param sid path int true "Sid of the endpoint to be deleted"
// @Success 204
// @Failure 400
// @Failure 500
// @Tags endpoints
// @Router /endpoints/{id} [delete]
// @Router /endpoints/{sid} [delete]
func (e *Endpoint) delete(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
if id == "" {
urlSid := chi.URLParam(r, "sid")
sid, err := strconv.Atoi(urlSid)
if err != nil || sid <= 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
Expand All @@ -370,7 +371,7 @@ func (e *Endpoint) delete(w http.ResponseWriter, r *http.Request) {

queries := sqlc.New(tx)

err = queries.DeleteEndpoint(r.Context(), id)
id, err := queries.DeleteEndpoint(r.Context(), int32(sid))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand Down
12 changes: 7 additions & 5 deletions internal/sqlc/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ VALUES
($1, $2, $1, $1, $3, 'all', $4, $5)
RETURNING sid;

-- name: DeleteEndpoint :exec
DELETE FROM ps_endpoints WHERE id = $1;
-- name: DeleteEndpoint :one
DELETE FROM ps_endpoints WHERE sid = $1 RETURNING id;

-- name: DeleteAOR :exec
DELETE FROM ps_aors WHERE id = $1;
Expand Down

0 comments on commit be24330

Please sign in to comment.