Skip to content

Commit

Permalink
Cater for new local D1 database location (#53)
Browse files Browse the repository at this point in the history
* Cater for new local D1 database location

* Seems "DB.sqlite3" => "db.sqlite"

* Add changeset and remove tool-versions file

---------

Co-authored-by: Josh Larson <jplhomer@gmail.com>
  • Loading branch information
dan-gamble and jplhomer committed Dec 15, 2023
1 parent 1dba901 commit aaebb5b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-eggs-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"superflare": patch
---

Fix the name and location of the local SQLite db
2 changes: 1 addition & 1 deletion packages/superflare/cli/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function consoleOptions(yargs: CommonYargsArgv) {
".wrangler",
"state",
"d1",
"DB.sqlite3"
"db.sqlite"
),
})
.option("models", {
Expand Down
2 changes: 1 addition & 1 deletion packages/superflare/cli/db/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function seedOptions(yargs: CommonYargsArgv) {
".wrangler",
"state",
"d1",
"DB.sqlite3"
"db.sqlite"
),
})
.option("seed-path", {
Expand Down
38 changes: 31 additions & 7 deletions packages/superflare/cli/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CommonYargsArgv, StrictYargsOptionsToInterface } from "./yargs-types";
import path from "node:path";
import fs, { mkdir, readdir, writeFile } from "node:fs/promises";
import fsSync from 'node:fs'
import { logger } from "./logger";
import { wranglerMigrate } from "./wrangler";
import { generateTypesFromSqlite, syncSuperflareTypes } from "./d1-types";
Expand All @@ -13,20 +14,43 @@ export function defaultSuperflareMigrationsPath(rootPath = process.cwd()) {
return path.join(rootPath, "db", "migrations");
}

export function defaultSuperflareDatabasePath (rootPath = process.cwd()) {
try {
const wranglerConfig = fsSync.readFileSync(
path.join(rootPath, 'wrangler.json'),
'utf8'
)
const wranglerConfigJson = JSON.parse(wranglerConfig)
const d1DatabaseId = wranglerConfigJson?.d1_databases?.[0]?.database_id

return path.join(
rootPath,
".wrangler",
"state",
"v3",
"d1",
d1DatabaseId,
"db.sqlite"
)
} catch (e) {
return path.join(
rootPath,
".wrangler",
"state",
"d1",
"db.sqlite"
)
}
}

export function migrateOptions(yargs: CommonYargsArgv) {
return yargs
.option("db", {
alias: "d",
describe: "Path to the database",

// Default to the path in the .wrangler directory
default: path.join(
process.cwd(),
".wrangler",
"state",
"d1",
"DB.sqlite3"
),
default: defaultSuperflareDatabasePath(),
})
.option("models", {
alias: "m",
Expand Down

0 comments on commit aaebb5b

Please sign in to comment.