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.
| PostgreSQL | MySQL | SQLite | |
|---|---|---|---|
| Local server | One you run | One you run | None — a file |
Programs on PATH | psql, pg_dump | mysql, mysqldump | sqlite3 |
verify replays in | A temporary schema | A temporary database | A temporary file |
| Concurrent deploys | Serialized by a migration lock | Not locked | Not locked |
Requirements
- The programs for your engine, listed above.
- dbmate — installed automatically:
cobousesruncobo-dbmatenext to itself, thendbmateon yourPATH, and otherwise downloads a pinned, checksum-verified release into.local/runcobo/dbmate/on first use (needscurlandsha256sumorshasum). - 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:
postgresql://postgres@127.0.0.1:5432/<prefix>_development
mysql://root@127.0.0.1:3306/<prefix>_development
sqlite3:./.local/sqlite/<prefix>_development.sqlite3and the same with _test. To change them:
- Set
database_useranddatabase_portinruncobo.ymlif your server uses another user or port. - Or put a full URL — with a password, if needed — in
DEVELOPMENT_DATABASE_URLandTEST_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
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 sqlite3The environment argument defaults to development.
| Command | Environments | Notes |
|---|---|---|
ensure [ENV] | development, test | Create the database if missing |
prepare [ENV] | development, test | ensure, then migrate |
migrate [ENV] | development, test, staging, production | Database must already exist |
status [ENV] | development, test, staging, production | Doesn't change anything |
wait [ENV] | development, test, staging, production | Wait for the server to accept connections |
rollback [ENV] | development, test, staging | Not available for production — write a corrective migration instead |
seed [ENV] | development, test | Runs seed_command with DATABASE_URL set |
console [ENV] | development, test | psql, mysql or sqlite3 on the local database |
verify | test | Check every migration's rollback and replay |
snapshot | test | Write 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:
-- 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
bin/cobo db verifyIn 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
bin/cobo db snapshotWrites 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:
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,cobousessslmode=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,coboaddstls=true, which verifies the server certificate. Other values:skip-verify,preferred,false. - Run the migration files from the release you're deploying.
Useful options:
| Option | Commands | Description |
|---|---|---|
--wait | migrate, rollback, status | Wait for the server first |
--wait-timeout TIME | all | Max wait (default 60s) |
--wait-interval TIME | all | Time between attempts (default 1s) |
--migration-lock-timeout TIME | migrate, rollback | Max wait for the lock (default 5m). PostgreSQL only. |
-v, --verbose | migrate, rollback | Print each statement's result |
--exit-code | status | Exit 1 when migrations are pending |
--quiet | status | No output; implies --exit-code |