Skip to main content

CockroachDB Driver

CockroachDB support follows PostgreSQL-like SQL syntax, with CockroachDB's serializable transaction behavior.

Packages

import (
"database/sql"

"github.com/yaop-labs/queen"
"github.com/yaop-labs/queen/drivers/cockroachdb"
_ "github.com/jackc/pgx/v5/stdlib"
)

db, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
if err != nil {
return err
}

driver, err := cockroachdb.New(db)
if err != nil {
return err
}

q := queen.New(driver)

How it works

AreaImplementation
Migration tableSQL table with metadata columns.
Lock tableSeparate <table>_lock table with lock_key primary key and expires_at.
Identifier quotingDouble quotes.
Placeholders$1, $2, ...
Lock ownerGenerated owner ID per driver instance.
LockingTable-backed lock through cleanup, check, and insert.
UnlockDeletes the lock row for the current owner.
Executiondatabase/sql transaction through the shared base driver.
Atomic recordNo. Migration body commits before Record.

Guarantees

  • Lock rows expire and are cleaned during acquisition attempts.
  • Wrapped sql.ErrNoRows is treated as no active lock.
  • Queen uses natural status ordering before planning operations.

Limitations

  • CockroachDB can return retryable serialization errors (40001).
  • The current driver does not implement a CockroachDB retry loop around migration transactions.
  • Migration metadata is not written in the same transaction as the migration body.

Operational advice

  • Run one migrator job per environment.
  • If a migration fails with a retryable transaction error, rerun the migrator after checking whether the migration body committed.
  • Prefer PostgreSQL for the first production target when you need Queen's strongest guarantees today.