postgres

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

PostgreSQL migrations

Versioned SQL for adapters that store data in PostgreSQL:

  • run snapshots (agentflow_run_snapshots; fence_token added in migration 0004)
  • tenant-scoped async job queue (agentflow_jobs, tenant isolation added in migration 0007)
  • tenant-scoped runtime events (agentflow_runtime_events, tenant isolation added in migration 0006)
  • transactional event outbox (agentflow_outbox, migration 0005; drained by the framework's WithOutboxRelay loop)
  • knowledge embeddings (agentflow_knowledge_embeddings)
  • memory tier warm records (agentflow_memory_tier_records, migration 0002)

Applying

Preferred: the embedded Go runner. It records applied versions in agentflow_schema_migrations, skips versions already applied, serializes concurrent runners with pg_advisory_lock, and commits each migration with its version record in one transaction:

./examples/deploy/init/apply-migrations.sh          # uses AGENT_POSTGRES_DSN
go run ./migrations/postgres/cmd/apply-migrations <dsn>

From Go:

import migrations "github.com/aijustin/agentflow-go/migrations/postgres"

err := migrations.Migrate(ctx, db)        // idempotent, safe on every boot
version, err := migrations.AppliedVersion(ctx, db) // 0 when never migrated

The raw *.up.sql files remain usable with any migration runner; migration 0004 creates the version table and records itself. ValidateWiring refuses to start a PostgreSQL run-state repository against a schema older than migrations.RequiredVersion so missing columns surface at boot.

NewPostgresEventStore can create agentflow_runtime_events at startup for local development. In locked-down environments, apply the migrations first and pass SkipSchemaSetup: true.

The vector table uses vector(1536); change the dimension if your embedding model differs.

Documentation

Overview

Package postgres applies and inspects the versioned PostgreSQL schema migrations embedded from this directory (the NNNN_*.up.sql files).

Migrations are tracked in the agentflow_schema_migrations table (created by the runner up front and formally owned by migration 0004), so reruns skip versions that are already applied. Concurrent first boots are serialized with a PostgreSQL advisory lock held on a single connection for the whole run, and each migration plus its version record commits in one transaction.

Index

Constants

View Source
const RequiredVersion = 7

RequiredVersion is the minimum applied schema version this codebase needs. Version 4 adds agentflow_run_snapshots.fence_token, which fenced snapshot saves (runstate.FencedRepository) depend on; version 5 adds the agentflow_outbox table used by transactional event outbox writes and the framework outbox relay; version 6 adds tenant isolation to runtime events; version 7 adds tenant isolation to async jobs. ValidateWiring refuses to start against an older schema so missing columns/tables surface at boot instead of at runtime.

Variables

This section is empty.

Functions

func AppliedVersion

func AppliedVersion(ctx context.Context, db *sql.DB) (int, error)

AppliedVersion returns the highest recorded migration version. A database without the version table (never migrated) reports version 0 so callers can treat "no bookkeeping" as "nothing applied" instead of an error.

func Migrate

func Migrate(ctx context.Context, db *sql.DB) error

Migrate applies every embedded *.up.sql migration whose version is not yet recorded in agentflow_schema_migrations, in version order. It is safe to call on every boot and from several instances at once.

Types

This section is empty.

Directories

Path Synopsis
cmd
apply-migrations command
Command apply-migrations applies the embedded agentflow PostgreSQL schema migrations to the database named by AGENT_POSTGRES_DSN (or the first argument).
Command apply-migrations applies the embedded agentflow PostgreSQL schema migrations to the database named by AGENT_POSTGRES_DSN (or the first argument).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL