cockroachdb

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package cockroachdb provides a CockroachDB driver for Queen migrations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func QuoteIdentifier

func QuoteIdentifier(name string) string

QuoteIdentifier quotes a SQL identifier (table name, column name) to prevent SQL injection. In CockroachDB, identifiers are quoted with double quotes.

This function is provided for backward compatibility.

Types

type Driver

type Driver struct {
	base.Driver
	// contains filtered or unexported fields
}

Driver implements the queen.Driver interface for CockroachDB.

func New

func New(db *sql.DB) *Driver

New creates a new CockroachDB driver.

The database connection should already be open and configured. The default migrations table name is "queen_migrations".

Cockroach Labs officially recommends using pgx.

Example:

db, err := sql.Open("pgx", DSN)
if err != nil {
    log.Fatal(err)
}
driver := cockroachdb.New(db)

func NewWithTableName

func NewWithTableName(db *sql.DB, tableName string) *Driver

NewWithTableName creates a new CockroachDB driver with a custom table name.

Use this when you need to manage multiple independent sets of migrations in the same database, or when you want to customize the table name for organizational purposes.

Example:

driver := cockroachdb.NewWithTableName(db, "my_custom_migrations")

func (*Driver) Init

func (d *Driver) Init(ctx context.Context) error

Init creates the migrations tracking table and lock table if they don't exist.

The migrations table schema:

  • version: VARCHAR(255) PRIMARY KEY - unique migration version
  • name: VARCHAR(255) NOT NULL - human-readable migration name
  • applied_at: TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP - when the migration was applied
  • checksum: VARCHAR(64) NOT NULL - hash of migration content for validation

The lock table schema:

  • lock_key: VARCHAR(255) PRIMARY KEY - lock identifier
  • acquired_at: TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP - when the lock was acquired
  • expires_at: TIMESTAMP NOT NULL - when the lock expires

This method is idempotent and safe to call multiple times.

func (*Driver) Lock

func (d *Driver) Lock(ctx context.Context, timeout time.Duration) error

Lock acquires a distributed lock to prevent concurrent migrations.

CockroachDB doesn't have advisory locks like PostgreSQL. Instead, this driver uses a lock table with expiration times to implement distributed locking across multiple processes/containers.

The lock mechanism: 1. Cleans up expired locks using DELETE FROM 2. Checks if an active lock exists using SELECT with LIMIT 3. If no lock exists, attempts INSERT 4. Retries with exponential backoff until timeout or lock is acquired

Exponential backoff starts at 50ms and doubles up to 1s maximum to reduce database load during lock contention.

If the lock cannot be acquired within the timeout, returns queen.ErrLockTimeout.

func (*Driver) Unlock

func (d *Driver) Unlock(ctx context.Context) error

Unlock releases the migration lock.

This removes the lock record from the lock table, allowing other processes to acquire the lock.

This method is graceful: it returns nil if the lock doesn't exist or was already released.

Jump to

Keyboard shortcuts

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