Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions cluster-manager/migrations/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package migrations

import "embed"

// Postgres migrations directory.
//
//go:embed postgres/*.sql
var Postgres embed.FS
24 changes: 24 additions & 0 deletions cluster-manager/migrations/postgres/00001_initial_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- +goose Up
-- Initial schema for cluster-manager Postgres

CREATE TABLE IF NOT EXISTS clusters (
id uuid PRIMARY KEY,
created_at timestamptz,
updated_at timestamptz,
name text,
token text,
status smallint,
config jsonb,
registered_at timestamptz,
deleted_at timestamptz
);

CREATE INDEX IF NOT EXISTS idx_clusters_created_at ON clusters (created_at DESC);
CREATE INDEX IF NOT EXISTS idx_clusters_name ON clusters (name);
CREATE UNIQUE INDEX IF NOT EXISTS idx_clusters_token ON clusters (token);
CREATE INDEX IF NOT EXISTS idx_clusters_status ON clusters (status);
CREATE INDEX IF NOT EXISTS idx_clusters_registered_at ON clusters (registered_at);
CREATE INDEX IF NOT EXISTS idx_clusters_deleted_at ON clusters (deleted_at);

-- +goose Down
DROP TABLE IF EXISTS clusters;
38 changes: 31 additions & 7 deletions cluster-manager/pkg/database/database.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package database

import (
"context"
"errors"
"fmt"
"io/fs"
"net/url"
"strings"
"time"

"github.com/pressly/goose/v3"
"github.com/rs/zerolog/log"
"github.com/runtime-radar/runtime-radar/cluster-manager/pkg/model"
"github.com/runtime-radar/runtime-radar/cluster-manager/migrations"
"github.com/runtime-radar/runtime-radar/cluster-manager/pkg/build"
"github.com/runtime-radar/runtime-radar/lib/logger"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand Down Expand Up @@ -72,15 +78,33 @@ func New(address, database, user, password string, sslMode, sslCheckCert bool) (
}

func Migrate(db *gorm.DB, newDB bool) error {
ctx := context.TODO()

sqlDB, err := db.DB()
if err != nil {
return fmt.Errorf("can't migrate postgresql db: %w", err)
}

migrationsFS, err := fs.Sub(migrations.Postgres, "postgres")
if err != nil {
return err
}

// Services share a single database, so each of them keeps its own migration history.
provider, err := goose.NewProvider("postgres", sqlDB, migrationsFS, goose.WithTableName(fmt.Sprintf("goose_db_version_%s", strings.ReplaceAll(build.AppName, "-", "_"))))
if err != nil {
return err
}

if newDB {
if err := db.Migrator().DropTable(
&model.Cluster{},
); err != nil {
if _, err := provider.DownTo(ctx, 0); err != nil && !errors.Is(err, goose.ErrNoNextVersion) {
return err
}
}

return db.Migrator().AutoMigrate(
&model.Cluster{},
)
if _, err := provider.Up(ctx); err != nil {
return err
}

return nil
}
8 changes: 8 additions & 0 deletions cs-manager/migrations/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package migrations

import "embed"

// Postgres migrations directory.
//
//go:embed postgres/*.sql
var Postgres embed.FS
16 changes: 16 additions & 0 deletions cs-manager/migrations/postgres/00001_initial_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- +goose Up
-- Initial schema for cs-manager Postgres

CREATE TABLE IF NOT EXISTS registrations (
id uuid PRIMARY KEY,
created_at timestamptz,
updated_at timestamptz,
token_hash text,
status smallint,
error text
);

CREATE INDEX IF NOT EXISTS idx_registrations_created_at ON registrations (created_at DESC);

-- +goose Down
DROP TABLE IF EXISTS registrations;
37 changes: 30 additions & 7 deletions cs-manager/pkg/database/database.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package database

import (
"context"
"errors"
"fmt"
"io/fs"
"net/url"
"strings"
"time"

"github.com/jackc/pgx/v5/pgconn"
"github.com/pressly/goose/v3"
"github.com/rs/zerolog/log"
"github.com/runtime-radar/runtime-radar/cs-manager/pkg/model"
"github.com/runtime-radar/runtime-radar/cs-manager/migrations"
"github.com/runtime-radar/runtime-radar/cs-manager/pkg/build"
"github.com/runtime-radar/runtime-radar/lib/logger"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand Down Expand Up @@ -74,17 +79,35 @@ func New(address, database, user, password string, sslMode, sslCheckCert bool) (
}

func Migrate(db *gorm.DB, newDB bool) error {
ctx := context.TODO()

sqlDB, err := db.DB()
if err != nil {
return fmt.Errorf("can't migrate postgresql db: %w", err)
}

migrationsFS, err := fs.Sub(migrations.Postgres, "postgres")
if err != nil {
return err
}

// Services share a single database, so each of them keeps its own migration history.
provider, err := goose.NewProvider("postgres", sqlDB, migrationsFS, goose.WithTableName(fmt.Sprintf("goose_db_version_%s", strings.ReplaceAll(build.AppName, "-", "_"))))
if err != nil {
return err
}

if newDB {
if err := db.Migrator().DropTable(
&model.Registration{},
); err != nil {
if _, err := provider.DownTo(ctx, 0); err != nil && !errors.Is(err, goose.ErrNoNextVersion) {
return err
}
}

return db.Migrator().AutoMigrate(
&model.Registration{},
)
if _, err := provider.Up(ctx); err != nil {
return err
}

return nil
}

func uniqueConstraintViolation(err error, table, field string) bool {
Expand Down
8 changes: 8 additions & 0 deletions event-processor/migrations/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package migrations

import "embed"

// Postgres migrations directory.
//
//go:embed postgres/*.sql
var Postgres embed.FS
34 changes: 34 additions & 0 deletions event-processor/migrations/postgres/00001_initial_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- +goose Up
-- Initial schema for event-processor Postgres

-- The table is named "event_processor_configs" rather than "configs" so that the more generic name
-- stays available for a possible cross-component dynamic config mechanism. See model.Config.
CREATE TABLE IF NOT EXISTS event_processor_configs (
id uuid PRIMARY KEY,
created_at timestamptz,
updated_at timestamptz,
config jsonb
);

CREATE INDEX IF NOT EXISTS idx_event_processor_configs_created_at ON event_processor_configs (created_at DESC);

CREATE TABLE IF NOT EXISTS detectors (
id text NOT NULL,
created_at timestamptz,
name text,
description text,
version bigint NOT NULL,
author text,
contact text,
license text,
wasm_binary bytea,
wasm_hash text,
PRIMARY KEY (id, version)
);

CREATE INDEX IF NOT EXISTS idx_detectors_name ON detectors (name);
CREATE INDEX IF NOT EXISTS idx_detectors_created_at ON detectors (created_at DESC);

-- +goose Down
DROP TABLE IF EXISTS detectors;
DROP TABLE IF EXISTS event_processor_configs;
33 changes: 25 additions & 8 deletions event-processor/pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"net/url"
"strings"
"time"

"github.com/pressly/goose/v3"
"github.com/rs/zerolog/log"
"github.com/runtime-radar/runtime-radar/event-processor/migrations"
"github.com/runtime-radar/runtime-radar/event-processor/pkg/build"
"github.com/runtime-radar/runtime-radar/event-processor/pkg/model"
"github.com/runtime-radar/runtime-radar/lib/logger"
"gorm.io/driver/postgres"
Expand Down Expand Up @@ -65,19 +70,31 @@ func New(address, database, user, password string, sslMode, sslCheckCert bool) (
}

func Migrate(db *gorm.DB, newDB bool) error {
ctx := context.TODO()

sqlDB, err := db.DB()
if err != nil {
return fmt.Errorf("can't migrate postgresql db: %w", err)
}

migrationsFS, err := fs.Sub(migrations.Postgres, "postgres")
if err != nil {
return err
}

// Services share a single database, so each of them keeps its own migration history.
provider, err := goose.NewProvider("postgres", sqlDB, migrationsFS, goose.WithTableName(fmt.Sprintf("goose_db_version_%s", strings.ReplaceAll(build.AppName, "-", "_"))))
if err != nil {
return err
}

if newDB {
if err := db.Migrator().DropTable(
&model.Detector{},
&model.Config{},
); err != nil {
if _, err := provider.DownTo(ctx, 0); err != nil && !errors.Is(err, goose.ErrNoNextVersion) {
return err
}
}

if err := db.Migrator().AutoMigrate(
&model.Config{},
&model.Detector{},
); err != nil {
if _, err := provider.Up(ctx); err != nil {
return err
}

Expand Down
Loading
Loading