Skip to content

migrate

Run all pending migrations against a D1 database. Migrations are tracked in the _migrations table, which is created automatically on first run. Already-applied migrations are skipped, so this command is safe to run multiple times.

Synopsis

bash
npx @orphnet/d1-eloquent migrate --db <DB_NAME> [--local | --remote] [--atomic]

Options

FlagDescription
--db <DB_NAME>Required. The D1 binding name defined in wrangler.toml.
--localRun against the local D1 database (used with wrangler dev). Default when neither flag is provided.
--remoteRun against the Cloudflare-hosted D1 database.
--atomicWrap all pending migrations in a single transaction. Available for remote execution only.

Examples

Run pending migrations against a local database:

bash
npx @orphnet/d1-eloquent migrate --db MY_DB --local

Run pending migrations against the remote Cloudflare D1 database:

bash
npx @orphnet/d1-eloquent migrate --db MY_DB --remote

Run all pending migrations atomically on remote (all succeed or all roll back):

bash
npx @orphnet/d1-eloquent migrate --db MY_DB --remote --atomic

Notes

  • The _migrations table is created automatically if it does not exist.
  • Migration files are discovered in src/database/migrations/ and sorted by filename (timestamp prefix ensures correct order).
  • Use make:migration to generate new migration files. See Generators.

status

Check which migrations have been applied and which are still pending.

Synopsis

bash
npx @orphnet/d1-eloquent status --db <DB_NAME> [--local | --remote]

Options

FlagDescription
--db <DB_NAME>Required. The D1 binding name.
--localQuery the local D1 database.
--remoteQuery the Cloudflare-hosted D1 database.

Example

bash
npx @orphnet/d1-eloquent status --db MY_DB --local

Example output:

Migration                                    Status    Applied At
-----------------------------------------    -------   --------------------
20240101_000000_create_users_table.ts        APPLIED   2024-01-15 10:23:41
20240102_000000_create_posts_table.ts        APPLIED   2024-01-15 10:23:42
20240110_000000_add_soft_deletes_to_posts.ts PENDING   —

Released under the MIT License.