Member-only story
pubspec.yaml
sqflite: ^1.3.2+1
lib/services/local/migrate/migrate.dart
import 'package:sqflite/sqflite.dart';abstract class Migrate {
/// Handle the creation of a fresh database in onCreate
Future<void> create(Batch batch); /// Handle the schema migration in onUpgrade
Future<void> upgrade(Batch batch); /// Clean table
void deleteRepo(Batch batch, String repoName) {
batch.execute('DELETE TABLE IF EXISTS $repoName');
} /// Add column
/// Ex: columnAndType = 'updated INTEGER'
void addColumn(Batch batch, String repoName, String columnAndType) {
batch.execute('ALTER TABLE IF EXISTS $repoName ADD COLUMN $columnAndType;');
} /// Drop repo
void dropRepo(Batch batch, String repoName) {
batch.execute('DROP TABLE IF EXISTS $repoName;');
}
}
lib/services/local/migrate/migrate_v1.dart
import 'package:sqflite/sqlite_api.dart';import '../repo/config_repo.dart';
import 'migrate.dart';class MigrateV1 extends Migrate {
@override
Future<void> create(Batch batch) async {
await ConfigRepo().create(batch);
} @override
Future<void> upgrade(Batch batch) async {
/// With the first version (v1) no need to upgrade anything
/// do nothing here
}
}