Skip to content

Commit 20b2733

Browse files
geeksilva97aduh95
authored andcommitted
sqlite, test: expose sqlite online backup api
PR-URL: #56253 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
1 parent d2e1bcf commit 20b2733

File tree

6 files changed

+626
-0
lines changed

6 files changed

+626
-0
lines changed

doc/api/sqlite.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,63 @@ exception.
551551
| `TEXT` | {string} |
552552
| `BLOB` | {TypedArray} or {DataView} |
553553

554+
## `sqlite.backup(sourceDb, destination[, options])`
555+
556+
<!-- YAML
557+
added: REPLACEME
558+
-->
559+
560+
* `sourceDb` {DatabaseSync} The database to backup. The source database must be open.
561+
* `destination` {string} The path where the backup will be created. If the file already exists, the contents will be
562+
overwritten.
563+
* `options` {Object} Optional configuration for the backup. The
564+
following properties are supported:
565+
* `source` {string} Name of the source database. This can be `'main'` (the default primary database) or any other
566+
database that have been added with [`ATTACH DATABASE`][] **Default:** `'main'`.
567+
* `target` {string} Name of the target database. This can be `'main'` (the default primary database) or any other
568+
database that have been added with [`ATTACH DATABASE`][] **Default:** `'main'`.
569+
* `rate` {number} Number of pages to be transmitted in each batch of the backup. **Default:** `100`.
570+
* `progress` {Function} Callback function that will be called with the number of pages copied and the total number of
571+
pages.
572+
* Returns: {Promise} A promise that resolves when the backup is completed and rejects if an error occurs.
573+
574+
This method makes a database backup. This method abstracts the [`sqlite3_backup_init()`][], [`sqlite3_backup_step()`][]
575+
and [`sqlite3_backup_finish()`][] functions.
576+
577+
The backed-up database can be used normally during the backup process. Mutations coming from the same connection - same
578+
{DatabaseSync} - object will be reflected in the backup right away. However, mutations from other connections will cause
579+
the backup process to restart.
580+
581+
```cjs
582+
const { backup, DatabaseSync } = require('node:sqlite');
583+
584+
(async () => {
585+
const sourceDb = new DatabaseSync('source.db');
586+
const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
587+
rate: 1, // Copy one page at a time.
588+
progress: ({ totalPages, remainingPages }) => {
589+
console.log('Backup in progress', { totalPages, remainingPages });
590+
},
591+
});
592+
593+
console.log('Backup completed', totalPagesTransferred);
594+
})();
595+
```
596+
597+
```mjs
598+
import { backup, DatabaseSync } from 'node:sqlite';
599+
600+
const sourceDb = new DatabaseSync('source.db');
601+
const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
602+
rate: 1, // Copy one page at a time.
603+
progress: ({ totalPages, remainingPages }) => {
604+
console.log('Backup in progress', { totalPages, remainingPages });
605+
},
606+
});
607+
608+
console.log('Backup completed', totalPagesTransferred);
609+
```
610+
554611
## `sqlite.constants`
555612

556613
<!-- YAML
@@ -632,6 +689,9 @@ resolution handler passed to [`database.applyChangeset()`][]. See also
632689
[`SQLITE_DIRECTONLY`]: https://www.sqlite.org/c3ref/c_deterministic.html
633690
[`SQLITE_MAX_FUNCTION_ARG`]: https://www.sqlite.org/limits.html#max_function_arg
634691
[`database.applyChangeset()`]: #databaseapplychangesetchangeset-options
692+
[`sqlite3_backup_finish()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
693+
[`sqlite3_backup_init()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
694+
[`sqlite3_backup_step()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep
635695
[`sqlite3_changes64()`]: https://www.sqlite.org/c3ref/changes.html
636696
[`sqlite3_close_v2()`]: https://www.sqlite.org/c3ref/close.html
637697
[`sqlite3_create_function_v2()`]: https://www.sqlite.org/c3ref/create_function.html

src/env_properties.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
V(asn1curve_string, "asn1Curve") \
7878
V(async_ids_stack_string, "async_ids_stack") \
7979
V(attributes_string, "attributes") \
80+
V(backup_string, "backup") \
8081
V(base_string, "base") \
8182
V(bits_string, "bits") \
8283
V(block_list_string, "blockList") \
@@ -300,6 +301,7 @@
300301
V(primordials_string, "primordials") \
301302
V(priority_string, "priority") \
302303
V(process_string, "process") \
304+
V(progress_string, "progress") \
303305
V(promise_string, "promise") \
304306
V(protocol_string, "protocol") \
305307
V(prototype_string, "prototype") \
@@ -314,6 +316,7 @@
314316
V(reason_string, "reason") \
315317
V(refresh_string, "refresh") \
316318
V(regexp_string, "regexp") \
319+
V(remaining_pages_string, "remainingPages") \
317320
V(rename_string, "rename") \
318321
V(replacement_string, "replacement") \
319322
V(required_module_facade_url_string, \
@@ -367,6 +370,7 @@
367370
V(time_to_first_byte_sent_string, "timeToFirstByteSent") \
368371
V(time_to_first_header_string, "timeToFirstHeader") \
369372
V(tls_ticket_string, "tlsTicket") \
373+
V(total_pages_string, "totalPages") \
370374
V(transfer_string, "transfer") \
371375
V(transfer_unsupported_type_str, \
372376
"Cannot transfer object of unsupported type.") \

0 commit comments

Comments
 (0)