Make framework reference id uniq
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -16,6 +16,7 @@ package coredata
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
"maps"
|
||||||
"time"
|
"time"
|
||||||
@@ -23,6 +24,7 @@ import (
|
|||||||
"github.com/getprobo/probo/pkg/gid"
|
"github.com/getprobo/probo/pkg/gid"
|
||||||
"github.com/getprobo/probo/pkg/page"
|
"github.com/getprobo/probo/pkg/page"
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
"go.gearno.de/kit/pg"
|
"go.gearno.de/kit/pg"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,8 +40,17 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
Frameworks []*Framework
|
Frameworks []*Framework
|
||||||
|
|
||||||
|
ErrFrameworkReferenceIDAlreadyExists struct {
|
||||||
|
ReferenceID string
|
||||||
|
OrganizationID gid.GID
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (e ErrFrameworkReferenceIDAlreadyExists) Error() string {
|
||||||
|
return fmt.Sprintf("framework with reference ID %q already exists for organization %s", e.ReferenceID, e.OrganizationID)
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Framework) CursorKey(orderBy FrameworkOrderField) page.CursorKey {
|
func (f *Framework) CursorKey(orderBy FrameworkOrderField) page.CursorKey {
|
||||||
switch orderBy {
|
switch orderBy {
|
||||||
case FrameworkOrderFieldCreatedAt:
|
case FrameworkOrderFieldCreatedAt:
|
||||||
@@ -175,7 +186,22 @@ VALUES (
|
|||||||
"updated_at": f.UpdatedAt,
|
"updated_at": f.UpdatedAt,
|
||||||
}
|
}
|
||||||
_, err := conn.Exec(ctx, q, args)
|
_, err := conn.Exec(ctx, q, args)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
var pgErr *pgconn.PgError
|
||||||
|
if errors.As(err, &pgErr) {
|
||||||
|
if pgErr.Code == "23505" && pgErr.ConstraintName == "frameworks_org_ref_unique" {
|
||||||
|
return &ErrFrameworkReferenceIDAlreadyExists{
|
||||||
|
ReferenceID: f.ReferenceID,
|
||||||
|
OrganizationID: f.OrganizationID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f Framework) Delete(
|
func (f Framework) Delete(
|
||||||
|
|||||||
9
pkg/coredata/migrations/20250331T150600Z.sql
Normal file
9
pkg/coredata/migrations/20250331T150600Z.sql
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
ALTER TABLE frameworks
|
||||||
|
ALTER COLUMN reference_id SET NOT NULL,
|
||||||
|
DROP CONSTRAINT IF EXISTS frameworks_reference_id_unique,
|
||||||
|
ADD CONSTRAINT frameworks_org_ref_unique UNIQUE (organization_id, reference_id);
|
||||||
|
|
||||||
|
ALTER TABLE controls
|
||||||
|
ALTER COLUMN reference_id DROP DEFAULT,
|
||||||
|
DROP CONSTRAINT IF EXISTS controls_reference_id_unique,
|
||||||
|
ADD CONSTRAINT controls_framework_ref_unique UNIQUE (framework_id, reference_id);
|
||||||
Reference in New Issue
Block a user