Skip to content

chkit generate

Compares your current TypeScript schema definitions against the previous snapshot, computes a migration plan, and writes migration SQL and an updated snapshot.

chkit generate [flags]
FlagTypeDefaultDescription
--name <name>stringMigration name (used in the filename)
--migration-id <id>stringEscape hatch: override the default timestamp migration prefix
--rename-table <mapping>stringExplicit table rename: old_db.old_table=new_db.new_table
--rename-column <mapping>stringExplicit column rename: db.table.old_column=new_column
--table <selector>stringScope operations to matching tables
--dryrunbooleanfalsePrint the plan without writing any files
--emptybooleanfalseScaffold a blank manual migration without diffing the schema

Global flags documented on CLI Overview.

  1. Loads your config and schema definitions
  2. Reads the previous snapshot from metaDir/snapshot.json
  3. Computes a diff between old and new definitions using planDiff() from @chkit/core
  4. Produces an ordered list of SQL operations

If there are no differences, no migration file is created.

Every operation in the plan is assigned a risk level:

RiskMeaningExample operations
safeNon-destructive, no data lossCREATE TABLE, ADD COLUMN
cautionPotentially impactful, review recommendedALTER TABLE settings changes
dangerDestructive, may cause data lossDROP TABLE, DROP COLUMN

The --table flag limits operations to tables matching a selector:

  • database.table — exact match
  • database.prefix* — prefix wildcard in a specific database
  • table — matches across all databases

An empty match set emits a warning and produces no output.

chkit detects potential renames through two mechanisms:

  1. Schema metadata — set renamedFrom on your schema definition
  2. CLI flags--rename-table old_db.old_table=new_db.new_table and --rename-column db.table.old_col=new_col

CLI flags take priority when both sources specify a mapping for the same object. Rename flags accept comma-separated values for multiple mappings.

Validation errors are raised for conflicting, chained, or cyclic rename mappings.

With --dryrun, the command prints the migration plan (operations with risk levels and SQL) without writing any files. Useful for previewing changes before committing.

Plans for objects in a database lead with a create_database operation (CREATE DATABASE IF NOT EXISTS, risk safe), so a single new table reports operationCount: 2 — the create_database plus the create_table. The CREATE DATABASE is idempotent and a no-op if the database already exists.

With --empty, the command skips the schema diff entirely and writes a blank, timestamped migration stub for you to hand-edit. Use it for DDL that chkit does not model — raw INSERT/backfill statements, OPTIMIZE, manual dictionary reloads, or one-off data fixes.

The stub carries the standard migration header (with operation-count: 0) plus a placeholder comment. The snapshot is left untouched, so an empty migration never absorbs pending schema drift. The --name and --migration-id flags apply; without --name, the file defaults to manual. Schema-diff flags (--table, --rename-table, --rename-column, --dryrun) are not used in empty mode.

chkit migrate picks the file up like any other migration and applies it in filename order. Write your SQL into the stub before applying it — editing a migration after it has run triggers a checksum mismatch.

If the codegen plugin is configured with runOnGenerate: true (the default), chkit generate automatically runs codegen after writing migration artifacts. A codegen failure causes generate to fail.

Schema validation issues (such as invalid definitions) produce a validation_failed error with structured issue codes and messages. The process exits with code 1.

Generate a named migration:

Terminal window
chkit generate --name add_users_table

Preview changes without writing files:

Terminal window
chkit generate --dryrun

Scaffold a blank manual migration:

Terminal window
chkit generate --empty --name backfill_signups

Scope to a specific table:

Terminal window
chkit generate --table analytics.events

Explicit table rename:

Terminal window
chkit generate --rename-table old_db.users=new_db.accounts

Explicit column rename:

Terminal window
chkit generate --rename-column analytics.events.old_name=new_name
CodeMeaning
0Success
1Validation error
{
"command": "generate",
"schemaVersion": 1,
"scope": { "enabled": false },
"mode": "plan",
"operationCount": 2,
"riskSummary": { "safe": 2, "caution": 0, "danger": 0 },
"operations": [
{ "type": "create_database", "key": "database:default", "risk": "safe", "sql": "CREATE DATABASE IF NOT EXISTS default;" },
{ "type": "create_table", "key": "default.users", "risk": "safe", "sql": "CREATE TABLE ..." }
],
"renameSuggestions": []
}
{
"command": "generate",
"schemaVersion": 1,
"scope": { "enabled": false },
"migrationFile": "./chkit/migrations/20260604104251_add_users_table.sql",
"snapshotFile": "./chkit/meta/snapshot.json",
"definitionCount": 3,
"operationCount": 2,
"riskSummary": { "safe": 2, "caution": 0, "danger": 0 }
}
{
"command": "generate",
"schemaVersion": 1,
"mode": "empty",
"migrationFile": "./chkit/migrations/20260604104251_backfill_signups.sql"
}
{
"command": "generate",
"schemaVersion": 1,
"error": "validation_failed",
"issues": [{ "code": "...", "message": "..." }]
}