lspostgres

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

Package lspostgres has a libschema.Driver support PostgreSQL

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Computed

func Computed[T ConnPtr](name string, action func(context.Context, T) error, opts ...libschema.MigrationOption) libschema.Migration

Computed defines a migration that runs arbitrary Go code. The signature of the action callback determines if the migration runs transactionally or if it runs outside a transaction:

func(context.Context, *sql.Tx) error // run transactionlly
func(context.Context, *sql.DB) error // run non-transactionally

func Generate

func Generate(name string, generator func(context.Context, *sql.Tx) string, opts ...libschema.MigrationOption) libschema.Migration

Generate registers a callback that returns a migration in a string. To run the migration, libschema automatically chooses transactional (*sql.Tx) or non-transactional (*sql.DB) execution based on Postgres rules for statements that cannot run inside a transaction (e.g. CREATE INDEX CONCURRENTLY, VACUUM FULL, CREATE DATABASE). If the migration will be run transactionally, it will run in the same transaction as the callback that returned the string. If it runs non-transactionally, the transaction that returned the string will be idle (hanging around) while the migration runs. The choice of transactional vs non-transactional Can be overridden with ForceNonTransactional() or ForceTransactional() options.

func New

func New(log *internal.Log, dbName string, schema *libschema.Schema, db *sql.DB) (*libschema.Database, error)

New creates a libschema.Database with a postgres driver built in. The dbName parameter is used internally by libschema, but does not affect where migrations are actually applied.

func Script

func Script(name string, sqlText string, opts ...libschema.MigrationOption) libschema.Migration

Script defines a literal SQL statement migration. To run the migration, libschema automatically chooses transactional (*sql.Tx) or non-transactional (*sql.DB) execution based on Postgres rules for statements that cannot run inside a transaction (e.g. CREATE INDEX CONCURRENTLY, VACUUM FULL, CREATE DATABASE). The choice of transactional vs non-transactional Can be overridden with ForceNonTransactional() or ForceTransactional() options.

Types

type ConnPtr added in v0.7.0

type ConnPtr interface{ *sql.Tx | *sql.DB }

type Postgres

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

Postgres is a libschema.Driver for connecting to Postgres-like databases that have the following characteristics: * Can do DDL commands inside transactions * Support UPSERT using INSERT ... ON CONFLICT

func (*Postgres) CreateSchemaTableIfNotExists

func (p *Postgres) CreateSchemaTableIfNotExists(ctx context.Context, _ *internal.Log, d *libschema.Database) error

CreateSchemaTableIfNotExists creates the migration tracking table for libschema. It is expected to be called by libschema.

func (*Postgres) DoOneMigration

func (p *Postgres) DoOneMigration(ctx context.Context, log *internal.Log, d *libschema.Database, m libschema.Migration) (result sql.Result, _ error)

DoOneMigration applies a single migration. It is expected to be called by libschema.

func (*Postgres) IsMigrationSupported

func (p *Postgres) IsMigrationSupported(d *libschema.Database, _ *internal.Log, migration libschema.Migration) error

IsMigrationSupported checks to see if a migration is well-formed. Absent a code change, this should always return nil. It is expected to be called by libschema.

func (*Postgres) LoadStatus

func (p *Postgres) LoadStatus(ctx context.Context, _ *internal.Log, d *libschema.Database) (_ []libschema.MigrationName, err error)

LoadStatus loads the current status of all migrations from the migration tracking table. It is expected to be called by libschema.

func (*Postgres) LockMigrationsTable

func (p *Postgres) LockMigrationsTable(ctx context.Context, _ *internal.Log, d *libschema.Database) error

LockMigrationsTable locks the migration tracking table for exclusive use by the migrations running now. It is expected to be called by libschema.

func (*Postgres) ServerVersion added in v0.7.0

func (p *Postgres) ServerVersion(ctx context.Context, db *sql.DB) (major, minor int)

ServerVersion queries and caches the PostgreSQL server version (major, minor). It returns zeroes if the version cannot be determined.

func (*Postgres) UnlockMigrationsTable

func (p *Postgres) UnlockMigrationsTable(_ *internal.Log) error

UnlockMigrationsTable unlocks the migration tracking table. It is expected to be called by libschema.

Jump to

Keyboard shortcuts

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