postgres

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package postgres is the PostgreSQL Dialect for github.com/gflydev/db/migrate.

Every migration file runs inside its own transaction (SupportsTransactionalDDL reports true): PostgreSQL allows DDL inside a transaction, so a failing statement leaves no partial effect.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect

type Dialect struct{}

Dialect implements migrate.Dialect for PostgreSQL. It is stateless; the zero value (Dialect{}) is ready to use.

func (Dialect) CreateMigrationsTableSQL

func (Dialect) CreateMigrationsTableSQL(table string) string

CreateMigrationsTableSQL returns the CREATE TABLE IF NOT EXISTS statement for table, matching the schema documented in the migrate package's README (migration, batch, status, run_count, checksum, migrated_at, rolled_back_at).

func (Dialect) Lock

func (Dialect) Lock(ctx context.Context, conn *sql.Conn, key string) error

Lock acquires a session-scoped PostgreSQL advisory lock keyed by the hash of key, blocking until it is available. The lock is released by Unlock on the same conn — advisory locks are tied to the session (connection) that took them, not to a transaction.

func (Dialect) Name

func (Dialect) Name() string

Name identifies this dialect as "postgres" in log and error output.

func (Dialect) Open

func (Dialect) Open() (*sql.DB, error)

Open reads the same DB_HOST/DB_PORT/DB_NAME/DB_USERNAME/DB_PASSWORD/DB_SSL_MODE environment variables as github.com/gflydev/db/psql and returns a pgx-backed connection pool. It does not verify connectivity — callers that need to fail fast on a bad connection should call (*sql.DB).PingContext themselves.

func (Dialect) Placeholder

func (Dialect) Placeholder(argPos int) string

Placeholder returns PostgreSQL's numbered placeholder ("$1", "$2", ...) for argument position argPos.

func (Dialect) SupportsTransactionalDDL

func (Dialect) SupportsTransactionalDDL() bool

SupportsTransactionalDDL always returns true: PostgreSQL rolls back DDL inside a failed transaction like any other statement.

func (Dialect) Unlock

func (Dialect) Unlock(ctx context.Context, conn *sql.Conn, key string) error

Unlock releases the advisory lock Lock acquired on conn for key.

func (Dialect) UpsertMigrationSQL

func (Dialect) UpsertMigrationSQL(table string) string

UpsertMigrationSQL returns an INSERT ... ON CONFLICT DO UPDATE statement for table, taking (migration, batch, run_count, checksum) as $1-$4 and always setting status to migrate.StatusUp and migrated_at to the current time.

Jump to

Keyboard shortcuts

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