mirror of https://github.com/AMT-Cheif/drift.git
Update documentation on migrations
This commit is contained in:
parent
a304d13927
commit
ceed461a06
|
@ -51,17 +51,17 @@ to run the statements.
|
|||
Starting from moor 1.5, you can use the `beforeOpen` parameter in the `MigrationStrategy` which will be called after
|
||||
migrations, but after any other queries are run. You could use it to populate data after the database has been created:
|
||||
```dart
|
||||
beforeOpen: (db, details) async {
|
||||
beforeOpen: (details) async {
|
||||
if (details.wasCreated) {
|
||||
final workId = await db.into(categories).insert(Category(description: 'Work'));
|
||||
final workId = await into(categories).insert(Category(description: 'Work'));
|
||||
|
||||
await db.into(todos).insert(TodoEntry(
|
||||
await into(todos).insert(TodoEntry(
|
||||
content: 'A first todo entry',
|
||||
category: null,
|
||||
targetDate: DateTime.now(),
|
||||
));
|
||||
|
||||
await db.into(todos).insert(
|
||||
await into(todos).insert(
|
||||
TodoEntry(
|
||||
content: 'Rework persistence code',
|
||||
category: workId,
|
||||
|
@ -72,12 +72,20 @@ beforeOpen: (db, details) async {
|
|||
```
|
||||
You could also activate pragma statements that you need:
|
||||
```dart
|
||||
beforeOpen: (db, details) async {
|
||||
beforeOpen: (details) async {
|
||||
if (details.wasCreated) {
|
||||
// ...
|
||||
}
|
||||
await db.customStatement('PRAGMA foreign_keys = ON');
|
||||
await customStatement('PRAGMA foreign_keys = ON');
|
||||
}
|
||||
```
|
||||
It is important that you run these queries on `db` explicitly. Failing to do so causes a deadlock which prevents the
|
||||
database from being opened.
|
||||
|
||||
## During development
|
||||
|
||||
During development, you might be changing your schema very often and don't want to write migrations for that
|
||||
yet. You can just delete your apps' data and reinstall the app - the database will be deleted and all tables
|
||||
will be created again. Please note that uninstalling is not enough sometimes - Android might have backed up
|
||||
the database file and will re-create it when installing the app again.
|
||||
|
||||
You can also delete and re-create all tables everytime your app is opened, see [this comment](https://github.com/simolus3/moor/issues/188#issuecomment-542682912)
|
||||
on how that can be achieved.
|
|
@ -41,6 +41,13 @@ If you're strict on keeping your business logic out of the widget layer, you pro
|
|||
framework like `kiwi` or `get_it` to instantiate services and view models. Creating a singleton instance of `MyDatabase`
|
||||
in your favorite dependency injection framework for flutter hence solves this problem for you.
|
||||
|
||||
## Why am I getting no such table errors?
|
||||
|
||||
If you add another table after your app has already been installed, you need to write a [migration]({{< relref "Advanced Features/migrations.md" >}})
|
||||
that covers creating that table. If you're in the process of developing your app and want to use un- and reinstall your app
|
||||
instead of writing migrations, that's fine too. Please note that your apps data might be backed up on Android, so
|
||||
manually deleting your app's data instead of a reinstall is necessary on some devices.
|
||||
|
||||
## How does moor compare to X?
|
||||
There are a variety of good persistence libraries for Dart and Flutter.
|
||||
|
||||
|
|
Loading…
Reference in New Issue