database

package
v0.0.0-...-9bf69fc Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 41 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultPostgresImage = "postgres:16.14"
	DefaultSidecarImage  = "alpine:3.24.1"
	DefaultContainerPort = 5432

	FlavorPostgres = "postgres"
	FlavorPostGIS  = "postgis"
)
View Source
const (
	ActionMigrationCreate = "devflow.database.migration.create"
)

Variables

View Source
var (
	ErrSnapshotIncompatible = errors.New("database snapshot is incompatible with the current runtime")
)

Functions

func GeneratePrismaMigration

func GeneratePrismaMigration(ctx context.Context, opts PrismaMigrationGenerateOptions) error

func GeneratePrismaMigrationForRuntime

func GeneratePrismaMigrationForRuntime(ctx context.Context, rt *project.Runtime, opts PrismaMigrationGenerateOptions) error

func MigrationSnapshotKey

func MigrationSnapshotKey(state *MigrationState) string

func PrismaMigrateDeployCommand

func PrismaMigrateDeployCommand(schemaPath string) process.CommandSpec

func PrismaMigrateDevCommand

func PrismaMigrateDevCommand(schemaPath, name string, createOnly bool) process.CommandSpec

func PrismaSnapshotKey

func PrismaSnapshotKey(state *PrismaState) string

func SnapshotKey

func SnapshotKey(parts ...string) string

Types

type CommandSourcePolicy

type CommandSourcePolicy struct {
	PolicyName string
	Spec       process.CommandSpec
}

func (CommandSourcePolicy) Name

func (p CommandSourcePolicy) Name() string

func (CommandSourcePolicy) PrepareBase

func (p CommandSourcePolicy) PrepareBase(ctx context.Context, db api.DBInstance, opts PrepareOptions) error

type Config

type Config struct {
	Flavor          string
	PostgresVersion int
	Image           string
	SidecarImage    string
	Host            string
	HostPort        int
	ContainerPort   int
	User            string
	Password        string
	Database        string
	ContainerPrefix string
	VolumePrefix    string
	SnapshotRoot    string
}

type ManagedMigrationOptions

type ManagedMigrationOptions struct {
	Worktree      string
	DB            api.DBInstance
	MigrationsDir string
	BasePaths     []string
	SourcePolicy  SourcePolicy
	Prepare       PrepareOptions
	Apply         process.CommandSpec
	ApplyEach     MigrationApplyFunc
	SnapshotKey   string
	ReadyTimeout  time.Duration
}

type ManagedMigrationResult

type ManagedMigrationResult struct {
	State       *MigrationState         `json:"state,omitempty"`
	Base        *MigrationBaseResult    `json:"base,omitempty"`
	Applied     bool                    `json:"applied"`
	Snapshot    *MigrationRestoreResult `json:"snapshot,omitempty"`
	SnapshotKey string                  `json:"snapshotKey,omitempty"`
	Plan        MigrationRestorePlan    `json:"plan"`
}

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func New

func New() *Manager

func (*Manager) Desired

func (m *Manager) Desired(instanceID string, cfg Config) api.DBInstance

func (*Manager) DestroyRuntime

func (m *Manager) DestroyRuntime(ctx context.Context, db api.DBInstance, removeVolume bool) error

func (*Manager) EnsureMigratedDatabase

func (m *Manager) EnsureMigratedDatabase(ctx context.Context, opts ManagedMigrationOptions) (*ManagedMigrationResult, error)

func (*Manager) EnsurePrismaDevDatabase

func (m *Manager) EnsurePrismaDevDatabase(ctx context.Context, opts PrismaDevDatabaseOptions) (*PrismaDevDatabaseResult, error)

func (*Manager) EnsureRuntime

func (m *Manager) EnsureRuntime(ctx context.Context, db api.DBInstance) error

func (*Manager) ExecSQL

func (m *Manager) ExecSQL(ctx context.Context, db api.DBInstance, statement string) ([]byte, error)

ExecSQL executes a SQL statement inside the managed Postgres container through the Docker Engine API. Output is returned even when psql fails.

func (*Manager) PrepareMigrationBase

