Initial export

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2025-09-04 10:34:21 +02:00
committed by Sacha Al Himdani
parent a11dfdb244
commit d47cac911d
13 changed files with 1022 additions and 63 deletions

View File

@@ -49,4 +49,5 @@ const (
SnapshotEntityType
ContinualImprovementRegistryEntityType
ProcessingActivityRegistryEntityType
FrameworkExportEntityType
)

View File

@@ -0,0 +1,163 @@
// 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"
"errors"
"fmt"
"maps"
"time"
"github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
)
type (
FrameworkExport struct {
ID gid.GID `db:"id"`
FrameworkID gid.GID `db:"framework_id"`
Status FrameworkExportStatus `db:"status"`
FileID *gid.GID `db:"file_id"`
CreatedAt time.Time `db:"created_at"`
StartedAt *time.Time `db:"started_at"`
CompletedAt *time.Time `db:"completed_at"`
}
FrameworkExports []*FrameworkExport
)
var (
ErrNoFrameworkExportAvailable = errors.New("no framework export available")
)
func (fe FrameworkExport) CursorKey(orderBy FrameworkExportOrderField) page.CursorKey {
switch orderBy {
case FrameworkExportOrderFieldCreatedAt:
return page.NewCursorKey(fe.ID, fe.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (fe *FrameworkExport) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO framework_exports (
id,
tenant_id,
framework_id,
status,
created_at,
expires_at
) VALUES (
@id,
@tenant_id,
@framework_id,
@status,
@created_at,
@expires_at
)`
args := pgx.StrictNamedArgs{
"id": fe.ID,
"tenant_id": scope.GetTenantID(),
"framework_id": fe.FrameworkID,
"status": fe.Status,
"created_at": fe.CreatedAt,
}
_, err := conn.Exec(ctx, q, args)
return err
}
func (fe *FrameworkExport) Update(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
UPDATE
framework_exports
SET
status = @status,
file_id = @file_id,
started_at = @started_at,
completed_at = @completed_at
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"status": fe.Status,
"file_id": fe.FileID,
"started_at": fe.StartedAt,
"completed_at": fe.CompletedAt,
"id": fe.ID,
}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
return err
}
func (fe *FrameworkExport) LoadNextPendingForUpdateSkipLocked(
ctx context.Context,
conn pg.Conn,
) error {
q := `
SELECT
id,
framework_id,
status,
created_at,
started_at,
completed_at
FROM
framework_exports
WHERE
status = @status
ORDER BY
created_at ASC
LIMIT 1
FOR UPDATE SKIP LOCKED
`
args := pgx.StrictNamedArgs{"status": FrameworkExportStatusPending}
rows, err := conn.Query(ctx, q, args)
if err != nil {
return err
}
fe2, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[FrameworkExport])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrNoFrameworkExportAvailable
}
return fmt.Errorf("cannot collect framework export: %w", err)
}
*fe = fe2
return nil
}

View File

@@ -0,0 +1,40 @@
// 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
type (
FrameworkExportOrderField string
)
const (
FrameworkExportOrderFieldCreatedAt FrameworkExportOrderField = "CREATED_AT"
)
func (p FrameworkExportOrderField) Column() string {
return string(p)
}
func (p FrameworkExportOrderField) String() string {
return string(p)
}
func (p FrameworkExportOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *FrameworkExportOrderField) UnmarshalText(text []byte) error {
*p = FrameworkExportOrderField(text)
return nil
}

View File

@@ -0,0 +1,86 @@
// 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 (
"database/sql/driver"
"fmt"
)
type (
FrameworkExportStatus string
)
const (
FrameworkExportStatusPending FrameworkExportStatus = "pending"
FrameworkExportStatusProcessing FrameworkExportStatus = "processing"
FrameworkExportStatusCompleted FrameworkExportStatus = "completed"
FrameworkExportStatusFailed FrameworkExportStatus = "failed"
)
func (pvs FrameworkExportStatus) MarshalText() ([]byte, error) {
return []byte(pvs.String()), nil
}
func (pvs *FrameworkExportStatus) UnmarshalText(data []byte) error {
val := string(data)
switch val {
case FrameworkExportStatusPending.String():
*pvs = FrameworkExportStatusPending
case FrameworkExportStatusProcessing.String():
*pvs = FrameworkExportStatusProcessing
case FrameworkExportStatusCompleted.String():
*pvs = FrameworkExportStatusCompleted
case FrameworkExportStatusFailed.String():
*pvs = FrameworkExportStatusFailed
default:
return fmt.Errorf("invalid FrameworkExportStatus value: %q", val)
}
return nil
}
func (pvs FrameworkExportStatus) String() string {
var val string
switch pvs {
case FrameworkExportStatusPending:
val = "pending"
case FrameworkExportStatusProcessing:
val = "processing"
case FrameworkExportStatusCompleted:
val = "completed"
case FrameworkExportStatusFailed:
val = "failed"
default:
panic(fmt.Errorf("invalid FrameworkExportStatus value: %q", string(pvs)))
}
return val
}
func (pvs *FrameworkExportStatus) Scan(value any) error {
val, ok := value.(string)
if !ok {
return fmt.Errorf("invalid scan source for FrameworkExportStatus, expected string got %T", value)
}
return pvs.UnmarshalText([]byte(val))
}
func (pvs FrameworkExportStatus) Value() (driver.Value, error) {
return pvs.String(), nil
}

View File

@@ -249,6 +249,8 @@ WHERE %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment())
fmt.Printf("\n%s\n", q)
args := pgx.NamedArgs{"control_id": controlID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, filter.SQLArguments())

View File

@@ -0,0 +1,20 @@
CREATE TYPE framework_export_status AS ENUM (
'pending',
'processing',
'completed',
'failed'
);
CREATE TABLE framework_exports (
id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
framework_id TEXT NOT NULL,
status framework_export_status NOT NULL,
file_id TEXT,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
started_at TIMESTAMP WITH TIME ZONE,
completed_at TIMESTAMP WITH TIME ZONE
);