Add continual improvement snapshots
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
@@ -37,6 +37,8 @@ type (
|
||||
TargetDate *time.Time `db:"target_date"`
|
||||
Status ContinualImprovementRegistriesStatus `db:"status"`
|
||||
Priority ContinualImprovementRegistriesPriority `db:"priority"`
|
||||
SnapshotID *gid.GID `db:"snapshot_id"`
|
||||
SourceID *gid.GID `db:"source_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
@@ -78,6 +80,8 @@ SELECT
|
||||
target_date,
|
||||
status,
|
||||
priority,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -113,6 +117,7 @@ func (cirs *ContinualImprovementRegistries) CountByOrganizationID(
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
filter *ContinualImprovementRegistryFilter,
|
||||
) (int, error) {
|
||||
q := `
|
||||
SELECT
|
||||
@@ -122,12 +127,14 @@ FROM
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, filter.SQLArguments())
|
||||
|
||||
row := conn.QueryRow(ctx, q, args)
|
||||
|
||||
@@ -146,6 +153,7 @@ func (cirs *ContinualImprovementRegistries) LoadByOrganizationID(
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[ContinualImprovementRegistriesOrderField],
|
||||
filter *ContinualImprovementRegistryFilter,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
@@ -158,6 +166,8 @@ SELECT
|
||||
target_date,
|
||||
status,
|
||||
priority,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -166,12 +176,14 @@ WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
AND %s
|
||||
AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, filter.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
@@ -314,3 +326,58 @@ WHERE
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cirs ContinualImprovementRegistries) Snapshot(ctx context.Context, conn pg.Conn, scope Scoper, organizationID, snapshotID gid.GID) error {
|
||||
query := `
|
||||
INSERT INTO continual_improvement_registries (
|
||||
id,
|
||||
tenant_id,
|
||||
snapshot_id,
|
||||
source_id,
|
||||
organization_id,
|
||||
reference_id,
|
||||
description,
|
||||
source,
|
||||
owner_id,
|
||||
target_date,
|
||||
status,
|
||||
priority,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
SELECT
|
||||
generate_gid(decode_base64_unpadded(@tenant_id), @continual_improvement_registry_entity_type),
|
||||
@tenant_id,
|
||||
@snapshot_id,
|
||||
r.id,
|
||||
r.organization_id,
|
||||
r.reference_id,
|
||||
r.description,
|
||||
r.source,
|
||||
r.owner_id,
|
||||
r.target_date,
|
||||
r.status,
|
||||
r.priority,
|
||||
r.created_at,
|
||||
r.updated_at
|
||||
FROM continual_improvement_registries r
|
||||
WHERE %s AND r.organization_id = @organization_id AND r.snapshot_id IS NULL
|
||||
`
|
||||
|
||||
query = fmt.Sprintf(query, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"snapshot_id": snapshotID,
|
||||
"organization_id": organizationID,
|
||||
"continual_improvement_registry_entity_type": ContinualImprovementRegistryEntityType,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, query, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert continual improvement registry snapshots: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
54
pkg/coredata/continual_improvement_registry_filter.go
Normal file
54
pkg/coredata/continual_improvement_registry_filter.go
Normal file
@@ -0,0 +1,54 @@
|
||||
// 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 (
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type (
|
||||
ContinualImprovementRegistryFilter struct {
|
||||
snapshotID **gid.GID
|
||||
}
|
||||
)
|
||||
|
||||
func NewContinualImprovementRegistryFilter(snapshotID **gid.GID) *ContinualImprovementRegistryFilter {
|
||||
return &ContinualImprovementRegistryFilter{
|
||||
snapshotID: snapshotID,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *ContinualImprovementRegistryFilter) SQLArguments() pgx.NamedArgs {
|
||||
args := pgx.NamedArgs{}
|
||||
|
||||
if f.snapshotID != nil && *f.snapshotID != nil {
|
||||
args["filter_snapshot_id"] = **f.snapshotID
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
func (f *ContinualImprovementRegistryFilter) SQLFragment() string {
|
||||
if f.snapshotID == nil {
|
||||
return "TRUE"
|
||||
}
|
||||
|
||||
if *f.snapshotID == nil {
|
||||
return "snapshot_id IS NULL"
|
||||
} else {
|
||||
return "snapshot_id = @filter_snapshot_id"
|
||||
}
|
||||
}
|
||||
13
pkg/coredata/migrations/20250829T123035Z.sql
Normal file
13
pkg/coredata/migrations/20250829T123035Z.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
ALTER TYPE snapshots_type ADD VALUE 'CONTINUAL_IMPROVEMENT_REGISTRIES';
|
||||
|
||||
ALTER TABLE continual_improvement_registries ADD COLUMN snapshot_id TEXT;
|
||||
ALTER TABLE continual_improvement_registries ADD COLUMN source_id TEXT;
|
||||
|
||||
ALTER TABLE continual_improvement_registries ADD CONSTRAINT continual_improvement_registries_snapshot_id_fkey
|
||||
FOREIGN KEY (snapshot_id)
|
||||
REFERENCES snapshots(id)
|
||||
ON UPDATE CASCADE
|
||||
ON DELETE CASCADE;
|
||||
|
||||
ALTER TABLE continual_improvement_registries ADD CONSTRAINT continual_improvement_registries_source_id_snapshot_id_key
|
||||
UNIQUE (source_id, snapshot_id);
|
||||
@@ -24,12 +24,13 @@ type (
|
||||
)
|
||||
|
||||
const (
|
||||
SnapshotsTypeRisks SnapshotsType = "RISKS"
|
||||
SnapshotsTypeVendors SnapshotsType = "VENDORS"
|
||||
SnapshotsTypeAssets SnapshotsType = "ASSETS"
|
||||
SnapshotsTypeData SnapshotsType = "DATA"
|
||||
SnapshotsTypeNonConformityRegistries SnapshotsType = "NONCONFORMITY_REGISTRIES"
|
||||
SnapshotsTypeComplianceRegistries SnapshotsType = "COMPLIANCE_REGISTRIES"
|
||||
SnapshotsTypeRisks SnapshotsType = "RISKS"
|
||||
SnapshotsTypeVendors SnapshotsType = "VENDORS"
|
||||
SnapshotsTypeAssets SnapshotsType = "ASSETS"
|
||||
SnapshotsTypeData SnapshotsType = "DATA"
|
||||
SnapshotsTypeNonConformityRegistries SnapshotsType = "NONCONFORMITY_REGISTRIES"
|
||||
SnapshotsTypeComplianceRegistries SnapshotsType = "COMPLIANCE_REGISTRIES"
|
||||
SnapshotsTypeContinualImprovementRegistries SnapshotsType = "CONTINUAL_IMPROVEMENT_REGISTRIES"
|
||||
)
|
||||
|
||||
func (st SnapshotsType) String() string {
|
||||
@@ -60,6 +61,8 @@ func (st *SnapshotsType) Scan(value any) error {
|
||||
*st = SnapshotsTypeNonConformityRegistries
|
||||
case SnapshotsTypeComplianceRegistries.String():
|
||||
*st = SnapshotsTypeComplianceRegistries
|
||||
case SnapshotsTypeContinualImprovementRegistries.String():
|
||||
*st = SnapshotsTypeContinualImprovementRegistries
|
||||
default:
|
||||
return fmt.Errorf("invalid SnapshotsType value: %q", s)
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ func GetSnapshottable(snapshotType SnapshotsType) (Snapshottable, error) {
|
||||
return NonconformityRegistries{}, nil
|
||||
case SnapshotsTypeComplianceRegistries:
|
||||
return ComplianceRegistries{}, nil
|
||||
case SnapshotsTypeContinualImprovementRegistries:
|
||||
return ContinualImprovementRegistries{}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported snapshot type: %s", snapshotType)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user