diff options
author | 2023-01-22 19:09:52 -0800 | |
---|---|---|
committer | 2023-01-22 19:09:52 -0800 | |
commit | 349224869751c9d74540d5de75bf0681c69dd45d (patch) | |
tree | 7dc7f48a085a217f187b80a445b351a7e4f332eb /test/bun.js | |
parent | bc7192dca1c9da8714f72be3848100e9d16a5425 (diff) | |
download | bun-349224869751c9d74540d5de75bf0681c69dd45d.tar.gz bun-349224869751c9d74540d5de75bf0681c69dd45d.tar.zst bun-349224869751c9d74540d5de75bf0681c69dd45d.zip |
Fixes #1366
Diffstat (limited to '')
-rw-r--r-- | test/bun.js/sqlite-cross-process.js | 45 | ||||
-rw-r--r-- | test/bun.js/sqlite.test.js | 26 |
2 files changed, 70 insertions, 1 deletions
diff --git a/test/bun.js/sqlite-cross-process.js b/test/bun.js/sqlite-cross-process.js new file mode 100644 index 000000000..d5b9b87b5 --- /dev/null +++ b/test/bun.js/sqlite-cross-process.js @@ -0,0 +1,45 @@ +// https://github.com/oven-sh/bun/issues/1366 +import { Database } from "bun:sqlite"; +import { rmSync } from "fs"; + +const dir = process.env.SQLITE_DIR; + +rmSync(dir + "get-persist.sqlite", { force: true }); + +var db = Database.open(dir + "get-persist.sqlite", { create: true }); + +// Note, I've played with various values and it doesn't seem to change +// the behavior. The "beter-sqlite3" npm package does not exhibit this +// bug, so it doesn't seem to be a general SQLite thing. +db.run(`PRAGMA journal_mode = WAL`); +db.run(`PRAGMA synchrounous = NORMAL`); + +db.run( + `CREATE TABLE IF NOT EXISTS examples ( + id TEXT PRIMARY KEY + )`, +); + +// This persists, but if you place this call +db.run( + ` + INSERT INTO examples + VALUES ('hello') + ON CONFLICT (id) DO + UPDATE SET id='hello' + RETURNING id + `, +); + +db.query(`SELECT id FROM examples WHERE id='hello'`).get().id; +db.query( + ` +INSERT INTO examples +VALUES ('world') +ON CONFLICT (id) DO + UPDATE SET id='world' +RETURNING id +`, +).get(); + +process.exit(0); diff --git a/test/bun.js/sqlite.test.js b/test/bun.js/sqlite.test.js index e5f83fe4d..f78d3b481 100644 --- a/test/bun.js/sqlite.test.js +++ b/test/bun.js/sqlite.test.js @@ -1,6 +1,9 @@ import { expect, it, describe } from "bun:test"; import { Database, constants } from "bun:sqlite"; -import { existsSync, fstat, writeFileSync } from "fs"; +import { existsSync, fstat, realpathSync, rmSync, writeFileSync } from "fs"; +import { spawnSync } from "bun"; +import { bunExe } from "bunExe"; +import { tmpdir } from "os"; var encode = (text) => new TextEncoder().encode(text); it("Database.open", () => { @@ -55,6 +58,27 @@ it("Database.open", () => { new Database().close(); }); +it("upsert cross-process, see #1366", () => { + const dir = realpathSync(tmpdir()) + "/"; + const { exitCode } = spawnSync( + [bunExe(), import.meta.dir + "/sqlite-cross-process.js"], + { + env: { + SQLITE_DIR: dir, + }, + stderr: "inherit", + }, + ); + expect(exitCode).toBe(0); + + const db2 = Database.open(dir + "get-persist.sqlite"); + + expect(db2.query(`SELECT id FROM examples`).all()).toEqual([ + { id: "hello" }, + { id: "world" }, + ]); +}); + it("creates", () => { const db = Database.open(":memory:"); db.exec( |