Skip to content

Commit

Permalink
sqlite: pass object to progress function instead of two args
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Jan 14, 2025
1 parent 5ad3f48 commit a050cd6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
V(address_string, "address") \
V(source_db_string, "sourceDb") \
V(target_db_string, "targetDb") \
V(total_pages_string, "totalPages") \
V(remaining_pages_string, "remainingPages") \
V(progress_string, "progress") \
V(aliases_string, "aliases") \
V(alpn_callback_string, "ALPNCallback") \
Expand Down
28 changes: 20 additions & 8 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,25 @@ class BackupJob : public ThreadPoolWork {
Local<Function>::New(env()->isolate(), progressFunc_);

if (!fn.IsEmpty()) {
Local<Value> argv[] = {
Integer::New(env()->isolate(), total_pages),
Integer::New(env()->isolate(), remaining_pages),
};
Local<Object> progress_info = Object::New(env()->isolate());

if (progress_info
->Set(env()->context(),
env()->total_pages_string(),
Integer::New(env()->isolate(), total_pages))
.IsNothing() ||
progress_info
->Set(env()->context(),
env()->remaining_pages_string(),
Integer::New(env()->isolate(), remaining_pages))
.IsNothing()) {
return;
}

Local<Value> argv[] = {progress_info};

TryCatch try_catch(env()->isolate());
fn->Call(env()->context(), Null(env()->isolate()), 2, argv)
fn->Call(env()->context(), Null(env()->isolate()), 1, argv)
.FromMaybe(Local<Value>());

if (try_catch.HasCaught()) {
Expand Down Expand Up @@ -742,7 +754,7 @@ void DatabaseSync::Exec(const FunctionCallbackInfo<Value>& args) {
CHECK_ERROR_OR_THROW(env->isolate(), db->connection_, r, SQLITE_OK, void());
}

// database.backup(destination, { sourceDb, targetDb, rate, progress: (total,
// database.backup(path, { sourceDb, targetDb, rate, progress: (total,
// remaining) => {} )
void DatabaseSync::Backup(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand All @@ -762,7 +774,7 @@ void DatabaseSync::Backup(const FunctionCallbackInfo<Value>& args) {

THROW_AND_RETURN_ON_BAD_STATE(env, !db->IsOpen(), "database is not open");

Utf8Value destFilename(env->isolate(), args[0].As<String>());
Utf8Value destPath(env->isolate(), args[0].As<String>());
Local<Function> progressFunc = Local<Function>();

if (args.Length() > 1) {
Expand Down Expand Up @@ -852,7 +864,7 @@ void DatabaseSync::Backup(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(resolver->GetPromise());

BackupJob* job = new BackupJob(
env, db, resolver, source_db, *destFilename, dest_db, rate, progressFunc);
env, db, resolver, source_db, *destPath, dest_db, rate, progressFunc);
db->backups_.insert(job);
job->ScheduleBackup();
}
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-sqlite-backup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ test('database backup', async (t) => {
// so the progress function should be called once (the last call is not made since
// the promise resolves)
t.assert.strictEqual(progressFn.mock.calls.length, 1);
t.assert.deepStrictEqual(progressFn.mock.calls[0].arguments, [{ totalPages: 2, remainingPages: 1 }]);
t.assert.deepStrictEqual(rows, [
{ __proto__: null, key: 1, value: 'value-1' },
{ __proto__: null, key: 2, value: 'value-2' },
]);
});

test('database backup in a single call', async (t) => {
const progressFn = t.mock.fn();
const database = makeSourceDb();
const destDb = nextDb();

// Let rate to be default (100) to backup in a single call
await database.backup(destDb, {
progress: progressFn,
});

const backup = new DatabaseSync(destDb);
const rows = backup.prepare('SELECT * FROM data').all();

t.assert.strictEqual(progressFn.mock.calls.length, 0);
t.assert.deepStrictEqual(rows, [
{ __proto__: null, key: 1, value: 'value-1' },
{ __proto__: null, key: 2, value: 'value-2' },
Expand Down

0 comments on commit a050cd6

Please sign in to comment.