Skip to content

Database & Migrations ​

The cobo db commands create your development and test databases and apply migrations everywhere. Migrations are run by dbmate.

They work with PostgreSQL, MySQL and SQLite. The engine comes from engine in your sqlc.yaml. A project without a sqlc.yaml has no database: cobo dev and cobo test run without DATABASE_URL, and cobo db asks you to add one.

PostgreSQLMySQLSQLite
Local serverOne you runOne you runNone — a file
Programs on PATHpsql, pg_dumpmysql, mysqldumpsqlite3
verify replays inA temporary schemaA temporary databaseA temporary file
Concurrent deploysSerialized by a migration lockNot lockedNot locked

Requirements ​

  • The programs for your engine, listed above.
  • dbmate — installed automatically: cobo uses runcobo-dbmate next to itself, then dbmate on your PATH, and otherwise downloads a pinned, checksum-verified release into .local/runcobo/dbmate/ on first use (needs curl and sha256sum or shasum).
  • A runcobo.yml.

Local databases ​

For PostgreSQL and MySQL, cobo uses a server you already run — installed locally, Postgres.app, Docker, whatever you like. ensure and prepare create <prefix>_development and <prefix>_test on it. For SQLite, databases are files in .local/sqlite/.

Local database URLs default to:

text
postgresql://postgres@127.0.0.1:5432/<prefix>_development
mysql://root@127.0.0.1:3306/<prefix>_development
sqlite3:./.local/sqlite/<prefix>_development.sqlite3

and the same with _test. To change them:

  • Set database_user and database_port in runcobo.yml if your server uses another user or port.
  • Or put a full URL — with a password, if needed — in DEVELOPMENT_DATABASE_URL and TEST_DATABASE_URL.

Local URLs must name the <prefix>_development or <prefix>_test database, and PostgreSQL and MySQL URLs must point at 127.0.0.1 or localhost, so cobo never touches another database by mistake.

Everyday commands ​

sh
bin/cobo db prepare            # create blog_development if missing, then migrate
bin/cobo db prepare test       # same for blog_test
bin/cobo db migrate            # apply pending migrations to an existing database
bin/cobo db status             # applied / pending / missing migrations
bin/cobo db rollback           # undo the most recent migration
bin/cobo db seed               # run seed_command
bin/cobo db console            # open psql, mysql or sqlite3

The environment argument defaults to development.

CommandEnvironmentsNotes
ensure [ENV]development, testCreate the database if missing
prepare [ENV]development, testensure, then migrate
migrate [ENV]development, test, staging, productionDatabase must already exist
status [ENV]development, test, staging, productionDoesn't change anything
wait [ENV]development, test, staging, productionWait for the server to accept connections
rollback [ENV]development, test, stagingNot available for production — write a corrective migration instead
seed [ENV]development, testRuns seed_command with DATABASE_URL set
console [ENV]development, testpsql, mysql or sqlite3 on the local database
verifytestCheck every migration's rollback and replay
snapshottestWrite a read-only schema reference

Run bin/cobo db --help, or bin/cobo db migrate --help for per-command options.

Writing migrations ​

Migrations are SQL files in db/migrations/, named YYYYMMDDHHMMSS_description.sql. Put the forward and backward steps in one file:

db/migrations/20261201093000_create_authors.sql
sql
-- migrate:up
CREATE TABLE authors (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name text NOT NULL,
  bio text
);

-- migrate:down
DROP TABLE authors;

Rules cobo enforces:

  • Every migration starts with -- migrate:up; put its rollback under -- migrate:down.
  • Version prefixes must be 14 digits and unique.
  • Migrations are applied in version order, and dbmate's strict mode refuses to apply a migration older than one already applied.
  • On PostgreSQL, an advisory lock stops two deploys from migrating at once.
  • Applied migrations are recorded in schema_migrations.

Treat published migration files as immutable. Fix mistakes with a new migration.

Verifying migrations ​

sh
bin/cobo db verify

In a throwaway place — a schema in the PostgreSQL test database, a database on the MySQL server, or a SQLite file — verify applies each migration, rolls it back, and applies it again, comparing a dump of the structure after every step (pg_dump, mysqldump or sqlite3). It fails if a rollback doesn't restore the previous structure exactly, or if replaying doesn't reproduce it. Run it in CI.

On PostgreSQL, replay runs in an isolated schema, so migrations must use unqualified, schema-local statements — no public. prefixes, CREATE SCHEMA or SET search_path. On MySQL, the database user needs permission to create and drop databases. Verification checks structure, not data.

Schema snapshot ​

sh
bin/cobo db snapshot

Writes db/schema.snapshot.sql: the structure produced by replaying all migrations, as a read-only reference for humans and code review. Nothing reads it back; migrations remain the only source of truth. Add it to .gitignore or commit it — your choice.

Staging and production ​

In deploy jobs, cobo connects only through STAGING_DATABASE_URL or PRODUCTION_DATABASE_URL:

sh
bin/cobo db status production --exit-code      # exit 1 if migrations are pending
bin/cobo db migrate production --wait --migration-lock-timeout 2m
  • The URL must name a host and database explicitly (for SQLite, a file: sqlite3:/var/lib/blog/production.sqlite3).
  • PostgreSQL: if you don't set sslmode, cobo uses sslmode=verify-full — the server certificate and host name are verified. Use a direct or session-pooling connection; the migration lock doesn't work through transaction-pooling proxies.
  • MySQL: if you don't set tls, cobo adds tls=true, which verifies the server certificate. Other values: skip-verify, preferred, false.
  • Run the migration files from the release you're deploying.

Useful options:

OptionCommandsDescription
--waitmigrate, rollback, statusWait for the server first
--wait-timeout TIMEallMax wait (default 60s)
--wait-interval TIMEallTime between attempts (default 1s)
--migration-lock-timeout TIMEmigrate, rollbackMax wait for the lock (default 5m). PostgreSQL only.
-v, --verbosemigrate, rollbackPrint each statement's result
--exit-codestatusExit 1 when migrations are pending
--quietstatusNo output; implies --exit-code

Released under the MIT License.