Skip to content

Commit

Permalink
Add local d1 migrations (#2260)
Browse files Browse the repository at this point in the history
* fix: make it possible to use a local db for migrations

* Create violet-lamps-cry.md

* Update execute.tsx

* Update execute.tsx

* Update apply.tsx

* Update packages/wrangler/src/d1/migrations/apply.tsx

Co-authored-by: Somhairle MacLeòid <samuel@macleod.space>

* Update apply.tsx

* Update helpers.ts

* Update execute.tsx

* add isInteractiveAndNotCI

* Revert "add isInteractiveAndNotCI"

This reverts commit 02cb33f.

* use isCI

Co-authored-by: Somhairle MacLeòid <samuel@macleod.space>
  • Loading branch information
rozenmd and penalosa committed Nov 23, 2022
1 parent 60d31c0 commit c294016
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/violet-lamps-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: make it possible to use a local db for d1 migrations

As of this change, wrangler's d1 migrations commands now accept `local` and `persist-to` as flags, so migrations can run against the local d1 db.
19 changes: 15 additions & 4 deletions packages/wrangler/src/d1/migrations/apply.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Table from "ink-table";
import React from "react";
import { withConfig } from "../../config";
import { confirm } from "../../dialogs";
import { CI } from "../../is-ci";
import isInteractive from "../../is-interactive";
import { logger } from "../../logger";
import { requireAuth } from "../../user";
import { createBackup } from "../backups";
Expand All @@ -21,7 +23,17 @@ import type { BaseSqlExecuteArgs } from "../execute";
import type { Argv } from "yargs";

export function ApplyOptions(yargs: Argv): Argv<BaseSqlExecuteArgs> {
return Database(yargs);
return Database(yargs)
.option("local", {
describe:
"Execute commands/files against a local DB for use with wrangler dev --local",
type: "boolean",
})
.option("persist-to", {
describe: "Specify directory to use for local persistence (for --local)",
type: "string",
requiresArg: true,
});
}

export const ApplyHandler = withConfig<BaseSqlExecuteArgs>(
Expand Down Expand Up @@ -88,8 +100,7 @@ export const ApplyHandler = withConfig<BaseSqlExecuteArgs>(
return;
}

const isInteractive = process.stdout.isTTY;
if (isInteractive) {
if (isInteractive() && !CI.isCI()) {
const ok = await confirm(
`About to apply ${unappliedMigrations.length} migration(s)\n` +
"Your database may not be available to serve requests during the migration, continue?",
Expand Down Expand Up @@ -121,7 +132,7 @@ export const ApplyHandler = withConfig<BaseSqlExecuteArgs>(
local,
config,
database,
undefined,
isInteractive() && !CI.isCI(),
persistTo,
undefined,
query
Expand Down
6 changes: 4 additions & 2 deletions packages/wrangler/src/d1/migrations/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fs from "node:fs";
import path from "path";
import { confirm } from "../../dialogs";
import { CI } from "../../is-ci";
import isInteractive from "../../is-interactive";
import { logger } from "../../logger";
import { DEFAULT_MIGRATION_PATH } from "../constants";
import { executeSql } from "../execute";
Expand Down Expand Up @@ -79,7 +81,7 @@ const listAppliedMigrations = async (
local,
config,
name,
undefined,
isInteractive() && !CI.isCI(),
persistTo,
undefined,
Query
Expand Down Expand Up @@ -130,7 +132,7 @@ export const initMigrationsTable = async (
local,
config,
name,
undefined,
isInteractive() && !CI.isCI(),
persistTo,
undefined,
`
Expand Down
12 changes: 11 additions & 1 deletion packages/wrangler/src/d1/migrations/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ import type { BaseSqlExecuteArgs } from "../execute";
import type { Argv } from "yargs";

export function ListOptions(yargs: Argv): Argv<BaseSqlExecuteArgs> {
return Database(yargs);
return Database(yargs)
.option("local", {
describe:
"Execute commands/files against a local DB for use with wrangler dev",
type: "boolean",
})
.option("persist-to", {
describe: "Specify directory to use for local persistence (for --local)",
type: "string",
requiresArg: true,
});
}

export const ListHandler = withConfig<BaseSqlExecuteArgs>(
Expand Down

0 comments on commit c294016

Please sign in to comment.