-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start using new drift features since 2.5.0 #1248
base: main
Are you sure you want to change the base?
Changes from all commits
41480c4
a74a5d8
23321dd
c1c2301
5a30df0
03a627c
e9243d1
bddcede
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// dart format width=80 | ||
import 'package:drift/internal/versioned_schema.dart' as i0; | ||
import 'package:drift/drift.dart' as i1; | ||
import 'package:drift/drift.dart'; // ignore_for_file: type=lint,unused_import | ||
|
||
// GENERATED BY drift_dev, DO NOT MODIFY. | ||
final class Schema2 extends i0.VersionedSchema { | ||
Schema2({required super.database}) : super(version: 2); | ||
@override | ||
late final List<i1.DatabaseSchemaEntity> entities = [ | ||
accounts, | ||
]; | ||
late final Shape0 accounts = Shape0( | ||
source: i0.VersionedTable( | ||
entityName: 'accounts', | ||
withoutRowId: false, | ||
isStrict: false, | ||
tableConstraints: [ | ||
'UNIQUE(realm_url, user_id)', | ||
'UNIQUE(realm_url, email)', | ||
], | ||
columns: [ | ||
_column_0, | ||
_column_1, | ||
_column_2, | ||
_column_3, | ||
_column_4, | ||
_column_5, | ||
_column_6, | ||
_column_7, | ||
_column_8, | ||
], | ||
attachedDatabase: database, | ||
), | ||
alias: null); | ||
} | ||
|
||
class Shape0 extends i0.VersionedTable { | ||
Shape0({required super.source, required super.alias}) : super.aliased(); | ||
i1.GeneratedColumn<int> get id => | ||
columnsByName['id']! as i1.GeneratedColumn<int>; | ||
i1.GeneratedColumn<String> get realmUrl => | ||
columnsByName['realm_url']! as i1.GeneratedColumn<String>; | ||
i1.GeneratedColumn<int> get userId => | ||
columnsByName['user_id']! as i1.GeneratedColumn<int>; | ||
i1.GeneratedColumn<String> get email => | ||
columnsByName['email']! as i1.GeneratedColumn<String>; | ||
i1.GeneratedColumn<String> get apiKey => | ||
columnsByName['api_key']! as i1.GeneratedColumn<String>; | ||
i1.GeneratedColumn<String> get zulipVersion => | ||
columnsByName['zulip_version']! as i1.GeneratedColumn<String>; | ||
i1.GeneratedColumn<String> get zulipMergeBase => | ||
columnsByName['zulip_merge_base']! as i1.GeneratedColumn<String>; | ||
i1.GeneratedColumn<int> get zulipFeatureLevel => | ||
columnsByName['zulip_feature_level']! as i1.GeneratedColumn<int>; | ||
i1.GeneratedColumn<String> get ackedPushToken => | ||
columnsByName['acked_push_token']! as i1.GeneratedColumn<String>; | ||
} | ||
|
||
i1.GeneratedColumn<int> _column_0(String aliasedName) => | ||
i1.GeneratedColumn<int>('id', aliasedName, false, | ||
hasAutoIncrement: true, | ||
type: i1.DriftSqlType.int, | ||
defaultConstraints: | ||
i1.GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT')); | ||
i1.GeneratedColumn<String> _column_1(String aliasedName) => | ||
i1.GeneratedColumn<String>('realm_url', aliasedName, false, | ||
type: i1.DriftSqlType.string); | ||
i1.GeneratedColumn<int> _column_2(String aliasedName) => | ||
i1.GeneratedColumn<int>('user_id', aliasedName, false, | ||
type: i1.DriftSqlType.int); | ||
i1.GeneratedColumn<String> _column_3(String aliasedName) => | ||
i1.GeneratedColumn<String>('email', aliasedName, false, | ||
type: i1.DriftSqlType.string); | ||
i1.GeneratedColumn<String> _column_4(String aliasedName) => | ||
i1.GeneratedColumn<String>('api_key', aliasedName, false, | ||
type: i1.DriftSqlType.string); | ||
i1.GeneratedColumn<String> _column_5(String aliasedName) => | ||
i1.GeneratedColumn<String>('zulip_version', aliasedName, false, | ||
type: i1.DriftSqlType.string); | ||
i1.GeneratedColumn<String> _column_6(String aliasedName) => | ||
i1.GeneratedColumn<String>('zulip_merge_base', aliasedName, true, | ||
type: i1.DriftSqlType.string); | ||
i1.GeneratedColumn<int> _column_7(String aliasedName) => | ||
i1.GeneratedColumn<int>('zulip_feature_level', aliasedName, false, | ||
type: i1.DriftSqlType.int); | ||
i1.GeneratedColumn<String> _column_8(String aliasedName) => | ||
i1.GeneratedColumn<String>('acked_push_token', aliasedName, true, | ||
type: i1.DriftSqlType.string); | ||
i0.MigrationStepWithVersion migrationSteps({ | ||
required Future<void> Function(i1.Migrator m, Schema2 schema) from1To2, | ||
}) { | ||
return (currentVersion, database) async { | ||
switch (currentVersion) { | ||
case 1: | ||
final schema = Schema2(database: database); | ||
final migrator = i1.Migrator(database, schema); | ||
await from1To2(migrator, schema); | ||
return 2; | ||
default: | ||
throw ArgumentError.value('Unknown migration from $currentVersion'); | ||
} | ||
}; | ||
} | ||
|
||
i1.OnUpgrade stepByStep({ | ||
required Future<void> Function(i1.Migrator m, Schema2 schema) from1To2, | ||
}) => | ||
i0.VersionedSchema.stepByStepHelper( | ||
step: migrationSteps( | ||
from1To2: from1To2, | ||
)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ dependencies: | |
convert: ^3.1.1 | ||
crypto: ^3.0.3 | ||
device_info_plus: ^11.2.0 | ||
drift: ^2.5.0 | ||
drift: ^2.23.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Confirmed by running There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for confirming! |
||
file_picker: ^8.0.0+1 | ||
firebase_core: ^3.3.0 | ||
firebase_messaging: ^15.0.1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,37 +98,62 @@ void main() { | |
verifier = SchemaVerifier(GeneratedHelper()); | ||
}); | ||
|
||
test('upgrade to v2, empty', () async { | ||
final connection = await verifier.startAt(1); | ||
test('downgrading', () async { | ||
final connection = await verifier.startAt(2); | ||
final db = AppDatabase(connection); | ||
await verifier.migrateAndValidate(db, 2); | ||
await verifier.migrateAndValidate(db, 1); | ||
await db.close(); | ||
}); | ||
|
||
test('upgrade to v2, with data', () async { | ||
final schema = await verifier.schemaAt(1); | ||
final before = v1.DatabaseAtV1(schema.newConnection()); | ||
await before.into(before.accounts).insert(v1.AccountsCompanion.insert( | ||
realmUrl: 'https://chat.example/', | ||
userId: 1, | ||
email: '[email protected]', | ||
apiKey: '1234', | ||
zulipVersion: '6.0', | ||
zulipMergeBase: const Value('6.0'), | ||
zulipFeatureLevel: 42, | ||
)); | ||
final accountV1 = await before.select(before.accounts).watchSingle().first; | ||
await before.close(); | ||
}, skip: true); // TODO(#1172): unskip this | ||
|
||
final db = AppDatabase(schema.newConnection()); | ||
await verifier.migrateAndValidate(db, 2); | ||
await db.close(); | ||
group('migrate without data', () { | ||
// These simple tests verify all possible schema updates with a simple (no | ||
// data) migration. This is a quick way to ensure that written database | ||
// migrations properly alter the schema. | ||
const versions = GeneratedHelper.versions; | ||
for (final (i, fromVersion) in versions.indexed) { | ||
group('from $fromVersion', () { | ||
for (final toVersion in versions.skip(i + 1)) { | ||
test('to $toVersion', () async { | ||
final schema = await verifier.schemaAt(fromVersion); | ||
final db = AppDatabase(schema.newConnection()); | ||
await verifier.migrateAndValidate(db, toVersion); | ||
await db.close(); | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
final after = v2.DatabaseAtV2(schema.newConnection()); | ||
final account = await after.select(after.accounts).getSingle(); | ||
check(account.toJson()).deepEquals({ | ||
...accountV1.toJson(), | ||
'ackedPushToken': null, | ||
// Testing this can be useful for migrations that change existing columns | ||
// (e.g. by alterating their type or constraints). Migrations that only add | ||
// tables or columns typically don't need these advanced tests. For more | ||
// information, see https://drift.simonbinder.eu/migrations/tests/#verifying-data-integrity | ||
group('migrate with data', () { | ||
test('upgrade to v2', () async { | ||
late final v1.AccountsData oldAccountData; | ||
await verifier.testWithDataIntegrity( | ||
oldVersion: 1, createOld: v1.DatabaseAtV1.new, | ||
newVersion: 2, createNew: v2.DatabaseAtV2.new, | ||
openTestedDatabase: AppDatabase.new, | ||
createItems: (batch, oldDb) async { | ||
await oldDb.into(oldDb.accounts).insert(v1.AccountsCompanion.insert( | ||
realmUrl: 'https://chat.example/', | ||
userId: 1, | ||
email: '[email protected]', | ||
apiKey: '1234', | ||
zulipVersion: '6.0', | ||
zulipMergeBase: const Value('6.0'), | ||
zulipFeatureLevel: 42, | ||
)); | ||
oldAccountData = await oldDb.select(oldDb.accounts).watchSingle().first; | ||
}, | ||
validateItems: (newDb) async { | ||
final account = await newDb.select(newDb.accounts).getSingle(); | ||
check(account.toJson()).deepEquals({ | ||
...oldAccountData.toJson(), | ||
'ackedPushToken': null, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specific command we can put here, or perhaps refer the reader to some documentation, even if it's just
dart run build_runner --help
? (Thedart run
seems helpful to a contributor arriving here for the first time.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't include the command for this as there are different ways to generate them, and some are more efficient the the others. We should point to the relevant piece of documentation ("Generated files" in
README.md
), or move this section to the README.