Skip to content

Commit 2aa4241

Browse files
committed
Merge branch 'sqlite3-v3' into develop
2 parents bd67732 + 90dc220 commit 2aa4241

210 files changed

Lines changed: 1320 additions & 1753 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/actions/prepare/action.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,6 @@ runs:
1818
cache-key: "flutter-:os:-:channel:-:version:-:arch:-:hash:"
1919
cache-path: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:"
2020

21-
# Setup sqlite3
22-
- name: Download sqlite3
23-
uses: actions/download-artifact@v4
24-
with:
25-
name: sqlite3
26-
path: drift/.dart_tool/sqlite3/
27-
- name: Use downloaded sqlite3
28-
shell: bash
29-
run: |
30-
chmod a+x drift/.dart_tool/sqlite3/latest/sqlite3
31-
echo $(realpath drift/.dart_tool/sqlite3/latest) >> $GITHUB_PATH
32-
echo "LD_LIBRARY_PATH=$(realpath drift/.dart_tool/sqlite3/latest)" >> $GITHUB_ENV
33-
- name: Check sqlite3 version
34-
run: sqlite3 --version
35-
shell: bash
36-
3721
# Setup Dart packages
3822
- uses: actions/cache@v4
3923
with:

.github/workflows/main.yml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -306,21 +306,6 @@ jobs:
306306
- name: Install dependencies in drift/example/app
307307
run: melos bootstrap --scope app
308308

309-
- name: Download sqlite3
310-
uses: actions/download-artifact@v4
311-
with:
312-
name: sqlite3
313-
path: drift/.dart_tool/sqlite3/
314-
- name: Use downloaded sqlite3
315-
shell: bash
316-
run: |
317-
chmod a+x drift/.dart_tool/sqlite3/latest/sqlite3
318-
echo $(realpath drift/.dart_tool/sqlite3/latest) >> $GITHUB_PATH
319-
echo "LD_LIBRARY_PATH=$(realpath drift/.dart_tool/sqlite3/latest)" >> $GITHUB_ENV
320-
- name: Check sqlite3 version
321-
run: sqlite3 --version
322-
shell: bash
323-
324309
- name: Generate code
325310
run: dart run build_runner build --delete-conflicting-outputs
326311
working-directory: examples/app

.github/workflows/setup.yml

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,6 @@ on:
66
value: ${{ jobs.dart_dependencies.outputs.flutter_version }}
77

88
jobs:
9-
# Compile the latest sqlite3 library, which will be used to run tests in drift
10-
# and sqlparser
11-
compile_sqlite3:
12-
strategy:
13-
matrix:
14-
# We only really need this for Ubuntu, but we recommend users run the same
15-
# steps so we better make sure they work on all platforms.
16-
os: [ubuntu-latest, macos-latest, windows-latest]
17-
name: "Compile sqlite3 for tests"
18-
runs-on: ${{ matrix.os }}
19-
steps:
20-
- uses: actions/checkout@v4
21-
- uses: actions/cache@v4
22-
id: cache_build
23-
with:
24-
path: drift/.dart_tool/sqlite3/
25-
key: ${{ runner.os }}-${{ hashFiles('drift/tool/') }}
26-
- name: Download Dart
27-
uses: subosito/flutter-action@v2
28-
if: steps.cache_build.outputs.cache-hit != 'true'
29-
with:
30-
channel: stable
31-
flutter-version: ${{ inputs.flutter_version }}
32-
cache: true
33-
cache-key: "flutter-:os:-:channel:-:version:-:arch:-:hash:"
34-
cache-path: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:"
35-
- name: Compile sqlite3
36-
if: steps.cache_build.outputs.cache-hit != 'true'
37-
run: |
38-
dart pub global activate melos
39-
dart pub get
40-
melos bootstrap --scope drift
41-
dart run drift/tool/download_sqlite3.dart
42-
- name: Upload built sqlite3 binaries
43-
uses: actions/upload-artifact@v4
44-
# we only need these artifacts on Linux since we run unit tests on Linux only
45-
if: runner.os == 'Linux'
46-
with:
47-
name: sqlite3
48-
path: drift/.dart_tool/sqlite3/
49-
if-no-files-found: error
50-
retention-days: 1
51-
529
dart_dependencies:
5310
name: Get and cache Dart dependencies
5411
runs-on: ubuntu-latest

docs/content/examples/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Drift's repository also contains a number of smaller examples showcasing select
1515
drift features:
1616

1717
- The [encryption] example contains a simple Flutter app using an encrypted drift
18-
database, powered by the `sqlcipher_flutter_libs` package.
18+
database.
1919
<!-- - [web_worker] and [flutter_web_worker] are small web-only apps using drift in
2020
a shared web worker, which allows for a real-time synchronization of the
2121
database across tabs. Of course, this pattern can only be embedded into

