Skip to content

Commit

Permalink
sqlite, test: expose sqlite online backup api
Browse files Browse the repository at this point in the history
PR-URL: #56253
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
  • Loading branch information
geeksilva97 authored and targos committed Feb 7, 2025
1 parent 3c082d4 commit 8dc6376
Show file tree
Hide file tree
Showing 6 changed files with 626 additions and 0 deletions.
60 changes: 60 additions & 0 deletions doc/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,63 @@ exception.
| `TEXT` | {string} |
| `BLOB` | {TypedArray} or {DataView} |

## `sqlite.backup(sourceDb, destination[, options])`

<!-- YAML
added: REPLACEME
-->

* `sourceDb` {DatabaseSync} The database to backup. The source database must be open.
* `destination` {string} The path where the backup will be created. If the file already exists, the contents will be
overwritten.
* `options` {Object} Optional configuration for the backup. The
following properties are supported:
* `source` {string} Name of the source database. This can be `'main'` (the default primary database) or any other
database that have been added with [`ATTACH DATABASE`][] **Default:** `'main'`.
* `target` {string} Name of the target database. This can be `'main'` (the default primary database) or any other
database that have been added with [`ATTACH DATABASE`][] **Default:** `'main'`.
* `rate` {number} Number of pages to be transmitted in each batch of the backup. **Default:** `100`.
* `progress` {Function} Callback function that will be called with the number of pages copied and the total number of
pages.
* Returns: {Promise} A promise that resolves when the backup is completed and rejects if an error occurs.

This method makes a database backup. This method abstracts the [`sqlite3_backup_init()`][], [`sqlite3_backup_step()`][]
and [`sqlite3_backup_finish()`][] functions.

The backed-up database can be used normally during the backup process. Mutations coming from the same connection - same
{DatabaseSync} - object will be reflected in the backup right away. However, mutations from other connections will cause
the backup process to restart.

```cjs
const { backup, DatabaseSync } = require('node:sqlite');

(async () => {
const sourceDb = new DatabaseSync('source.db');
const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
rate: 1, // Copy one page at a time.
progress: ({ totalPages, remainingPages }) => {
console.log('Backup in progress', { totalPages, remainingPages });
},
});

console.log('Backup completed', totalPagesTransferred);
})();
```

```mjs
import { backup, DatabaseSync } from 'node:sqlite';

const sourceDb = new DatabaseSync('source.db');
const totalPagesTransferred = await backup(sourceDb, 'backup.db', {
rate: 1, // Copy one page at a time.
progress: ({ totalPages, remainingPages }) => {
console.log('Backup in progress', { totalPages, remainingPages });
},
});

console.log('Backup completed', totalPagesTransferred);
```

## `sqlite.constants`

<!-- YAML
Expand Down Expand Up @@ -589,6 +646,9 @@ resolution handler passed to [`database.applyChangeset()`][]. See also
[`SQLITE_DIRECTONLY`]: https://www.sqlite.org/c3ref/c_deterministic.html
[`SQLITE_MAX_FUNCTION_ARG`]: https://www.sqlite.org/limits.html#max_function_arg
[`database.applyChangeset()`]: #databaseapplychangesetchangeset-options
[`sqlite3_backup_finish()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
[`sqlite3_backup_init()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
[`sqlite3_backup_step()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep
[`sqlite3_changes64()`]: https://www.sqlite.org/c3ref/changes.html
[`sqlite3_close_v2()`]: https://www.sqlite.org/c3ref/close.html
[`sqlite3_create_function_v2()`]: https://www.sqlite.org/c3ref/create_function.html
Expand Down
4 changes: 4 additions & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
V(asn1curve_string, "asn1Curve") \
V(async_ids_stack_string, "async_ids_stack") \
V(attributes_string, "attributes") \
V(backup_string, "backup") \
V(base_string, "base") \
V(base_url_string, "baseURL") \
V(bits_string, "bits") \
Expand Down Expand Up @@ -302,6 +303,7 @@
V(primordials_string, "primordials") \
V(priority_string, "priority") \
V(process_string, "process") \
V(progress_string, "progress") \
V(promise_string, "promise") \
V(protocol_string, "protocol") \
V(prototype_string, "prototype") \
Expand All @@ -316,6 +318,7 @@
V(reason_string, "reason") \
V(refresh_string, "refresh") \
V(regexp_string, "regexp") \
V(remaining_pages_string, "remainingPages") \
V(rename_string, "rename") \
V(replacement_string, "replacement") \
V(required_module_facade_url_string, \
Expand Down Expand Up @@ -369,6 +372,7 @@
V(time_to_first_byte_sent_string, "timeToFirstByteSent") \
V(time_to_first_header_string, "timeToFirstHeader") \
V(tls_ticket_string, "tlsTicket") \
V(total_pages_string, "totalPages") \
V(transfer_string, "transfer") \
V(transfer_unsupported_type_str, \
"Cannot transfer object of unsupported type.") \
Expand Down
Loading

0 comments on commit 8dc6376

Please sign in to comment.