import 'package:drift/drift.dart'; import 'dart:io'; import 'package:drift/native.dart'; import 'package:path_provider/path_provider.dart'; import 'package:path/path.dart' as p; import 'package:sqlite3/sqlite3.dart'; import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart'; part 'database.g.dart'; var database = AppDatabase(); //global, since we should only use one instance class PersistentNote extends Table { TextColumn get id => text()(); TextColumn get content => text()(); } class PersistentTheme extends Table { TextColumn get brightness => text().withDefault(const Constant("dark"))(); } @DriftDatabase(tables: [PersistentNote, PersistentTheme]) class AppDatabase extends _$AppDatabase { AppDatabase() : super(_openConnection()); @override int get schemaVersion => 2; @override MigrationStrategy get migration { return MigrationStrategy( onCreate: (Migrator m) async { await m.createAll(); }, onUpgrade: (Migrator m, int from, int to) async { if (from < 2) { await m.renameTable(persistentNote, "note_table"); await m.createTable(persistentTheme); } }, ); } } LazyDatabase _openConnection() { return LazyDatabase(() async { final dbFolder = await getApplicationDocumentsDirectory(); final file = File(p.join(dbFolder.path, 'db.sqlite')); if (Platform.isAndroid) { await applyWorkaroundToOpenSqlite3OnOldAndroidVersions(); } final cachebase = (await getTemporaryDirectory()).path; sqlite3.tempDirectory = cachebase; return NativeDatabase.createInBackground(file); }); }