func (m *Manager) PrepareMigrationBase(ctx context.Context, db api.DBInstance, state *MigrationState, policy SourcePolicy, opts PrepareOptions) (*MigrationBaseResult, error)

func (*Manager) PreparePrismaBase

func (m *Manager) PreparePrismaBase(ctx context.Context, db api.DBInstance, state *PrismaState, policy SourcePolicy, opts PrepareOptions) (*PrismaBaseResult, error)

func (*Manager) PreparePrismaMigrationAuthoringDatabase

func (m *Manager) PreparePrismaMigrationAuthoringDatabase(ctx context.Context, opts PrismaMigrationAuthoringOptions) (*PrismaMigrationAuthoringResult, error)

func (*Manager) RestoreNearestMigrationSnapshot

func (m *Manager) RestoreNearestMigrationSnapshot(ctx context.Context, db api.DBInstance, state *MigrationState) (*MigrationRestoreResult, error)

func (*Manager) RestoreNearestPrismaSnapshot

func (m *Manager) RestoreNearestPrismaSnapshot(ctx context.Context, db api.DBInstance, state *PrismaState) (*PrismaRestoreResult, error)

func (*Manager) RestoreSnapshot

func (m *Manager) RestoreSnapshot(ctx context.Context, db api.DBInstance, key string) (*SnapshotManifest, error)

func (*Manager) Snapshot

func (m *Manager) Snapshot(ctx context.Context, db api.DBInstance, key string) (*SnapshotManifest, error)

func (*Manager) SnapshotMigrations

func (m *Manager) SnapshotMigrations(ctx context.Context, db api.DBInstance, key string, state *MigrationState) (*MigrationRestoreResult, error)

func (*Manager) SnapshotPrisma

func (m *Manager) SnapshotPrisma(ctx context.Context, db api.DBInstance, key string, state *PrismaState) (*PrismaRestoreResult, error)

func (*Manager) StartRuntimeService

func (m *Manager) StartRuntimeService(ctx context.Context, db api.DBInstance, opts RuntimeServiceOptions) (*RuntimeServiceHandle, error)

StartRuntimeService ensures the database container is running and starts an Engine API log/wait stream. The returned handle stops the container when the owning task context is canceled.

func (*Manager) StopRuntime

func (m *Manager) StopRuntime(ctx context.Context, db api.DBInstance) error

func (*Manager) WaitHostReady

func (m *Manager) WaitHostReady(ctx context.Context, db api.DBInstance, timeout time.Duration) error

func (*Manager) WaitReady

func (m *Manager) WaitReady(ctx context.Context, db api.DBInstance, timeout time.Duration) error

type MigrationApplyFunc

type MigrationApplyFunc func(ctx context.Context, db api.DBInstance, migration MigrationPoint, opts MigrationApplyOptions) error

func PostgresMigrationFileApplier

func PostgresMigrationFileApplier(fileName string) MigrationApplyFunc

type MigrationApplyOptions

type MigrationApplyOptions struct {
	Worktree      string
	MigrationsDir string
	Index         int
	LogPath       string
	OnLine        func(stream, line string)
	Env           map[string]string
}

type MigrationBaseResult

type MigrationBaseResult struct {
	Restored      *MigrationRestoreResult `json:"restored,omitempty"`
	Recreated     bool                    `json:"recreated"`
	SourceApplied bool                    `json:"sourceApplied"`
	SourcePolicy  string                  `json:"sourcePolicy,omitempty"`
}

type MigrationNeededError

type MigrationNeededError struct {
	Reason  string
	Message string
}

func (*MigrationNeededError) Error

func (e *MigrationNeededError) Error() string

func (*MigrationNeededError) MigrationNeeded

func (e *MigrationNeededError) MigrationNeeded() bool

type MigrationPoint

type MigrationPoint struct {
	Name string `json:"name"`
	Hash string `json:"hash"`
}

type MigrationRestorePlan

type MigrationRestorePlan struct {
	ExactMatch   bool               `json:"exactMatch"`
	SnapshotKey  string             `json:"snapshotKey,omitempty"`
	PrefixLength int                `json:"prefixLength"`
	Snapshot     *MigrationSnapshot `json:"snapshot,omitempty"`
}

