feat: add migration
This commit is contained in:
@@ -1,23 +1,17 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.front.kjuulh.io/kjuulh/orbis/internal/persistence"
|
||||
"git.front.kjuulh.io/kjuulh/orbis/internal/utilities"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
var Postgres = utilities.Singleton(func() (*pgx.Conn, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
|
||||
conn, err := pgx.Connect(ctx, os.Getenv("ORBIS_POSTGRES_DB"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to orbis postgres database: %w", err)
|
||||
if err := persistence.Migrate(); err != nil {
|
||||
return nil, fmt.Errorf("failed to migrate database: %w", err)
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
return persistence.NewConnection()
|
||||
})
|
||||
|
@@ -18,5 +18,10 @@ func NewExecutor(logger *slog.Logger) *Executor {
|
||||
func (e *Executor) DispatchEvents(ctx context.Context) error {
|
||||
e.logger.InfoContext(ctx, "dispatching events")
|
||||
|
||||
// TODO: Process updates to models
|
||||
// TODO: Insert new cron for runtime
|
||||
// TODO: Calculate time since last run
|
||||
// TODO: Send events for workers to pick up
|
||||
|
||||
return nil
|
||||
}
|
||||
|
22
internal/persistence/connection.go
Normal file
22
internal/persistence/connection.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func NewConnection() (*pgx.Conn, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
|
||||
conn, err := pgx.Connect(ctx, os.Getenv("ORBIS_POSTGRES_DB"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to orbis postgres database: %w", err)
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
59
internal/persistence/migrations.go
Normal file
59
internal/persistence/migrations.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
migratepgx "github.com/golang-migrate/migrate/v4/database/pgx/v5"
|
||||
"github.com/golang-migrate/migrate/v4/source"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
)
|
||||
|
||||
const migrationSource = "migrations"
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrations embed.FS
|
||||
|
||||
func Migrate() error {
|
||||
db, err := sql.Open("pgx", os.Getenv("ORBIS_POSTGRES_DB"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to establish connection to database: %w", err)
|
||||
}
|
||||
|
||||
driver, err := migratepgx.WithInstance(db, &migratepgx.Config{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to install postgres driver for migrations: %w", err)
|
||||
}
|
||||
|
||||
migrationSource, err := NewEmbedDriver(migrationSource, migrations)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to setup embedded driver for migrations: %w", err)
|
||||
}
|
||||
|
||||
migration, err := migrate.NewWithInstance("iofs", migrationSource, "postgres", driver)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := migration.Up(); err != nil {
|
||||
if !errors.Is(err, migrate.ErrNoChange) {
|
||||
return fmt.Errorf("failed to migrate database: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewEmbedDriver(path string, files embed.FS) (source.Driver, error) {
|
||||
driver, err := iofs.New(files, migrationSource)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return driver, nil
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
CREATE TABLE test_table (
|
||||
id UUID primary key not null
|
||||
);
|
16
internal/worker/sqlc.yaml
Normal file
16
internal/worker/sqlc.yaml
Normal file
@@ -0,0 +1,16 @@
|
||||
version: "2"
|
||||
sql:
|
||||
- queries: queries.sql
|
||||
schema: ../persistence/migrations/
|
||||
engine: "postgresql"
|
||||
gen:
|
||||
go:
|
||||
out: "repositories"
|
||||
package: "repositories"
|
||||
sql_package: "sql/db"
|
||||
emit_json_tags: true
|
||||
emit_prepared_queries: true
|
||||
emit_interface: true
|
||||
emit_empty_slices: true
|
||||
emit_result_struct_pointers: true
|
||||
emit_params_struct_pointers: true
|
22
internal/worker/worker.go
Normal file
22
internal/worker/worker.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Worker struct {
|
||||
workerID uuid.UUID
|
||||
}
|
||||
|
||||
func NewWorker() *Worker {
|
||||
return &Worker{
|
||||
workerID: uuid.New(),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Worker) Setup(ctx context.Context) error {
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user