19 Replies
Hmmm, you could try applying migrations before constructing
new Miniflare()
maybe? You could use child_process.execSync("pnpm wrangler d1 migrations apply snow-db --local", { stdio: "inherit" })
for running migrations, then you wouldn't have the worry about creating that extra Promise
.
An alternative solution would be to just read and apply the migrations yourself using Miniflare's API. Something like...
Unfortunately, this requires each of the statements in your migrations to be on their own line, but you could probably get around that with something like https://www.npmjs.com/package/@databases/split-sql-query.Each statement on its own line, as in 100% of each statement in its own single line?
Surprisingly this results in
D1_EXEC_ERROR: Error in line 1: CREATE TABLE posts (id integer PRIMARY KEY AUTOINCREMENT, title text NOT NULL, author text NOT NULL);: SQL code did not contain a statement.
Yeah π
Does it work if you remove the
;
at the end?Nope, same error π¦
Any chance you're using Windows?
Currently I am, yes
Want me to try on mac? Lol
Maybe try
migration = migration.split("\r\n").slice(1).join("\n");
instead
Or migration = migration.split("\r\n").slice(1).map(l => l.trim()).filter(Boolean).join("\n");
D1_EXEC_ERROR: Error in line 1: : No SQL statements detected.
One last go,
migration = migration.split(/\r?\n/).slice(1).map(l => l.trim()).filter(Boolean).join("\n");
π
Hah, well it runs without a
D1_EXEC_ERROR
, but it doesn't seem to run the migration as I now get D1_ERROR: no such table: posts
(β―Β°β‘Β°)β―οΈ΅ β»ββ»
what does
migration
look like after that line? β¬ββ¬γ( ΒΊ _ ΒΊγ)Actually I'm wrong... I accidentally deleted the
await DB.exec(migration);
.... it's back to Error in line 1: CREATE ... SQL code did not contain a statement.
Hmmm, not too sure what's going on here. If you call
DB.exec("CREATE TABLE ...")
directly, does that work?Yes
My migration creates the table and seeds 1 row. If I do those in seperate
DB.exec()
calls directly it worksHmmm, ok, I'd probably just stick with that for now. Not ideal, but really not sure what's going wrong here.
No worries, thank you for trying!!
Is there anything planned in Miniflare to make it easier to work with D1 (and migrations, specifically)? Want me to raise a feature request or something π
Definitely things planned, I'm currently working on porting Miniflare 2's unit testing environments to Miniflare 3. An aim of this new version is to make migrations super easy. π
Can also confirm that it looks like miniflare is not using the DB created when migrations are applied. It's creating a new sqlite db.
If you use wrangler d1 migrations apply
Damn. Even ensuring the --persist-to configs are set to the same dir, still creating a new sqlite db
Hey! π It looks like you're running the
wrangler d1 migrations apply
command inside a pnpm
workspace project. This means the migrations are applied in $PWD/api/.wrangler/test_state
whereas new Miniflare()
appears to be pointed to $PWD/.wrangler/test_state
. Try change the d1Persist
option when constructing new Miniflare()
to api/.wrangler/test_state/v3/d1
. I'd also recommend apply migrations before constructing new Miniflare()
, so Miniflare starts up with migrations already applied.