Skip to content

Commit c6c61b5

Browse files
authored
CFSQL-1589 migrations_pattern for configuring recursive migration discovery (#14089)
1 parent 8400fb9 commit c6c61b5

15 files changed

Lines changed: 2008 additions & 172 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Restore the D1 `executeSql` logger level via try/finally
6+
7+
`wrangler d1 execute --json` and the internal `executeSql` helper temporarily lower the global logger to `"error"` to keep human-readable output out of the JSON payload. Previously the level was restored only on the happy path, so any early return or thrown error left the singleton logger muted, silencing later `logger.warn`/`logger.log` output (notably from migration helpers that wrap `executeSql` and are commonly mocked in tests).
8+
9+
The level swap is now wrapped in `try`/`finally` so it is always restored.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
"wrangler": minor
3+
"@cloudflare/workers-utils": minor
4+
---
5+
6+
Add `migrations_pattern` to D1 database bindings
7+
8+
The D1 binding now accepts an optional `migrations_pattern` field, allowing you to point `wrangler d1 migrations apply` and `wrangler d1 migrations list` at migration files in nested layouts (e.g. ORM-generated folders like `migrations/0000_init/migration.sql`).
9+
10+
`migrations_pattern` is a glob (relative to the wrangler config file) and defaults to `${migrations_dir}/*.sql`, which preserves today's behaviour. Files that do not match the pattern are not executed.
11+
12+
```jsonc
13+
{
14+
"d1_databases": [
15+
{
16+
"binding": "DB",
17+
"database_name": "my-db",
18+
"database_id": "...",
19+
"migrations_dir": "migrations",
20+
"migrations_pattern": "migrations/*/migration.sql",
21+
},
22+
],
23+
}
24+
```
25+
26+
When no migrations match the configured pattern but files matching the common `migrations/*/migration.sql` (drizzle-style) layout do exist, Wrangler logs a hint suggesting `migrations_pattern` as an opt-in.
27+
28+
`wrangler d1 migrations create` now returns an actionable error if the generated migration filename would not match the configured pattern.

packages/workers-utils/src/config/environment.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,21 @@ export interface EnvironmentNonInheritable {
990990
migrations_table?: string;
991991
/** The path to the directory of migrations for this D1 database (defaults to './migrations'). */
992992
migrations_dir?: string;
993+
/**
994+
* A glob pattern (relative to the Wrangler config file) used to discover
995+
* migration files for this D1 database. Defaults to `${migrations_dir}/*.sql`
996+
* if not specified.
997+
*
998+
* Use this to opt in to nested layouts such as `migrations/*\/migration.sql`
999+
* (as produced by some ORMs).
1000+
*
1001+
* When `migrations_pattern` is set, `migrations_dir` must also be set, and
1002+
* `migrations_pattern` must start with `${migrations_dir}/`. This keeps the
1003+
* relationship between the two settings explicit and lets Wrangler record
1004+
* each migration's name in the migrations table as a path relative to
1005+
* `migrations_dir`.
1006+
*/
1007+
migrations_pattern?: string;
9931008
/** Internal use only. */
9941009
database_internal_env?: string;
9951010
/** Whether the D1 database should be remote or not in local development */

packages/workers-utils/src/config/validation.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4117,12 +4117,22 @@ const validateD1Binding: ValidatorFn = (diagnostics, field, value) => {
41174117
isValid = false;
41184118
}
41194119

4120+
if (!isOptionalProperty(value, "migrations_pattern", "string")) {
4121+
diagnostics.errors.push(
4122+
`"${field}" bindings should, optionally, have a string "migrations_pattern" field but got ${JSON.stringify(
4123+
value
4124+
)}.`
4125+
);
4126+
isValid = false;
4127+
}
4128+
41204129
validateAdditionalProperties(diagnostics, field, Object.keys(value), [
41214130
"binding",
41224131
"database_id",
41234132
"database_internal_env",
41244133
"database_name",
41254134
"migrations_dir",
4135+
"migrations_pattern",
41264136
"migrations_table",
41274137
"preview_database_id",
41284138
"remote",

packages/workers-utils/src/worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ export interface CfD1Database {
220220
database_internal_env?: string;
221221
migrations_table?: string;
222222
migrations_dir?: string;
223+
migrations_pattern?: string;
223224
remote?: boolean;
224225
raw?: boolean;
225226
}

0 commit comments

Comments
 (0)