Docs: Describe how to export databases (#376)

This commit is contained in:
Simon Binder 2022-05-28 13:52:18 +02:00
parent 645f954a9b
commit cbcde53da6
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 42 additions and 7 deletions

View File

@ -22,9 +22,9 @@ class ShoppingCarts extends Table {
@DataClassName('ShoppingCartEntry') @DataClassName('ShoppingCartEntry')
class ShoppingCartEntries extends Table { class ShoppingCartEntries extends Table {
// id of the cart that should contain this item. // id of the cart that should contain this item.
IntColumn get shoppingCart => integer()(); IntColumn get shoppingCart => integer().references(ShoppingCarts, #id)();
// id of the item in this cart // id of the item in this cart
IntColumn get item => integer()(); IntColumn get item => integer().references(BuyableItems, #id)();
// again, we could store additional information like when the item was // again, we could store additional information like when the item was
// added, an amount, etc. // added, an amount, etc.
} }

View File

@ -1,13 +1,23 @@
--- ---
data: data:
title: "Existing databases" title: "Importing and exporting databases"
description: Using drift with an existing database description: Using drift with an existing database
template: layouts/docs/single template: layouts/docs/single
--- ---
You can use drift with a pre-propulated database that you ship with your app. You can use drift with a pre-propulated database that you ship with your app.
This page also describes how to export the underlying sqlite3 database used
by drift into a file.
## Including the database ## Using an existing database
You can use a `LazyDatabase` wrapper to run an asynchronous computation before drift
opens a database.
This is a good place to check if the target database file exists, and, if it doesnt,
create one.
This example shows how to do that from assets.
### Including the database
First, create the sqlite3 database you want to ship with your app. First, create the sqlite3 database you want to ship with your app.
You can create a database with the [sqlite3 CLI tool](https://sqlite.org/cli.html) You can create a database with the [sqlite3 CLI tool](https://sqlite.org/cli.html)
@ -24,7 +34,7 @@ flutter:
- assets/my_database.db - assets/my_database.db
``` ```
## Extracting the database ### Extracting the database
To initialize the database before using drift, you need to extract the asset from your To initialize the database before using drift, you need to extract the asset from your
app onto the device. app onto the device.
@ -63,3 +73,25 @@ class MyDatabase extends _$MyDatabase {
// ... // ...
``` ```
## Exporting a databasee
To export a sqlite3 database into a file, you can use the `VACUUM INTO` statement.
Inside your database class, this could look like the following:
```dart
Future<void> exportInto(File file) async {
// Make sure the directory of the target file exists
await file.parent.create(recursive: true);
// Override an existing backup, sqlite expects the target file to be empty
if (file.existsSync()) {
file.deleteSync();
}
await customStatement('VACUUM INTO ?', [file.path]);
}
```
You can now export this file containing the database of your app with another
package like `flutter_share` or other backup approaches.

View File

@ -15,7 +15,10 @@ queries in drift. First, we need to store some items that can be bought:
{% include "blocks/snippet" snippets=snippets name="buyable_items" %} {% include "blocks/snippet" snippets=snippets name="buyable_items" %}
We're going to define two tables for shopping carts: One for the cart We're going to define two tables for shopping carts: One for the cart
itself, and another one to store the entries in the cart: itself, and another one to store the entries in the cart.
The latter uses [references]({{ '../Getting started/advanced_dart_tables.md#references' | pageUrl }})
to express the foreign key constraints of referencing existing shopping
carts or product items.
{% include "blocks/snippet" snippets=snippets name="cart_tables" %} {% include "blocks/snippet" snippets=snippets name="cart_tables" %}