migrate method
Applies all pending migrations.
Creates the _dartapi_migrations tracking table if it does not exist.
Runs each unapplied .sql file inside a transaction and records it.
Implementation
Future<void> migrate() async {
await _ensureMigrationsTable();
final applied = await _appliedMigrations();
final pending = await _pendingMigrations(applied);
if (pending.isEmpty) {
print('✅ No pending migrations.');
return;
}
for (final file in pending) {
final name = _fileName(file);
print('⏳ Applying migration: $name');
final sql = file.readAsStringSync();
await db.transaction((tx) async {
await tx.rawQuery(sql);
await tx.rawQuery(
'INSERT INTO _dartapi_migrations (name) VALUES (:name);',
values: {'name': name},
);
});
print('✅ Applied: $name');
}
print('🎉 All migrations applied (${pending.length} total).');
}