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: handle exceptions in filter callback database.applyChangeset() #56903

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions doc/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ added:

* `changeset` {Uint8Array} A binary changeset or patchset.
* `options` {Object} The configuration options for how the changes will be applied.
* `filter` {Function} Skip changes that, when targeted table name is supplied to this function, return a truthy value.
By default, all changes are attempted.
* `filter` {Function} A table name is provided as an argument to this callback. Returning a truthy value means changes
for the table with that table name should be attempted. By default, changes for all tables are attempted.
* `onConflict` {Function} A function that determines how to handle conflicts. The function receives one argument,
which can be one of the following values:

Expand Down
20 changes: 14 additions & 6 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -825,13 +825,21 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
Local<Function> filterFunc = filterValue.As<Function>();

filterCallback = [env, filterFunc](std::string item) -> bool {
TryCatch try_catch(env->isolate());
Local<Value> argv[] = {String::NewFromUtf8(env->isolate(),
item.c_str(),
NewStringType::kNormal)
.ToLocalChecked()};
Local<Value> result =
filterFunc->Call(env->context(), Null(env->isolate()), 1, argv)
.ToLocalChecked();
item.c_str(),
NewStringType::kNormal)
.ToLocalChecked()};
MaybeLocal<Value> maybe_result =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need the TryCatch here. The problem is the use of ToLocalChecked(). Take a look at this code. You can tell if V8 has an exception pending if the ToLocal() call does not succeed.

filterFunc->Call(env->context(), Null(env->isolate()), 1, argv);
if (try_catch.HasCaught()) {
try_catch.ReThrow();
return false;
}
if (maybe_result.IsEmpty()) {
return false;
}
Local<Value> result = maybe_result.ToLocalChecked();
return result->BooleanValue(env->isolate());
};
}
Expand Down
110 changes: 93 additions & 17 deletions test/parallel/test-sqlite-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,29 +361,105 @@ suite('conflict resolution', () => {
});
});

test('database.createSession() - filter changes', (t) => {
const database1 = new DatabaseSync(':memory:');
const database2 = new DatabaseSync(':memory:');
const createTableSql = 'CREATE TABLE data1(key INTEGER PRIMARY KEY); CREATE TABLE data2(key INTEGER PRIMARY KEY);';
database1.exec(createTableSql);
database2.exec(createTableSql);
suite('filter tables', () => {
function testFilter(t, options) {
const database1 = new DatabaseSync(':memory:');
const database2 = new DatabaseSync(':memory:');
const createTableSql = 'CREATE TABLE data1(key INTEGER PRIMARY KEY); CREATE TABLE data2(key INTEGER PRIMARY KEY);';

database1.exec(createTableSql);
database2.exec(createTableSql);

const session = database1.createSession();
database1.exec('INSERT INTO data1 (key) VALUES (1), (2), (3)');
database1.exec('INSERT INTO data2 (key) VALUES (1), (2), (3), (4), (5)');

const applyChangeset = () => database2.applyChangeset(session.changeset(), {
...(options.filter ? { filter: options.filter } : {})
});
if (options.error) {
t.assert.throws(applyChangeset, options.error);
} else {
try {
applyChangeset();
} catch (error) {
if (!options.expectError) throw error;
}
}

t.assert.strictEqual(database2.prepare('SELECT * FROM data1').all().length, options.data1);
t.assert.strictEqual(database2.prepare('SELECT * FROM data2').all().length, options.data2);
}

test('database.createSession() - filter one table', (t) => {
testFilter(t, {
filter: (tableName) => tableName === 'data2',
// Only changes from data2 should be included
data1: 0,
data2: 5
});
});

test('database.createSession() - throw in filter callback', (t) => {
const error = Error('hello world');
testFilter(t, {
filter: () => { throw error; },
error: error,
// When an exception is thrown in the filter function, no changes should be applied
data1: 0,
data2: 0
});
});

test('database.createSession() - throw sometimes in filter callback', (t) => {
testFilter(t, {
filter: (tableName) => { if (tableName === 'data2') throw new Error(); else { return true; } },
// Only changes to data1 should be applied
// note that the changeset was not aborted
data1: 3,
data2: 0,
expectError: true
});
});

const session = database1.createSession();

database1.exec('INSERT INTO data1 (key) VALUES (1), (2), (3)');
database1.exec('INSERT INTO data2 (key) VALUES (1), (2), (3), (4), (5)');
test('database.createSession() - do not return anything in filter callback', (t) => {
testFilter(t, {
filter: () => {},
// Undefined is falsy, so it is interpreted as "do not include changes from this table"
data1: 0,
data2: 0
});
});

database2.applyChangeset(session.changeset(), {
filter: (tableName) => tableName === 'data2'
test('database.createSession() - return true for all tables', (t) => {
const tables = new Set();
testFilter(t, {
filter: (tableName) => { tables.add(tableName); return true; },
// Changes from all tables should be included
data1: 3,
data2: 5
});
t.assert.deepEqual(tables, new Set(['data1', 'data2']));
});

const data1Rows = database2.prepare('SELECT * FROM data1').all();
const data2Rows = database2.prepare('SELECT * FROM data2').all();
test('database.createSession() - return truthy value for all tables', (t) => {
testFilter(t, {
filter: () => 'yes',
// Truthy, so changes from all tables should be included
data1: 3,
data2: 5
});
});

// Expect no rows since all changes were filtered out
t.assert.strictEqual(data1Rows.length, 0);
// Expect 5 rows since these changes were not filtered out
t.assert.strictEqual(data2Rows.length, 5);
test('database.createSession() - no filter callback', (t) => {
testFilter(t, {
filter: undefined,
// All changes should be applied
data1: 3,
data2: 5
});
});
});

test('database.createSession() - specify other database', (t) => {
Expand Down
Loading