Skip to content

Commit 34cd2cb

Browse files
committed
Docs: MIgrate user version when encrypting existing databases
1 parent 73b8e76 commit 34cd2cb

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

docs/lib/snippets/modular/many_to_many/json.drift.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ class $ShoppingCartsTable extends i3.ShoppingCarts
174174
requiredDuringInsert: false,
175175
defaultConstraints:
176176
i0.GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT'));
177-
static const i0.VerificationMeta _entriesMeta =
178-
const i0.VerificationMeta('entries');
179177
@override
180178
late final i0.GeneratedColumnWithTypeConverter<i3.ShoppingCartEntries, String>
181179
entries = i0.GeneratedColumn<String>('entries', aliasedName, false,
@@ -198,7 +196,6 @@ class $ShoppingCartsTable extends i3.ShoppingCarts
198196
if (data.containsKey('id')) {
199197
context.handle(_idMeta, id.isAcceptableOrUnknown(data['id']!, _idMeta));
200198
}
201-
context.handle(_entriesMeta, const i0.VerificationResult.success());
202199
return context;
203200
}
204201

docs/lib/snippets/platforms/encryption.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,18 @@ void databases() {
7878

7979
if (await existing.exists() && !await encrypted.exists()) {
8080
// We have an existing database to migrate.
81-
sqlite3.open(existingDatabasePath)
81+
final plaintextDb = sqlite3.open(existingDatabasePath)
8282
..execute("ATTACH DATABASE '${escapeString(encryptedDatabasePath)}' "
8383
"AS encrypted KEY '${escapeString(yourKey)}';")
84-
..execute("SELECT sqlcipher_export('encrypted');")
84+
..execute("SELECT sqlcipher_export('encrypted');");
85+
86+
// sqlcipher_export doesn't apply the user_version pragma used by drift
87+
// to implement migrations. The version of the encrypted database must
88+
// match the previous state.
89+
final userVersion =
90+
plaintextDb.select('PRAGMA user_version;').first.columnAt(0) as int;
91+
plaintextDb
92+
..execute('PRAGMA encrypted.user_version = $userVersion;')
8593
..execute('DETACH DATABASE encrypted;')
8694
..dispose();
8795

0 commit comments

Comments
 (0)