Migrate audit reports to the files table

Signed-off-by: Ludovic Vielle <ludovic@probo.com>
This commit is contained in:
Ludovic Vielle
2026-06-03 22:19:17 +02:00
parent b0a0f0efc9
commit 0e4d73bb0f
31 changed files with 517 additions and 947 deletions

View File

@@ -34,7 +34,7 @@ type (
Name *string `db:"name"`
OrganizationID gid.GID `db:"organization_id"`
FrameworkID gid.GID `db:"framework_id"`
ReportID *gid.GID `db:"report_id"`
ReportFileID *gid.GID `db:"report_file_id"`
ValidFrom *time.Time `db:"valid_from"`
ValidUntil *time.Time `db:"valid_until"`
State AuditState `db:"state"`
@@ -113,7 +113,7 @@ SELECT
name,
organization_id,
framework_id,
report_id,
report_file_id,
valid_from,
valid_until,
state,
@@ -199,7 +199,7 @@ SELECT
name,
organization_id,
framework_id,
report_id,
report_file_id,
valid_from,
valid_until,
state,
@@ -250,7 +250,7 @@ SELECT
name,
organization_id,
framework_id,
report_id,
report_file_id,
valid_from,
valid_until,
state,
@@ -299,7 +299,7 @@ INSERT INTO audits (
tenant_id,
organization_id,
framework_id,
report_id,
report_file_id,
valid_from,
valid_until,
state,
@@ -312,7 +312,7 @@ INSERT INTO audits (
@tenant_id,
@organization_id,
@framework_id,
@report_id,
@report_file_id,
@valid_from,
@valid_until,
@state,
@@ -328,7 +328,7 @@ INSERT INTO audits (
"tenant_id": scope.GetTenantID(),
"organization_id": a.OrganizationID,
"framework_id": a.FrameworkID,
"report_id": a.ReportID,
"report_file_id": a.ReportFileID,
"valid_from": a.ValidFrom,
"valid_until": a.ValidUntil,
"state": a.State,
@@ -354,7 +354,7 @@ func (a *Audit) Update(
UPDATE audits
SET
name = @name,
report_id = @report_id,
report_file_id = @report_file_id,
valid_from = @valid_from,
valid_until = @valid_until,
state = @state,
@@ -370,7 +370,7 @@ WHERE
args := pgx.StrictNamedArgs{
"id": a.ID,
"name": a.Name,
"report_id": a.ReportID,
"report_file_id": a.ReportFileID,
"valid_from": a.ValidFrom,
"valid_until": a.ValidUntil,
"state": a.State,
@@ -427,7 +427,7 @@ WITH audits_by_control AS (
a.name,
a.organization_id,
a.framework_id,
a.report_id,
a.report_file_id,
a.valid_from,
a.valid_until,
a.state,
@@ -446,7 +446,7 @@ SELECT
name,
organization_id,
framework_id,
report_id,
report_file_id,
valid_from,
valid_until,
state,
@@ -494,7 +494,7 @@ WITH audits_by_finding AS (
a.name,
a.organization_id,
a.framework_id,
a.report_id,
a.report_file_id,
a.valid_from,
a.valid_until,
a.state,
@@ -513,7 +513,7 @@ SELECT
name,
organization_id,
framework_id,
report_id,
report_file_id,
valid_from,
valid_until,
state,
@@ -632,11 +632,11 @@ WHERE
return count, nil
}
func (a *Audit) LoadByReportID(
func (a *Audit) LoadByReportFileID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
reportID gid.GID,
fileID gid.GID,
) error {
q := `
SELECT
@@ -644,7 +644,7 @@ SELECT
name,
organization_id,
framework_id,
report_id,
report_file_id,
valid_from,
valid_until,
state,
@@ -654,12 +654,12 @@ SELECT
FROM
audits
WHERE %s
AND report_id = @report_id
AND report_file_id = @report_file_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"report_id": reportID}
args := pgx.StrictNamedArgs{"report_file_id": fileID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
@@ -681,6 +681,52 @@ LIMIT 1;
return nil
}
func (as *Audits) LoadByReportFileIDs(
ctx context.Context,
conn pg.Querier,
scope Scoper,
fileIDs []gid.GID,
) error {
q := `
SELECT
id,
name,
organization_id,
framework_id,
report_file_id,
valid_from,
valid_until,
state,
trust_center_visibility,
created_at,
updated_at
FROM
audits
WHERE
%s
AND report_file_id = ANY(@file_ids)
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"file_ids": fileIDs}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query audits by report file IDs: %w", err)
}
audits, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Audit])
if err != nil {
return fmt.Errorf("cannot collect audits by report file IDs: %w", err)
}
*as = audits
return nil
}
func (as *Audits) LoadByReportIDs(
ctx context.Context,
conn pg.Querier,