Initial AI service skeleton
This commit is contained in:
38
internal/migrate/migrate.go
Normal file
38
internal/migrate/migrate.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"ai-service/internal/store"
|
||||
)
|
||||
|
||||
//go:embed sql/*.up.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func Up(ctx context.Context, db *store.Store) error {
|
||||
entries, err := migrationFiles.ReadDir("sql")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var names []string
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".up.sql") {
|
||||
names = append(names, entry.Name())
|
||||
}
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
body, err := migrationFiles.ReadFile("sql/" + name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.Exec(ctx, string(body)); err != nil {
|
||||
return fmt.Errorf("%s: %w", name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
39
internal/migrate/sql/001_ai_jobs.up.sql
Normal file
39
internal/migrate/sql/001_ai_jobs.up.sql
Normal file
@@ -0,0 +1,39 @@
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ai_jobs (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
owner_service TEXT NOT NULL,
|
||||
owner_ref TEXT NOT NULL,
|
||||
task_type TEXT NOT NULL,
|
||||
model_profile TEXT NOT NULL,
|
||||
priority INTEGER NOT NULL DEFAULT 0,
|
||||
status TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (status IN ('pending', 'running', 'done', 'failed', 'cancelled')),
|
||||
attempts INTEGER NOT NULL DEFAULT 0,
|
||||
max_attempts INTEGER NOT NULL DEFAULT 3,
|
||||
input JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
result JSONB,
|
||||
error_code TEXT,
|
||||
error_message TEXT,
|
||||
scheduled_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
started_at TIMESTAMPTZ,
|
||||
completed_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
idempotency_key TEXT
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS ai_jobs_idempotency_key_idx
|
||||
ON ai_jobs (idempotency_key)
|
||||
WHERE idempotency_key IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS ai_jobs_queue_idx
|
||||
ON ai_jobs (status, priority DESC, scheduled_at ASC, created_at ASC)
|
||||
WHERE status IN ('pending', 'running');
|
||||
|
||||
CREATE INDEX IF NOT EXISTS ai_jobs_owner_idx
|
||||
ON ai_jobs (owner_service, owner_ref);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS ai_jobs_error_idx
|
||||
ON ai_jobs (task_type, model_profile, error_code, updated_at DESC)
|
||||
WHERE status = 'failed';
|
||||
Reference in New Issue
Block a user