up method

  1. @override
Future up()
override

Implementation

@override
Future up() async {
  await _init();
  var r = await connection.query('SELECT path from migrations;');
  var existing = r.expand((x) => x).cast<String>();
  var toRun = <String>[];

  migrations.forEach((k, v) {
    if (!existing.contains(k)) toRun.add(k);
  });

  if (toRun.isNotEmpty) {
    var r = await connection.query('SELECT MAX(batch) from migrations;');
    var curBatch = (r[0][0] ?? 0) as int;
    var batch = curBatch + 1;

    for (var k in toRun) {
      var migration = migrations[k]!;
      var schema = PostgresSchema();
      migration.up(schema);
      _log.info('Added "$k" into "migrations" table.');
      await schema.run(connection).then((_) {
        return connection.transaction((ctx) async {
          var result = await ctx.query(
              "INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, '$k')");

          return result.affectedRowCount;
        });
        //return connection.execute(
        //    'INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, \'$k\');');
      }).catchError((e) {
        _log.severe('Failed to insert into "migrations" table.');
      });
    }
  } else {
    _log.warning('Nothing to add into "migrations" table.');
  }
}