-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwith_migrations.dart
55 lines (42 loc) · 1.44 KB
/
with_migrations.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import 'package:drift/drift.dart';
import 'package:drift_sqlite_async/drift_sqlite_async.dart';
import 'package:sqlite_async/sqlite_async.dart';
part 'with_migrations.g.dart';
class TodoItems extends Table {
@override
String get tableName => 'todos';
IntColumn get id => integer().autoIncrement()();
TextColumn get description => text()();
}
@DriftDatabase(tables: [TodoItems])
class AppDatabase extends _$AppDatabase {
AppDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));
@override
int get schemaVersion => 1;
@override
MigrationStrategy get migration {
return MigrationStrategy(onCreate: (m) async {
// In this example, the schema is managed by Drift.
// For more options, see:
// https://drift.simonbinder.eu/migrations/#usage
await m.createAll();
});
}
}
Future<void> main() async {
final db = SqliteDatabase(path: 'with_migrations.db');
final appdb = AppDatabase(db);
// Watch a query on the Drift database
appdb.select(appdb.todoItems).watch().listen((todos) {
print('Todos: $todos');
});
// Insert using the Drift database
await appdb
.into(appdb.todoItems)
.insert(TodoItemsCompanion.insert(description: 'Test Drift'));
// Insert using the sqlite_async database
await db.execute('INSERT INTO todos(description) VALUES(?)', ['Test Direct']);
await Future.delayed(const Duration(milliseconds: 100));
await appdb.close();
await db.close();
}