docs/content/faq.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ It would be cleaner to use `InheritedWidgets` for that, and the `provider` packa
2222
### Provider
2323
If you're using the [provider](https://pub.dev/packages/provider) package, you can wrap your top-level widget in a
2424
provider that manages the database instance:
25+
2526
```dart
2627
void main() {
2728
runApp(
@@ -33,7 +34,9 @@ void main() {
3334
);
3435
}
3536
```
37+
3638
Your widgets would then have access to the database using `Provider.of<MyDatabase>(context)`.
39+
3740
### GetX
3841
If you're using the [GetX](https://pub.dev/packages/get) package, you can add it as a service that manages the database instance:.
3942

docs/content/platforms/encryption.md

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,81 +5,56 @@ description: Use drift on encrypted databases
55

66
---
77

8-
9-
108
There are two ways to use drift on encrypted databases.
119
The `encrypted_drift` package is similar to `drift_sqflite` and uses a platform plugin written in
1210
Java.
13-
Alternatively, you can use the ffi-based implementation with the `sqlcipher_flutter_libs` package.
11+
Alternatively, you can use the ffi-based implementation with regular `package:drift/native.dart` APIs.
1412

15-
For new apps, we recommend using `sqlcipher_flutter_libs` with a `NativeDatabase`
16-
from drift.
13+
For new apps, we recommend using a `NativeDatabase` with encrypted databases.
1714
An example of a Flutter app using the new encryption package is available
1815
[here](https://github.com/simolus3/drift/tree/develop/examples/encryption).
1916

2017
## Encrypted version of a `NativeDatabase`
2118

22-
You can also use the new `drift/native` library with an encrypted executor.
23-
This allows you to use an encrypted drift database on more platforms, which is particularly
24-
interesting for Desktop applications.
19+
You can use the `drift/native` library with an encrypted executor.
20+
This allows you to use an encrypted drift database on all native platforms, including Desktop.
2521

2622
### Setup
2723

28-
To use `sqlcipher`, add a dependency on `sqlcipher_flutter_libs`:
24+
The `sqlite3` package uses build hooks to bundle a version of SQLite with your app.
25+
It can be configured to bundle [SQLite3MultipleCiphers](https://utelle.github.io/SQLite3MultipleCiphers/)
26+
instead, which provides encryption support.
27+
To enable encryption, add this to the pubspec of your pub workspace root (or your app's pubspec if you're
28+
not using pub workspaces):
2929

3030
```yaml
31-
dependencies:
32-
sqlcipher_flutter_libs: ^0.6.0
31+
hooks:
32+
user_defines:
33+
sqlite3:
34+
source: sqlite3mc
3335
```
3436
35-
If you already have a dependency on `sqlite3_flutter_libs`, __drop that dependency__.
36-
`sqlite3_flutter_libs` and `sqlcipher_flutter_libs` are not compatible
37-
as they both provide a (different) set of `sqlite3` native apis.
38-
39-
On Android, you also need to adapt the opening behavior of the `sqlite3` package to use the encrypted library instead
40-
of the regular `libsqlite3.so`:
37+
For more information on configuring hooks, see [the documentation](https://pub.dev/documentation/sqlite3/latest/topics/hook-topic.html).
4138
42-
<Snippet href="/lib/src/snippets/platforms/encryption.dart" name="setup" />
43-
44-
When using drift on a background database, you need to call `setupSqlCipher` on the background isolate
45-
as well. With `NativeDatabase.createInBackground`, which are using isolates internally, you can use
46-
the `setupIsolate` callback to do this - the examples on this page use this as well.
47-
Since `applyWorkaroundToOpenSqlCipherOnOldAndroidVersions()` invokes a platform channel, one needs
48-
to install a `BackgroundIsolateBinaryMessenger` on the isolate as well.
49-
50-
On iOS, macOS and Windows, no additional setup is necessary - simply depend on `sqlcipher_flutter_libs`.
51-
For Linux builds, note that OpenSSL is linked statically by default. If you want to compile your app to use
52-
a dynamically-linked distribution of OpenSSL, see [this](https://github.com/simolus3/sqlite3.dart/issues/186#issuecomment-1742110933)
53-
issue comment.
39+
Previous versions of the `sqlite3` package required a dependency on `sqlcipher_flutter_libs`. That package is no longer necessary
40+
after upgrading to drift 2.32.0 and can be removed.
5441

5542
### Using
5643

57-
SQLCipher implements sqlite3's C api, which means that you can continue to use the `sqlite3` package
58-
or `drift/ffi` without changes. They're both fully compatible with `sqlcipher_flutter_libs`.
44+
SQLite3MultipleCiphers implements the SQLite C API, which means that you can continue to use the `sqlite3` package
45+
or `package:drift/native.dart` without code changes.
5946

60-
To actually encrypt a database, you must set an encryption key before using it.
47+
To actually encrypt a database though, you must set an encryption key before using it.
6148
A good place to do that in drift is the `setup` parameter of `NativeDatabase`, which runs before drift
6249
is using the database in any way:
6350

6451
<Snippet href="/lib/src/snippets/platforms/encryption.dart" name="encrypted1" />
6552

66-
??? note "Disabling double-quoted string literals"
67-
68-
In `sqlite3_flutter_libs`, sqlite3 is compiled to only accept single-quoted string literals.
69-
This is a recommended option to avoid confusion - `SELECT "column" FROM tbl` is always a
70-
column reference, `SELECT 'column'` is always a string literal.
71-
72-
SQLCipher does not disable double-quoted string literals at compile-time. For consistency,
73-
it is recommended to manually disable them for databases used with drift.
74-
7553
### Important notice
7654

77-
On the native side, `SQLCipher` and `sqlite3` stand in conflict with each other.
78-
If your package depends on both native libraries, the one you will actually get may be undefined on some platforms.
79-
In particular, if you depend on `sqlcipher_flutter_libs` and another package you use depends on say `sqflite`,
80-
you could still be getting the regular `sqlite3` library without support for encryption!
81-
82-
For this reason, it is recommended that you check that the `cipher_version` pragma is available at runtime:
55+
SQLite3MultipleCiphers is a fork of SQLite with added encryption support.
56+
To ensure you're using the right version in your app, it is recommended that you check for the `cipher`
57+
pragma at runtime (since that is not provided by upstream SQLite):
8358

8459
<Snippet href="/lib/src/snippets/platforms/encryption.dart" name="check_cipher" />
8560

@@ -88,25 +63,34 @@ Next, add an `assert(_debugCheckHasCipher(database))` before using the database.
8863

8964
<Snippet href="/lib/src/snippets/platforms/encryption.dart" name="encrypted2" />
9065

91-
If this check reveals that the encrypted variant is not available, please see [the documentation here](https://github.com/simolus3/sqlite3.dart/tree/master/sqlcipher_flutter_libs#incompatibilities-with-sqlite3-on-ios-and-macos) for advice.
66+
If this check reveals that the encrypted variant is not available, check your hook options
67+
or open an issue on [this repository](https://github.com/simolus3/sqlite3.dart/) for further advice.
9268

9369
### Encrypting existing databases
9470

9571
If you have an existing database which you now want to encrypt, there are a few steps to consider.
96-
First, replace your dependencies on `drift_flutter` or `sqlite3_flutter_libs` with
97-
`sqlcipher_flutter_libs` as shown in the [setup](#setup).
72+
First, add hook options to link SQLite3MultipleCiphers as shown in [setup](#setup).
9873

9974
Note however that you can't just apply the `pragma key = ` statement on existing databases!
100-
To migrate existing databases to encryption, SQLCipher recommends [these steps](https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/868):
75+
To migrate existing databases to encryption, this creates a temporary copy that will be encrypted with [`pragma rekey`](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-rekey--hexrekey):
10176

102-
1. Opening your existing database.
103-
2. Attaching a new encrypted-variant.
104-
3. Calling the `sqcipher_export` function to copy the unencrypted database into the encrypted file.
105-
4. Closing and deleting the unencrypted database.
77+
<Snippet href="/lib/src/snippets/platforms/encryption.dart" name="migration" />
10678

107-
In drift, you can run these steps in the `isolateSetup` callback when opening a `NativeDatabase`:
79+
### Migrating from SQLCipher to SQLite3MultipleCiphers
10880

109-
<Snippet href="/lib/src/snippets/platforms/encryption.dart" name="migration" />
81+
Previous versions of this page suggested using `sqlcipher_flutter_libs` and custom code to enable
82+
SQLCipher. Starting from drift version 2.32.0 and version 3.x of the `sqlite3` package, using
83+
SQLite3MultipleCiphers is much easier.
84+
85+
SQLCipher is no longer supported with a straightforward setup. It is possible to use existing encrypted
86+
databases with SQLite3MultipleCiphers. To do that, run the following pragmas before `pragma key`
87+
after upgrading:
88+
89+
1. `PRAGMA cipher = 'sqlcipher'`
90+
2. `PRAGMA legacy = 4`.
91+
92+
Please carefully test that your app supports existing databases after upgrading to SQLite3MultipleCiphers.
93+
For more details, see [this issue](https://github.com/simolus3/sqlite3.dart/issues/302#issuecomment-3765247078).
11094

11195
## Using `encrypted_drift`
11296

0 commit comments

Comments
 (0)