@@ -5,9 +5,22 @@ import 'package:sqlite3/sqlite3.dart';
55
66import 'sqlite_connection.dart' ;
77
8+ /// Migrations to initialize and update a database.
89class SqliteMigrations {
10+ /// Name of table used to store migrations.
11+ ///
12+ /// Defaults to "_migrations".
913 String migrationTable;
14+
15+ /// List of migrations to execute, in order. Use [add] to add new migrations.
1016 List <SqliteMigration > migrations = [];
17+
18+ /// Optional: Migration to create database from scratch.
19+ ///
20+ /// Use this to speed up initialization for a fresh database.
21+ ///
22+ /// This must be supplied _in addition to_ migrations for the same version,
23+ /// and should produce the same state for the database.
1124 SqliteMigration ? createDatabase;
1225
1326 SqliteMigrations ({this .migrationTable = "_migrations" });
@@ -34,10 +47,12 @@ class SqliteMigrations {
3447 migrations.add (migration);
3548 }
3649
50+ /// The current version as specified by the migrations.
3751 get version {
3852 return migrations.isEmpty ? 0 : migrations.last.toVersion;
3953 }
4054
55+ /// Get the last applied migration version in the database.
4156 Future <int > getCurrentVersion (SqliteWriteContext db) async {
4257 try {
4358 final currentVersionRow = await db.getOptional (
@@ -69,6 +84,9 @@ class SqliteMigrations {
6984 }
7085 }
7186
87+ /// Initialize or update the database.
88+ ///
89+ /// Throws MigrationError if the database cannot be migrated.
7290 Future <void > migrate (SqliteConnection db) async {
7391 _validateCreateDatabase ();
7492
@@ -167,12 +185,22 @@ class MigrationError extends Error {
167185typedef SqliteMigrationFunction = FutureOr <void > Function (
168186 SqliteWriteContext tx);
169187
188+ /// A migration for a single database version.
170189class SqliteMigration {
190+ /// Function to execute for the migration.
171191 final SqliteMigrationFunction fn;
192+
193+ /// Database version that this migration upgrades to.
172194 final int toVersion;
173195
174196 /// Optional: Add a down migration to allow this migration to be reverted
175197 /// if the user installs an older version.
198+ ///
199+ /// If the user will never downgrade the application/database version, this is
200+ /// not required.
201+ ///
202+ /// Limited downgrade support can be added by only providing downMigrations
203+ /// for the last migration(s).
176204 SqliteDownMigration ? downMigration;
177205
178206 SqliteMigration (this .toVersion, this .fn, {this .downMigration});
@@ -189,12 +217,18 @@ class _SqliteMigrationStatement {
189217 }
190218}
191219
220+ /// Set of down migration statements, persisted in the database.
221+ ///
222+ /// Since this will execute in an older application version, only static
223+ /// SQL statements are supported.
192224class SqliteDownMigration {
225+ /// The database version after this downgrade.
193226 final int toVersion;
194227 final List <_SqliteMigrationStatement > _statements = [];
195228
196229 SqliteDownMigration ({required this .toVersion});
197230
231+ /// Add an statement to execute to downgrade the database version.
198232 add (String sql, [List <Object ?>? params]) {
199233 _statements.add (_SqliteMigrationStatement (sql, params ?? []));
200234 }
0 commit comments