Add migrations system

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-01-24 14:48:01 +01:00
parent 6a378a8a2b
commit 64a3072a54
4 changed files with 60 additions and 3 deletions

View File

@@ -0,0 +1,10 @@
package coredata
import (
"embed"
)
var (
//go:embed migrations/*.sql
Migrations embed.FS
)

View File

@@ -0,0 +1,37 @@
CREATE TABLE organizations (
id UUID PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);
CREATE TABLE frameworks (
id UUID PRIMARY KEY,
organization_id UUID REFERENCES organizations(id) NOT NULL,
name TEXT NOT NULL,
description TEXT NOT NULL,
content_ref TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);
CREATE TABLE controls (
id UUID PRIMARY KEY,
framework_id UUID REFERENCES frameworks(id) NOT NULL,
name TEXT NOT NULL,
description TEXT NOT NULL,
content_ref TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);
CREATE TABLE tasks (
id UUID PRIMARY KEY,
control_id UUID REFERENCES controls(id) NOT NULL,
name TEXT NOT NULL,
description TEXT NOT NULL,
content_ref TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);

View File

@@ -16,10 +16,12 @@ package probo
import (
"context"
"fmt"
"github.com/getprobo/probo/pkg/probo/coredata"
"github.com/getprobo/probo/pkg/probo/coredata/gid"
"github.com/getprobo/probo/pkg/probo/coredata/page"
"go.gearno.de/kit/migrator"
"go.gearno.de/kit/pg"
)
@@ -30,11 +32,16 @@ type (
}
)
func NewService(ctx context.Context, pgClient *pg.Client) *Service {
func NewService(ctx context.Context, pgClient *pg.Client) (*Service, error) {
err := migrator.NewMigrator(pgClient, coredata.Migrations).Run(ctx, "migrations")
if err != nil {
return nil, fmt.Errorf("cannot migrate database schema: %w", err)
}
return &Service{
pg: pgClient,
scope: coredata.NewScope(), // must be created from auth
}
}, nil
}
func (s *Service) GetOrganization(

View File

@@ -94,7 +94,10 @@ func (impl *Implm) Run(
return fmt.Errorf("cannot create pg client: %w", err)
}
probo := probo.NewService(ctx, pgClient)
probo, err := probo.NewService(ctx, pgClient)
if err != nil {
return fmt.Errorf("cannot create probo service: %w", err)
}
apiServer, err := api.NewServer(
api.Config{