Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite: allow foreign_key_check for whole DB, not just a single table #2479

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/workerd/api/sql-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import * as assert from 'node:assert'

async function test(storage) {
async function test(state) {
const storage = state.storage
const sql = storage.sql
// Test numeric results
const resultNumber = [...sql.exec('SELECT 123')]
Expand Down Expand Up @@ -889,6 +890,36 @@ async function test(storage) {
`),
'Error: not authorized'
)

// Assert foreign keys can be truly turned off, not just deferred
await state.blockConcurrencyWhile(async () => {
sql.exec(`PRAGMA foreign_keys = OFF;`)
})
storage.transactionSync(() => {
sql.exec(`
CREATE TABLE A (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
bId INTEGER NOT NULL REFERENCES B (id) ON DELETE RESTRICT ON UPDATE CASCADE
);
INSERT INTO A VALUES(1,1); -- this would throw a parse error with foreign keys on
CREATE TABLE B (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
);
`)
})

// Until we've inserted the row into B, we can detect our
// foreign key violation (even with foreign_keys=OFF)
assert.deepEqual(Array.from(sql.exec(`pragma foreign_key_check;`)), [
{ table: 'A', rowid: 1, parent: 'B', fkid: 0 },
])
sql.exec(`INSERT INTO B VALUES (1);`)
assert.deepEqual(Array.from(sql.exec(`pragma foreign_key_check;`)), [])

// Restore foreign keys for the rest of the tests
await state.blockConcurrencyWhile(async () => {
sql.exec(`PRAGMA foreign_keys = ON;`)
})
}

async function testIoStats(storage) {
Expand Down Expand Up @@ -1097,7 +1128,7 @@ export class DurableObjectExample {

async fetch(req) {
if (req.url.endsWith('/sql-test')) {
await test(this.state.storage)
await test(this.state)
return Response.json({ ok: true })
} else if (req.url.endsWith('/sql-test-foreign-keys')) {
await testForeignKeys(this.state.storage)
Expand Down
7 changes: 6 additions & 1 deletion src/workerd/util/sqlite.c++
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ enum class PragmaSignature {
NO_ARG,
BOOLEAN,
OBJECT_NAME,
OPTIONAL_OBJECT_NAME,
NULL_NUMBER_OR_OBJECT_NAME
};
struct PragmaInfo {
Expand All @@ -350,7 +351,7 @@ static constexpr PragmaInfo ALLOWED_PRAGMAS[] = {
{ "reverse_unordered_selects"_kj, PragmaSignature::BOOLEAN },

// Takes an argument of table name or index name, returns info about it.
{ "foreign_key_check"_kj, PragmaSignature::OBJECT_NAME },
{ "foreign_key_check"_kj, PragmaSignature::OPTIONAL_OBJECT_NAME },
{ "foreign_key_list"_kj, PragmaSignature::OBJECT_NAME },
{ "index_info"_kj, PragmaSignature::OBJECT_NAME },
{ "index_list"_kj, PragmaSignature::OBJECT_NAME },
Expand Down Expand Up @@ -703,6 +704,10 @@ bool SqliteDatabase::isAuthorized(int actionCode,
auto val = KJ_UNWRAP_OR(param2, return false);
return regulator.isAllowedName(val);
}
case PragmaSignature::OPTIONAL_OBJECT_NAME: {
auto val = KJ_UNWRAP_OR(param2, return true);
return regulator.isAllowedName(val);
}
case PragmaSignature::NULL_NUMBER_OR_OBJECT_NAME: {
// Argument is not required
auto val = KJ_UNWRAP_OR(param2, return true);
Expand Down
Loading