func PlanMigrationRestore

func PlanMigrationRestore(root string, state *MigrationState) (MigrationRestorePlan, error)

type MigrationRestoreResult

type MigrationRestoreResult struct {
	Manifest *SnapshotManifest    `json:"manifest,omitempty"`
	Metadata *MigrationSnapshot   `json:"metadata,omitempty"`
	Plan     MigrationRestorePlan `json:"plan"`
}

type MigrationSnapshot

type MigrationSnapshot struct {
	Version         int               `json:"version"`
	Key             string            `json:"key"`
	CreatedAt       time.Time         `json:"createdAt"`
	BaseFingerprint string            `json:"baseFingerprint,omitempty"`
	PathHashes      map[string]string `json:"pathHashes,omitempty"`
	Migrations      []MigrationPoint  `json:"migrations"`
	FullHash        string            `json:"fullHash"`
}

func LoadMigrationSnapshot

func LoadMigrationSnapshot(root, key string) (*MigrationSnapshot, error)

func SaveMigrationSnapshot

func SaveMigrationSnapshot(root, key string, state *MigrationState) (*MigrationSnapshot, error)

type MigrationState

type MigrationState struct {
	BaseFingerprint string            `json:"baseFingerprint,omitempty"`
	PathHashes      map[string]string `json:"pathHashes,omitempty"`
	Migrations      []MigrationPoint  `json:"migrations"`
	FullHash        string            `json:"fullHash"`
}

func InspectMigrationState

func InspectMigrationState(worktree, migrationsDir string, basePaths []string) (*MigrationState, error)

type PayloadCMSComponent

type PayloadCMSComponent struct {
	// contains filtered or unexported fields
}

func PayloadCMS

func PayloadCMS(name string) *PayloadCMSComponent

func (*PayloadCMSComponent) AddPrompts

func (p *PayloadCMSComponent) AddPrompts(prompts ...process.PromptSpec) *PayloadCMSComponent

func (*PayloadCMSComponent) AddSchemaInputs

func (p *PayloadCMSComponent) AddSchemaInputs(inputs ...any) *PayloadCMSComponent

func (*PayloadCMSComponent) Command

func (p *PayloadCMSComponent) Command(name string, args ...string) *PayloadCMSComponent

func (*PayloadCMSComponent) Config

func (*PayloadCMSComponent) Database

func (*PayloadCMSComponent) ForceAcceptWarningsEnv

func (p *PayloadCMSComponent) ForceAcceptWarningsEnv(key string) *PayloadCMSComponent

func (*PayloadCMSComponent) MigrationDir

func (p *PayloadCMSComponent) MigrationDir(path string) *PayloadCMSComponent

func (*PayloadCMSComponent) MigrationNameEnv

func (p *PayloadCMSComponent) MigrationNameEnv(key string) *PayloadCMSComponent

func (*PayloadCMSComponent) Migrations

func (*PayloadCMSComponent) NewMigration

func (p *PayloadCMSComponent) NewMigration(b *project.Builder) *project.TaskBuilder

func (*PayloadCMSComponent) Prompts

func (*PayloadCMSComponent) ReadyTimeout

func (p *PayloadCMSComponent) ReadyTimeout(timeout time.Duration) *PayloadCMSComponent

func (*PayloadCMSComponent) SchemaInputs

func (p *PayloadCMSComponent) SchemaInputs(inputs ...any) *PayloadCMSComponent

type PostgresComponent

type PostgresComponent struct {
	// contains filtered or unexported fields
}

func PostGIS

func PostGIS(name string, postgresVersion int) *PostgresComponent

func Postgres

func Postgres(name string) *PostgresComponent

func (*PostgresComponent) ContainerPort

func (p *PostgresComponent) ContainerPort(port int) *PostgresComponent

func (*PostgresComponent) DatabaseName

func (p *PostgresComponent) DatabaseName(name string) *PostgresComponent

func (*PostgresComponent) Image

func (p *PostgresComponent) Image(image string) *PostgresComponent

func (*PostgresComponent) Password

func (p *PostgresComponent) Password(password string) *PostgresComponent

func (*PostgresComponent) PortName

