Skip to content

Drizzle migrations ​

Narew keeps one Drizzle history: application schema SQL generated by drizzle-kit, and Narew metadata SQL appended by narew-cms generate through Kit's supported custom-migration workflow. Both live in your project's drizzle/ directory.

Drizzle configuration ​

Create drizzle.config.ts at the project root:

ts
export { drizzleConfig as default } from "@narew/drizzle/kit";

The default export comes from @narew/drizzle/kit: SQLite dialect, ./narew.config.ts as the schema entry point, ./drizzle as the migration directory, and tablesFilter: ["*", "!__narew_*"] to keep Narew's reserved tables out of ORM introspection. No database credentials are needed: generation works on schema files only.

If the tables live in a separate module, pass the path explicitly:

ts
import { createDrizzleConfig } from "@narew/drizzle/kit";

export default createDrizzleConfig({ schema: "./src/db/schema.ts" });

Generate ​

Run from the project package directory, in this order:

sh
pnpm exec drizzle-kit generate
pnpm exec narew-cms generate

The first command produces the application migration (drizzle/0000_*.sql; the file name is Kit's, so treat it as illustrative). The second appends a custom migration (drizzle/0001_*_narew.sql) carrying the compiled __narew_* metadata SQL, plus drizzle/narew.snapshot.json.

Each tool reads its own configuration: drizzle-kit reads drizzle.config.ts from the current directory, and narew-cms reads both narew.config.ts and drizzle.config.ts. The --cwd, --config, and --drizzle-config overrides belong to narew-cms only; drizzle-kit has its own --config flag.

  • Schema change → always run drizzle-kit generate first, then narew-cms generate. Adding or changing exposed columns changes the inferred metadata too, so the second command may append a custom migration; if the metadata genuinely did not change, it reports no changes and writes nothing.
  • Metadata-only change (labels, widgets, display in narew.config.ts) → run narew-cms generate; it appends a custom migration.
  • narew-cms generate --check --json exits 1 with METADATA_DRIFT when a custom migration is due. Use it in CI.
  • narew-cms generate --dry-run --json exits 0 and writes nothing. --check and --dry-run are mutually exclusive.

Commit the history ​

Commit drizzle/*.sql, drizzle/meta/_journal.json plus Kit's snapshots, and drizzle/narew.snapshot.json together; they are one coherent history.

Never hand-edit applied SQL or the journal, never add __narew_* tables to your ORM schema, and never run other history writers concurrently with narew-cms generate.

Apply to D1 ​

There is no narew-cms migrate command. Apply the reviewed history with the Wrangler CLI. Point Wrangler at the project's drizzle/ directory in wrangler.jsonc:

json
{
	"name": "narew-blog-site",
	"main": "src/worker.ts",
	"compatibility_date": "2026-10-13",
	"compatibility_flags": ["nodejs_compat"],
	// The D1 database registered in Narew for this environment.
	"d1_databases": [
		{
			"binding": "DB",
			"database_name": "narew-blog",
			"database_id": "00000000-0000-0000-0000-000000000000",
			// Wrangler applies the reviewed Drizzle history from this directory.
			"migrations_dir": "drizzle",
		},
	],
	"durable_objects": {
		"bindings": [{ "name": "NAREW_SESSIONS", "class_name": "ContentSession" }],
	},
	"migrations": [
		{ "tag": "preview-v1", "new_sqlite_classes": ["PreviewReplica"] },
		{
			"tag": "content-session-v1",
			"renamed_classes": [{ "from": "PreviewReplica", "to": "ContentSession" }],
		},
	],
	"vars": {
		// Public base URL configured on the registered media bucket
		// (Dashboard → Media → Storage). Empty only disables URL construction;
		// bucket access is configured separately.
		"MEDIA_BASE_URL": "https://pub-00000000000000000000000000000000.r2.dev",
		// Set once for this environment. NAREW_SESSION_PUBLIC_JWK is a public verification key.
		"NAREW_CMS_ORIGIN": "",
		"NAREW_SESSION_PUBLIC_JWK": "",
	},
}

Then apply the pending files locally or remotely:

sh
pnpm exec wrangler d1 migrations apply <DB_NAME> --local
pnpm exec wrangler d1 migrations apply <DB_NAME> --remote

Wrangler applies the .sql files in order, records them in its d1_migrations table, and rolls back a failed migration before the previous successful one. Review the generated SQL first, and use the same reviewed drizzle/ history for local and remote targets. Do not also use drizzle-kit migrate for D1 over HTTP: its d1-http driver sends each statement in a separate request.

Remote application is a real database mutation, so select an authorized non-production target and provide Cloudflare credentials through Wrangler's own login or a secure CI secret.

narew-cms content bind appends a reviewed __narew_content_binding custom migration through the same history and writes a companion narew.content-binding.snapshot.json — see Website preview.

Migrating scalar columns to localized JSON ​

narew-cms generate does not automatically generate data migrations when converting existing scalar columns (e.g. TEXT) to localized JSON columns (localized.text() or localized.richText()).

Converting existing content requires an application-level migration with explicit data conversion, verification, and rollback planning:

  1. Backup existing rows: Create a staging backup before altering data:
    sql
    CREATE TABLE backup_posts AS SELECT * FROM posts;
  2. Convert values to language-keyed JSON: Non-empty strings and explicit empty strings ("") map to {"<defaultLocale>": "<value>"}, preserving empty string semantics. NULL remains NULL:
    sql
    UPDATE posts
    SET title = json_object('en', title),
        summary = CASE WHEN summary IS NULL THEN NULL ELSE json_object('en', summary) END;
  3. Verify and roll back if needed: Confirm row counts and field integrity before dropping backup tables. Byte-for-byte rollback can restore from the backup table:
    sql
    DELETE FROM posts;
    INSERT INTO posts SELECT id, title, summary FROM backup_posts;

This migration pattern is verified in tooling/localized-migration-rehearsal.test.ts.