Add controls snapshots
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
@@ -30,7 +30,6 @@ type (
|
||||
Control struct {
|
||||
ID gid.GID `db:"id"`
|
||||
SectionTitle string `db:"section_title"`
|
||||
TenantID gid.TenantID `db:"tenant_id"`
|
||||
FrameworkID gid.GID `db:"framework_id"`
|
||||
Name string `db:"name"`
|
||||
Description string `db:"description"`
|
||||
@@ -137,7 +136,6 @@ SELECT
|
||||
id,
|
||||
section_title,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
status,
|
||||
@@ -247,7 +245,6 @@ SELECT
|
||||
id,
|
||||
section_title,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
status,
|
||||
@@ -369,7 +366,6 @@ SELECT
|
||||
id,
|
||||
section_title,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
status,
|
||||
@@ -449,7 +445,6 @@ SELECT
|
||||
id,
|
||||
section_title,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
status,
|
||||
@@ -562,7 +557,6 @@ SELECT
|
||||
id,
|
||||
section_title,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
status,
|
||||
@@ -609,7 +603,6 @@ SELECT
|
||||
id,
|
||||
section_title,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
status,
|
||||
@@ -654,11 +647,10 @@ SELECT
|
||||
id,
|
||||
section_title,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
status,
|
||||
exclusion_justification,
|
||||
exclusion_justification,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -777,7 +769,6 @@ WHERE %s
|
||||
RETURNING
|
||||
id,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
section_title,
|
||||
@@ -903,7 +894,6 @@ SELECT
|
||||
id,
|
||||
section_title,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
status,
|
||||
@@ -937,3 +927,113 @@ WHERE %s
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controls) CountBySnapshotID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
snapshotID gid.GID,
|
||||
filter *ControlFilter,
|
||||
) (int, error) {
|
||||
q := `
|
||||
WITH ctrl AS (
|
||||
SELECT
|
||||
c.id,
|
||||
c.tenant_id,
|
||||
c.search_vector
|
||||
FROM
|
||||
controls c
|
||||
INNER JOIN
|
||||
controls_snapshots cs ON c.id = cs.control_id
|
||||
WHERE
|
||||
cs.snapshot_id = @snapshot_id
|
||||
)
|
||||
SELECT
|
||||
COUNT(id)
|
||||
FROM
|
||||
ctrl
|
||||
WHERE %s
|
||||
AND %s
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"snapshot_id": snapshotID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, filter.SQLArguments())
|
||||
|
||||
row := conn.QueryRow(ctx, q, args)
|
||||
|
||||
var count int
|
||||
if err := row.Scan(&count); err != nil {
|
||||
return 0, fmt.Errorf("cannot scan count: %w", err)
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (c *Controls) LoadBySnapshotID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
snapshotID gid.GID,
|
||||
cursor *page.Cursor[ControlOrderField],
|
||||
filter *ControlFilter,
|
||||
) error {
|
||||
q := `
|
||||
WITH ctrl AS (
|
||||
SELECT
|
||||
c.id,
|
||||
c.section_title,
|
||||
c.framework_id,
|
||||
c.tenant_id,
|
||||
c.name,
|
||||
c.description,
|
||||
c.status,
|
||||
c.exclusion_justification,
|
||||
c.created_at,
|
||||
c.updated_at,
|
||||
c.search_vector
|
||||
FROM
|
||||
controls c
|
||||
INNER JOIN
|
||||
controls_snapshots cs ON c.id = cs.control_id
|
||||
WHERE
|
||||
cs.snapshot_id = @snapshot_id
|
||||
)
|
||||
SELECT
|
||||
id,
|
||||
section_title,
|
||||
framework_id,
|
||||
name,
|
||||
description,
|
||||
status,
|
||||
exclusion_justification,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
ctrl
|
||||
WHERE %s
|
||||
AND %s
|
||||
AND %s
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"snapshot_id": snapshotID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, filter.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query controls: %w", err)
|
||||
}
|
||||
|
||||
controls, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Control])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect controls: %w", err)
|
||||
}
|
||||
|
||||
*c = controls
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
168
pkg/coredata/control_snapshot.go
Normal file
168
pkg/coredata/control_snapshot.go
Normal file
@@ -0,0 +1,168 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
|
||||
type (
|
||||
ControlSnapshot struct {
|
||||
ControlID gid.GID `db:"control_id"`
|
||||
SnapshotID gid.GID `db:"snapshot_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
|
||||
ControlSnapshots []*ControlSnapshot
|
||||
)
|
||||
|
||||
func (cs ControlSnapshot) Upsert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO
|
||||
controls_snapshots (
|
||||
control_id,
|
||||
snapshot_id,
|
||||
tenant_id,
|
||||
created_at
|
||||
)
|
||||
VALUES (
|
||||
@control_id,
|
||||
@snapshot_id,
|
||||
@tenant_id,
|
||||
@created_at
|
||||
)
|
||||
ON CONFLICT (control_id, snapshot_id) DO NOTHING;
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"control_id": cs.ControlID,
|
||||
"snapshot_id": cs.SnapshotID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"created_at": cs.CreatedAt,
|
||||
}
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
}
|
||||
|
||||
func (cs ControlSnapshot) Delete(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
controlID gid.GID,
|
||||
snapshotID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
DELETE
|
||||
FROM
|
||||
controls_snapshots
|
||||
WHERE
|
||||
%s
|
||||
AND control_id = @control_id
|
||||
AND snapshot_id = @snapshot_id;
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"control_id": controlID,
|
||||
"snapshot_id": snapshotID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
}
|
||||
|
||||
func (css *ControlSnapshots) LoadByControlID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
controlID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
control_id,
|
||||
snapshot_id,
|
||||
created_at
|
||||
FROM
|
||||
controls_snapshots
|
||||
WHERE
|
||||
%s
|
||||
AND control_id = @control_id
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"control_id": controlID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query controls_snapshots: %w", err)
|
||||
}
|
||||
|
||||
controlSnapshots, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[ControlSnapshot])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect controls_snapshots: %w", err)
|
||||
}
|
||||
|
||||
*css = controlSnapshots
|
||||
return nil
|
||||
}
|
||||
|
||||
func (css *ControlSnapshots) LoadBySnapshotID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
snapshotID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
control_id,
|
||||
snapshot_id,
|
||||
created_at
|
||||
FROM
|
||||
controls_snapshots
|
||||
WHERE
|
||||
%s
|
||||
AND snapshot_id = @snapshot_id
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"snapshot_id": snapshotID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query controls_snapshots: %w", err)
|
||||
}
|
||||
|
||||
controlSnapshots, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[ControlSnapshot])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect controls_snapshots: %w", err)
|
||||
}
|
||||
|
||||
*css = controlSnapshots
|
||||
return nil
|
||||
}
|
||||
15
pkg/coredata/migrations/20250826T174137Z.sql
Normal file
15
pkg/coredata/migrations/20250826T174137Z.sql
Normal file
@@ -0,0 +1,15 @@
|
||||
CREATE TABLE controls_snapshots (
|
||||
control_id TEXT NOT NULL REFERENCES controls(id) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
snapshot_id TEXT NOT NULL REFERENCES snapshots(id) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
tenant_id TEXT NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
PRIMARY KEY (control_id, snapshot_id)
|
||||
);
|
||||
|
||||
ALTER TABLE controls_audits DROP CONSTRAINT controls_audits_control_id_fkey;
|
||||
ALTER TABLE controls_audits ADD CONSTRAINT controls_audits_control_id_fkey
|
||||
FOREIGN KEY (control_id) REFERENCES controls(id) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE controls_audits DROP CONSTRAINT controls_audits_audit_id_fkey;
|
||||
ALTER TABLE controls_audits ADD CONSTRAINT controls_audits_audit_id_fkey
|
||||
FOREIGN KEY (audit_id) REFERENCES audits(id) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -237,3 +237,60 @@ WHERE
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Snapshots) LoadByControlID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
controlID gid.GID,
|
||||
cursor *page.Cursor[SnapshotOrderField],
|
||||
) error {
|
||||
q := `
|
||||
WITH snapshots_by_control AS (
|
||||
SELECT
|
||||
s.id,
|
||||
s.tenant_id,
|
||||
s.organization_id,
|
||||
s.name,
|
||||
s.description,
|
||||
s.type,
|
||||
s.created_at
|
||||
FROM
|
||||
snapshots s
|
||||
INNER JOIN
|
||||
controls_snapshots cs ON s.id = cs.snapshot_id
|
||||
WHERE
|
||||
cs.control_id = @control_id
|
||||
)
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
name,
|
||||
description,
|
||||
type,
|
||||
created_at
|
||||
FROM
|
||||
snapshots_by_control
|
||||
WHERE %s
|
||||
AND %s
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"control_id": controlID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query snapshots: %w", err)
|
||||
}
|
||||
|
||||
snapshots, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Snapshot])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect snapshots: %w", err)
|
||||
}
|
||||
|
||||
*s = snapshots
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user