func (p *PostgresComponent) PortName(name string) *PostgresComponent

func (*PostgresComponent) SidecarImage

func (p *PostgresComponent) SidecarImage(image string) *PostgresComponent

func (*PostgresComponent) SnapshotRoot

func (p *PostgresComponent) SnapshotRoot(path string) *PostgresComponent

func (*PostgresComponent) User

type PostgresDumpSourcePolicy

type PostgresDumpSourcePolicy struct {
	PolicyName string
	RemoteURL  string
}

func (PostgresDumpSourcePolicy) Name

func (PostgresDumpSourcePolicy) PrepareBase

type PrepareOptions

type PrepareOptions struct {
	Worktree string
	Env      map[string]string
	LogPath  string
	OnLine   func(stream, line string)
}

func PrepareOptionsFromRuntime

func PrepareOptionsFromRuntime(rt *project.Runtime) PrepareOptions

type PrismaBaseResult

type PrismaBaseResult struct {
	Restored      *PrismaRestoreResult `json:"restored,omitempty"`
	Recreated     bool                 `json:"recreated"`
	SourceApplied bool                 `json:"sourceApplied"`
	SourcePolicy  string               `json:"sourcePolicy,omitempty"`
}

type PrismaComponent

type PrismaComponent struct {
	// contains filtered or unexported fields
}

func Prisma

func Prisma(name string) *PrismaComponent

func (*PrismaComponent) BasePaths

func (p *PrismaComponent) BasePaths(paths ...string) *PrismaComponent

func (*PrismaComponent) Client

func (*PrismaComponent) CloneFromEnv

func (p *PrismaComponent) CloneFromEnv(key string) *PrismaComponent

func (*PrismaComponent) Database

func (*PrismaComponent) MigrationDir

func (p *PrismaComponent) MigrationDir(path string) *PrismaComponent

func (*PrismaComponent) Migrations

func (p *PrismaComponent) Migrations(b *project.Builder) *project.TaskBuilder

func (*PrismaComponent) NewMigration

func (p *PrismaComponent) NewMigration(b *project.Builder) *project.TaskBuilder

func (*PrismaComponent) ReadyTimeout

func (p *PrismaComponent) ReadyTimeout(timeout time.Duration) *PrismaComponent

func (*PrismaComponent) Schema

func (p *PrismaComponent) Schema(path string) *PrismaComponent

func (*PrismaComponent) SourcePolicy

func (p *PrismaComponent) SourcePolicy(policy SourcePolicy) *PrismaComponent

type PrismaDevDatabaseOptions

type PrismaDevDatabaseOptions struct {
	Worktree      string
	DB            api.DBInstance
	SchemaPath    string
	MigrationsDir string
	BasePaths     []string
	SourcePolicy  SourcePolicy
	Prepare       PrepareOptions
	Migrate       process.CommandSpec
	MigrateEach   PrismaMigrationApplyFunc
	SnapshotKey   string
	ReadyTimeout  time.Duration
}

type PrismaDevDatabaseResult

type PrismaDevDatabaseResult struct {
	State       *PrismaState         `json:"state,omitempty"`
	Base        *PrismaBaseResult    `json:"base,omitempty"`
	Applied     bool                 `json:"applied"`
	Snapshot    *PrismaRestoreResult `json:"snapshot,omitempty"`
	SnapshotKey string               `json:"snapshotKey,omitempty"`
	Plan        PrismaRestorePlan    `json:"plan"`
}

type PrismaDevDatabaseSummary

type PrismaDevDatabaseSummary struct {
	Applied              bool     `json:"applied"`
	SnapshotKey          string   `json:"snapshotKey,omitempty"`
	PlanSnapshot         string   `json:"planSnapshot,omitempty"`
	ExactMatch           bool     `json:"exactMatch"`
	PrefixLength         int      `json:"prefixLength"`
	MigrationCount       int      `json:"migrationCount"`
	MigrationNames       []string `json:"migrationNames,omitempty"`
	Restored             bool     `json:"restored"`
	RestoredSnapshot     string   `json:"restoredSnapshot,omitempty"`
	RestoredExactMatch   bool     `json:"restoredExactMatch"`
	RestoredPrefixLength int      `json:"restoredPrefixLength"`
	Recreated            bool     `json:"recreated"`
	SourceApplied        bool     `json:"sourceApplied"`
	SourcePolicy         string   `json:"sourcePolicy,omitempty"`
}

