Documentation
¶
Overview ¶
Package lspostgres has a libschema.Driver support PostgreSQL
Index ¶
- func Computed[T ConnPtr](name string, action func(context.Context, T) error, ...) libschema.Migration
- func Generate(name string, generator func(context.Context, *sql.Tx) string, ...) libschema.Migration
- func New(log *internal.Log, dbName string, schema *libschema.Schema, db *sql.DB) (*libschema.Database, error)
- func Script(name string, sqlText string, opts ...libschema.MigrationOption) libschema.Migration
- type ConnPtr
- type Postgres
- func (p *Postgres) CreateSchemaTableIfNotExists(ctx context.Context, _ *internal.Log, d *libschema.Database) error
- func (p *Postgres) DoOneMigration(ctx context.Context, log *internal.Log, d *libschema.Database, ...) (result sql.Result, _ error)
- func (p *Postgres) IsMigrationSupported(d *libschema.Database, _ *internal.Log, migration libschema.Migration) error
- func (p *Postgres) LoadStatus(ctx context.Context, _ *internal.Log, d *libschema.Database) (_ []libschema.MigrationName, err error)
- func (p *Postgres) LockMigrationsTable(ctx context.Context, _ *internal.Log, d *libschema.Database) error
- func (p *Postgres) ServerVersion(ctx context.Context, db *sql.DB) (major, minor int)
- func (p *Postgres) UnlockMigrationsTable(_ *internal.Log) error
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 ¶
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 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
ServerVersion queries and caches the PostgreSQL server version (major, minor). It returns zeroes if the version cannot be determined.