Skip to content

Commit 5121b23

Browse files
fix(wrangler): show error when D1 migration commands run without config file (#11711)
* fix(wrangler): show error when D1 migration commands run without config file Previously, running `wrangler d1 migrations apply`, `list`, or `create` in a directory without a wrangler.toml or wrangler.json would silently exit with no feedback. Now these commands display a clear error message guiding users to create a configuration file. Fixes #11709 * fix(wrangler): show error when D1 migration commands run without config file Previously, running `wrangler d1 migrations apply`, `list`, or `create` in a directory without a Wrangler configuration file would silently exit with no feedback. Now these commands display a clear error message guiding users to create a configuration file. Fixes #11709
1 parent 1223663 commit 5121b23

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

.changeset/itchy-squids-design.md

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+
Show an error when D1 migration commands are run without a configuration file
6+
7+
Previously, running `wrangler d1 migrations apply`, `wrangler d1 migrations list`, or `wrangler d1 migrations create` in a directory without a Wrangler configuration file would silently exit with no feedback. Now these commands display a clear error message:
8+
9+
"No configuration file found. Create a wrangler.jsonc file to define your D1 database."

packages/wrangler/src/__tests__/d1/migrate.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ describe("migrate", () => {
3232
runWrangler("d1 migrations create test some-message --local DATABASE")
3333
).rejects.toThrowError(`Unknown argument: local`);
3434
});
35+
36+
it("should error when no config file is present", async () => {
37+
setIsTTY(false);
38+
await expect(
39+
runWrangler("d1 migrations create DATABASE test-migration")
40+
).rejects.toThrowError(
41+
"No configuration file found. Create a wrangler.jsonc file to define your D1 database."
42+
);
43+
});
3544
});
3645

3746
describe("apply", () => {
@@ -69,6 +78,15 @@ describe("migrate", () => {
6978
).rejects.toThrowError(`No migrations present at <cwd>/migrations.`);
7079
});
7180

81+
it("should error when no config file is present", async () => {
82+
setIsTTY(false);
83+
await expect(
84+
runWrangler("d1 migrations apply DATABASE")
85+
).rejects.toThrowError(
86+
"No configuration file found. Create a wrangler.jsonc file to define your D1 database."
87+
);
88+
});
89+
7290
it("should reject the use of --preview with --local", async () => {
7391
setIsTTY(false);
7492
writeWranglerConfig({
@@ -209,6 +227,15 @@ Your database may not be available to serve requests during the migration, conti
209227
).rejects.toThrowError(`No migrations present at <cwd>/migrations.`);
210228
});
211229

230+
it("should error when no config file is present", async () => {
231+
setIsTTY(false);
232+
await expect(
233+
runWrangler("d1 migrations list DATABASE")
234+
).rejects.toThrowError(
235+
"No configuration file found. Create a wrangler.jsonc file to define your D1 database."
236+
);
237+
});
238+
212239
it("should use the custom migrations folder when provided", async () => {
213240
setIsTTY(false);
214241
writeWranglerConfig({

packages/wrangler/src/d1/migrations/apply.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ export const d1MigrationsApplyCommand = createCommand({
6868
},
6969
positionalArgs: ["database"],
7070
async handler({ database, local, remote, persistTo, preview }, { config }) {
71+
if (!config.configPath) {
72+
throw new UserError(
73+
"No configuration file found. Create a wrangler.jsonc file to define your D1 database."
74+
);
75+
}
76+
7177
const databaseInfo = getDatabaseInfoFromConfig(config, database);
7278

7379
if (!databaseInfo && remote) {
@@ -76,10 +82,6 @@ export const d1MigrationsApplyCommand = createCommand({
7682
);
7783
}
7884

79-
if (!config.configPath) {
80-
return;
81-
}
82-
8385
const migrationsPath = await getMigrationsPath({
8486
projectPath: path.dirname(config.configPath),
8587
migrationsFolderPath:

packages/wrangler/src/d1/migrations/create.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,19 @@ export const d1MigrationsCreateCommand = createCommand({
4040
},
4141
positionalArgs: ["database", "message"],
4242
async handler({ database, message }, { config }) {
43+
if (!config.configPath) {
44+
throw new UserError(
45+
"No configuration file found. Create a wrangler.jsonc file to define your D1 database."
46+
);
47+
}
48+
4349
const databaseInfo = getDatabaseInfoFromConfig(config, database);
4450
if (!databaseInfo) {
4551
throw new UserError(
4652
`Couldn't find a D1 DB with the name or binding '${database}' in your ${configFileName(config.configPath)} file.`
4753
);
4854
}
4955

50-
if (!config.configPath) {
51-
return;
52-
}
53-
5456
const migrationsPath = await getMigrationsPath({
5557
projectPath: path.dirname(config.configPath),
5658
migrationsFolderPath:

packages/wrangler/src/d1/migrations/list.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,19 @@ export const d1MigrationsListCommand = createCommand({
5454
await requireAuth({});
5555
}
5656

57+
if (!config.configPath) {
58+
throw new UserError(
59+
"No configuration file found. Create a wrangler.jsonc file to define your D1 database."
60+
);
61+
}
62+
5763
const databaseInfo = getDatabaseInfoFromConfig(config, database);
5864
if (!databaseInfo && remote) {
5965
throw new UserError(
6066
`Couldn't find a D1 DB with the name or binding '${database}' in your ${configFileName(config.configPath)} file.`
6167
);
6268
}
6369

64-
if (!config.configPath) {
65-
return;
66-
}
67-
6870
const migrationsPath = await getMigrationsPath({
6971
projectPath: path.dirname(config.configPath),
7072
migrationsFolderPath:

0 commit comments

Comments
 (0)