func SummarizePrismaDevDatabase

func SummarizePrismaDevDatabase(result *PrismaDevDatabaseResult) PrismaDevDatabaseSummary

type PrismaDevelopmentStatus

type PrismaDevelopmentStatus struct {
	State             *PrismaState      `json:"state,omitempty"`
	Plan              PrismaRestorePlan `json:"plan"`
	NeedsNewMigration bool              `json:"needsNewMigration"`
	Reason            string            `json:"reason,omitempty"`
	Message           string            `json:"message,omitempty"`
}

func InspectPrismaDevelopmentStatus

func InspectPrismaDevelopmentStatus(worktree, schemaPath, migrationsDir string, extraPaths []string, snapshotRoot string) (*PrismaDevelopmentStatus, error)

type PrismaMigration

type PrismaMigration struct {
	Name string `json:"name"`
	Hash string `json:"hash"`
}

type PrismaMigrationApplyFunc

type PrismaMigrationApplyFunc func(ctx context.Context, db api.DBInstance, migration PrismaMigration, opts PrismaMigrationApplyOptions) error

func PrismaMigrateDeployPrefixApplier

func PrismaMigrateDeployPrefixApplier() PrismaMigrationApplyFunc

type PrismaMigrationApplyOptions

type PrismaMigrationApplyOptions struct {
	Worktree      string
	SchemaPath    string
	MigrationsDir string
	Index         int
	LogPath       string
	OnLine        func(stream, line string)
	Env           map[string]string
}

type PrismaMigrationAuthoringOptions

type PrismaMigrationAuthoringOptions struct {
	Worktree      string
	DB            api.DBInstance
	SchemaPath    string
	MigrationsDir string
	BasePaths     []string
	SourcePolicy  SourcePolicy
	Prepare       PrepareOptions
	MigrateEach   PrismaMigrationApplyFunc
	ReadyTimeout  time.Duration
}

type PrismaMigrationAuthoringResult

type PrismaMigrationAuthoringResult struct {
	State             *PrismaState      `json:"state,omitempty"`
	Base              *PrismaBaseResult `json:"base,omitempty"`
	Plan              PrismaRestorePlan `json:"plan"`
	Applied           bool              `json:"applied"`
	AppliedMigrations []string          `json:"appliedMigrations,omitempty"`
}

type PrismaMigrationAuthoringSummary

type PrismaMigrationAuthoringSummary struct {
	Applied              bool     `json:"applied"`
	AppliedMigrations    []string `json:"appliedMigrations,omitempty"`
	PlanSnapshot         string   `json:"planSnapshot,omitempty"`
	ExactMatch           bool     `json:"exactMatch"`
	PrefixLength         int      `json:"prefixLength"`
	MigrationCount       int      `json:"migrationCount"`
	MigrationNames       []string `json:"migrationNames,omitempty"`
	Restored             bool     `json:"restored"`
	RestoredSnapshot     string   `json:"restoredSnapshot,omitempty"`
	RestoredExactMatch   bool     `json:"restoredExactMatch"`
	RestoredPrefixLength int      `json:"restoredPrefixLength"`
	Recreated            bool     `json:"recreated"`
	SourceApplied        bool     `json:"sourceApplied"`
	SourcePolicy         string   `json:"sourcePolicy,omitempty"`
}

type PrismaMigrationGenerateOptions

type PrismaMigrationGenerateOptions struct {
	Worktree   string
	SchemaPath string
	Name       string
	CreateOnly bool
	Env        map[string]string
	LogPath    string
	OnLine     func(stream, line string)
	Command    process.CommandSpec
}

type PrismaRestorePlan

type PrismaRestorePlan struct {
	ExactMatch   bool            `json:"exactMatch"`
	SnapshotKey  string          `json:"snapshotKey,omitempty"`
	PrefixLength int             `json:"prefixLength"`
	Snapshot     *PrismaSnapshot `json:"snapshot,omitempty"`
}

