Initial project
This commit is contained in:
40
apps/im_app/lib/data/local/drift/app_database.dart
Normal file
40
apps/im_app/lib/data/local/drift/app_database.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:im_app/data/local/drift/tables/users.dart';
|
||||
|
||||
part 'app_database.g.dart';
|
||||
|
||||
@DriftDatabase(tables: [Users])
|
||||
class AppDatabase extends _$AppDatabase {
|
||||
AppDatabase(super.e);
|
||||
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
|
||||
@override
|
||||
MigrationStrategy get migration {
|
||||
return MigrationStrategy(
|
||||
onCreate: (m) async {
|
||||
await m.createAll();
|
||||
},
|
||||
onUpgrade: (m, from, to) async {
|
||||
// 自动检测并添加缺失列
|
||||
for (final table in allTables) {
|
||||
//取原来的字段
|
||||
final existingColumns = await m.database
|
||||
.customSelect('PRAGMA table_info(${table.actualTableName})')
|
||||
.get();
|
||||
final existingNames = existingColumns
|
||||
.map((r) => r.data['name'] as String)
|
||||
.toSet();
|
||||
|
||||
for (final column in table.$columns) {
|
||||
if (!existingNames.contains(column.name)) {
|
||||
//字段缺失,添加。
|
||||
await m.addColumn(table, column);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user