-
Notifications
You must be signed in to change notification settings - Fork 18
/
migrator.dart
38 lines (31 loc) · 1.14 KB
/
migrator.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
import 'dart:async';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_migration/src/migration_config.dart';
///
/// An internal class which contains methods to execute the initial and
/// migration scripts.
///
/// [config] (required) the migration configuration to execute.
///
class Migrator {
final MigrationConfig config;
Migrator(this.config);
Future<void> executeInitialization(DatabaseExecutor db, int version) async {
for (String script in config.initializationScript) {
await db.execute(script);
}
for (String script in config.migrationScripts) {
await db.execute(script);
}
}
Future<void> executeMigration(
DatabaseExecutor db, int oldVersion, int newVersion) async {
assert(oldVersion < newVersion,
'The newVersion($newVersion) should always be greater than the oldVersion($oldVersion).');
assert(config.migrationScripts.length == newVersion - 1,
'New version ($newVersion) requires exact ${newVersion - config.migrationScripts.length} migrations.');
for (var i = oldVersion - 1; i < newVersion - 1; i++) {
await db.execute(config.migrationScripts[i]);
}
}
}