MigrationPlan constructor

MigrationPlan(
  1. Map<int, List<Migration>> _migrationsByVersion
)

Build a MigrationPlan from a map associating database versions to the list of Migrations that cumulatively upgrade to (or downgrade from) that version.

MigrationPlan myMigrationPlan = MigrationPlan({

  2: [ //Migration for v2: create a table using SQL Operation
    SqlMigration('''CREATE TABLE $_table ( $_my_columns )''')
                   reverseSql: 'DROP TABLE $_table')// Reverse drops the table
  ],

  3: [  //Migration for v3: add initial records using custom function
    Migration(Operation((db) async {
      _insertRecord(Entity(null, "Mordecai", "Pitcher"), db);
      _insertRecord(Entity(null, "Tommy", "Pitcher"), db);
      _insertRecord(Entity(null, "Max", "Center Field"), db);
    }), reverse: Operation((db) async => db.execute('DELETE FROM $_table')))
  ],

  4: [// Two migration for v4: add a column, set initial values
    // a custom Migration defines desired behavior of adding/removing col
    AddColumnMigration(_table, _columnHome['name']!, _columnHome['def']!,
        [_columnId, _columnName, _columnDesc]),
    Migration.fromOperationFunctions((db) async => db.execute(
        'UPDATE $_table SET ${_columnHome['name']} = \'Terre Haute\'')),
    // Omit the reverse; no action on downgrade of this operation
  ],
});

Implementation

MigrationPlan(this._migrationsByVersion);