From 430ba5b175396b9aaf8ae450a3a6747c38b01fb6 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 25 Sep 2020 12:34:46 +0200 Subject: [PATCH] More tests for new alterTable migration, reword docs --- docs/content/en/docs/platforms.md | 26 ++++++++++++------- .../src/runtime/query_builder/migration.dart | 7 +++-- .../migrations_integration_test.dart | 9 +++++++ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/docs/content/en/docs/platforms.md b/docs/content/en/docs/platforms.md index 1a452a3d..9a8b3bc9 100644 --- a/docs/content/en/docs/platforms.md +++ b/docs/content/en/docs/platforms.md @@ -3,9 +3,12 @@ title: "Supported platforms" description: All platforms supported by moor, and how to use them --- -Being built ontop of the sqlite3 database, moor can run on almost all Dart platforms. -Since the initial release of moor, the Dart and Flutter ecosystems have changed a lot -(`dart:ffi` wasn't a even thing when moor first came out). +Being built ontop of the sqlite3 database, moor can run on almost every Dart platform. +Since the initial release of moor, the Dart and Flutter ecosystems have changed a lot. +For instance, `dart:ffi` wasn't a thing when moor first came out, and now it's the basis +for moor's most popular implemention. +To clear confusion about different moor packages and when to use them, this document +lists all supported platforms and how to use moor when building apps for them. To achive platform independence, moor separates its core apis from a platform-specific database implementation. The core apis are pure-Dart and run on all Dart platforms, even @@ -59,7 +62,7 @@ details. On most distributions, `libsqlite3.so` is installed already. If you only need to use moor for development, you can just install the sqlite3 libraries. On Ubuntu and other Debian-based -distros, you can install `libsqlite3-dev` package for this. Virtually every other distribution +distros, you can install the `libsqlite3-dev` package for this. Virtually every other distribution will also have a prebuilt package for sqlite. You can also ship a custom `libsqlite3.so` along with your app. See the section below for @@ -68,13 +71,17 @@ details. ### macOS This one is easy! Just use the `VmDatabase` from `package:moor/ffi.dart`. No further setup is -necessary. If you need the latest sqlite3 version, further setup is necessary. In that case, -keep on reading. +necessary. + +If you need a custom sqlite3 library, or want to make sure that your app will always use a +specific sqlite3 version, you can also ship that version with your app. ### Bundling sqlite with your app If you don't want to use the `sqlite3` version from the operating system (or if it's not available), you can also ship `sqlite3` with your app. +The best way to do that depends on how you ship your app. Here, we assume that you can +install the dynamic library for `sqlite` next to your application executable. This example shows how to do that on Linux, by using a custom `sqlite3.so` that we assume lives next to your application: @@ -88,8 +95,7 @@ import 'package:sqlite3/open.dart'; void main() { open.overrideFor(OperatingSystem.linux, _openOnLinux); - final db = sqlite3.openInMemory(); - db.dispose(); + // After setting all the overrides, you can use moor! } DynamicLibrary _openOnLinux() { @@ -98,4 +104,6 @@ DynamicLibrary _openOnLinux() { return DynamicLibrary.open(libraryNextToScript.path); } // _openOnWindows could be implemented similarly by opening `sqlite3.dll` -``` \ No newline at end of file +``` + +Be sure to use moor _after_ you set the platform-specific overrides. \ No newline at end of file diff --git a/moor/lib/src/runtime/query_builder/migration.dart b/moor/lib/src/runtime/query_builder/migration.dart index 7d99961e..f50a5756 100644 --- a/moor/lib/src/runtime/query_builder/migration.dart +++ b/moor/lib/src/runtime/query_builder/migration.dart @@ -96,7 +96,7 @@ class Migrator { return _issueCustomQuery(context.sql, context.boundVariables); } - /// Utility method to alter columns of an existing table. + /// Experimental utility method to alter columns of an existing table. /// /// Since sqlite does not provide a way to alter the type or constraint of an /// individual column, one needs to write a fairly complex migration procedure @@ -111,7 +111,10 @@ class Migrator { /// common migrations that can be run with [alterTable]. /// /// When deleting columns from a table, make sure to migrate tables that have - /// a foreign key constraint on that column first. + /// a foreign key constraint on those columns first. + /// + /// While this function will re-create affected indexes and triggers, it does + /// not reliably handle views at the moment. /// /// [other alter]: https://www.sqlite.org/lang_altertable.html#otheralter /// [moor docs]: https://moor.simonbinder.eu/docs/advanced-features/migrations/#complex-migrations diff --git a/moor/test/integration_tests/migrations_integration_test.dart b/moor/test/integration_tests/migrations_integration_test.dart index f9cef223..270add5b 100644 --- a/moor/test/integration_tests/migrations_integration_test.dart +++ b/moor/test/integration_tests/migrations_integration_test.dart @@ -19,8 +19,12 @@ void main() { ); '''); + db.execute('CREATE INDEX my_index ON todos (content);'); + db.execute('INSERT INTO todos (title, content, target_date, category) ' "VALUES ('title', 'content', 0, '12')"); + + db.execute('PRAGMA foreign_keys = ON'); }); final db = TodoDb(executor); @@ -46,6 +50,11 @@ void main() { final item = await db.select(db.todosTable).getSingle(); expect(item.category, 12); + + // We enabled foreign keys, so they should still be enabled now. + final foreignKeysResult = + await db.customSelect('PRAGMA foreign_keys').getSingle(); + expect(foreignKeysResult.readBool('foreign_keys'), isTrue); }); test('rename columns', () async {