func PlanPrismaRestore

func PlanPrismaRestore(root string, state *PrismaState) (PrismaRestorePlan, error)

type PrismaRestoreResult

type PrismaRestoreResult struct {
	Manifest *SnapshotManifest `json:"manifest,omitempty"`
	Metadata *PrismaSnapshot   `json:"metadata,omitempty"`
	Plan     PrismaRestorePlan `json:"plan"`
}

type PrismaSnapshot

type PrismaSnapshot struct {
	Version         int               `json:"version"`
	Key             string            `json:"key"`
	CreatedAt       time.Time         `json:"createdAt"`
	SchemaHash      string            `json:"schemaHash"`
	BaseFingerprint string            `json:"baseFingerprint,omitempty"`
	PathHashes      map[string]string `json:"pathHashes,omitempty"`
	Migrations      []PrismaMigration `json:"migrations"`
	FullHash        string            `json:"fullHash"`
}

func LoadPrismaSnapshot

func LoadPrismaSnapshot(root, key string) (*PrismaSnapshot, error)

func SavePrismaSnapshot

func SavePrismaSnapshot(root, key string, state *PrismaState) (*PrismaSnapshot, error)

type PrismaState

type PrismaState struct {
	SchemaHash      string            `json:"schemaHash"`
	BaseFingerprint string            `json:"baseFingerprint,omitempty"`
	PathHashes      map[string]string `json:"pathHashes,omitempty"`
	Migrations      []PrismaMigration `json:"migrations"`
	FullHash        string            `json:"fullHash"`
}

func InspectPrismaState

func InspectPrismaState(worktree, schemaPath, migrationsDir string, extraPaths []string) (*PrismaState, error)

type RuntimeServiceHandle

type RuntimeServiceHandle struct {
	// contains filtered or unexported fields
}

RuntimeServiceHandle adapts a Docker-managed database container to Devflow's generic supervised-service lifecycle without a wrapper process.

func (*RuntimeServiceHandle) Alive

func (h *RuntimeServiceHandle) Alive() bool

func (*RuntimeServiceHandle) PID

func (h *RuntimeServiceHandle) PID() int

PID returns zero because the supervised resource is an Engine-managed container rather than an operating-system child process.

func (*RuntimeServiceHandle) Stop

func (h *RuntimeServiceHandle) Stop() error

func (*RuntimeServiceHandle) Wait

func (h *RuntimeServiceHandle) Wait() error

type RuntimeServiceOptions

type RuntimeServiceOptions struct {
	OnLine func(stream, line string)
}

RuntimeServiceOptions controls managed-container log delivery. OnLine is called with "stdout" or "stderr" and a single line without its newline.

type SnapshotManifest

type SnapshotManifest struct {
	Version         int       `json:"version"`
	Key             string    `json:"key"`
	CreatedAt       time.Time `json:"createdAt"`
	Image           string    `json:"image"`
	Platform        string    `json:"platform,omitempty"`
	PostgresVersion int       `json:"postgresVersion,omitempty"`
	SidecarImage    string    `json:"sidecarImage,omitempty"`
	ContainerName   string    `json:"containerName"`
	VolumeName      string    `json:"volumeName"`
	Database        string    `json:"database"`
	User            string    `json:"user"`
	Port            int       `json:"port"`
	ContainerPort   int       `json:"containerPort,omitempty"`
	ArchivePath     string    `json:"archivePath"`
}

func LoadSnapshot

func LoadSnapshot(path string) (*SnapshotManifest, error)

type SourcePolicy

type SourcePolicy interface {
	Name() string
	PrepareBase(ctx context.Context, db api.DBInstance, opts PrepareOptions) error
}

type SourcePolicyFunc

type SourcePolicyFunc struct {
	PolicyName string
	Fn         func(ctx context.Context, db api.DBInstance, opts PrepareOptions) error
}

func (SourcePolicyFunc) Name

func (p SourcePolicyFunc) Name() string

func (SourcePolicyFunc) PrepareBase

func (p SourcePolicyFunc) PrepareBase(ctx context.Context, db api.DBInstance, opts PrepareOptions) error

Jump to

Keyboard shortcuts

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