migration property

  1. @override
MigrationStrategy get migration
override

Defines the migration strategy that will determine how to deal with an increasing schemaVersion. The default value only supports creating the database by creating all tables known in this database. When you have changes in your schema, you'll need a custom migration strategy to create the new tables or change the columns.

Implementation

@override
MigrationStrategy get migration {
  return MigrationStrategy(
    onUpgrade: (m, from, to) async {
      Log.info('MigrationStrategy.onUpgrade($from, $to)', '$runtimeType');

      if (_closed) {
        return;
      }

      if (from != to) {
        bool migrated = false;

        try {
          if (to >= 7 && from <= 6) {
            await m.addColumn(settings, settings.videoVolume);
            migrated = true;
          }

          if (to >= 6 && from <= 5) {
            await m.addColumn(settings, settings.noiseSuppression);
            await m.addColumn(settings, settings.noiseSuppressionLevel);
            await m.addColumn(settings, settings.echoCancellation);
            await m.addColumn(settings, settings.autoGainControl);
            await m.addColumn(settings, settings.highPassFilter);
            migrated = true;
          }

          if (to >= 5 && from <= 4) {
            await m.addColumn(settings, settings.muteKeys);
            migrated = true;
          }

          if (to >= 4 && from <= 3) {
            await m.addColumn(versions, versions.blocklistVersion);
            await m.addColumn(versions, versions.blocklistCount);
            migrated = true;
          }

          if (to >= 3 && from <= 2) {
            await m.addColumn(myUsers, myUsers.welcomeMessage);
            migrated = true;
          }

          if (to >= 2 && from <= 1) {
            await m.addColumn(versions, versions.sessionsListVersion);
            migrated = true;
          }
        } catch (e) {
          // Should log the error, but proceed with initialization, as
          // otherwise `drift` won't allow application to run at all.
          Log.error(
            'Unable to perform migrations due to: $e',
            '$runtimeType',
          );
        }

        if (!migrated) {
          Log.info(
            'MigrationStrategy.onUpgrade($from, $to) -> migration did not succeed, thus deleting the tables',
            '$runtimeType',
          );

          for (var e in m.database.allTables) {
            await m.deleteTable(e.actualTableName);
          }
        } else {
          Log.info(
            'MigrationStrategy.onUpgrade($from, $to) -> migration did succeed',
            '$runtimeType',
          );
        }
      }

      await m.createAll();
    },
    beforeOpen: (_) async {
      Log.debug('MigrationStrategy.beforeOpen()', '$runtimeType');

      if (_closed) {
        return;
      }

      try {
        await customStatement('PRAGMA foreign_keys = ON;');

        // Note, that WAL doesn't work in Web:
        // https://github.com/simolus3/sqlite3.dart/issues/200
        await customStatement('PRAGMA journal_mode = WAL;');
      } catch (e) {
        Log.error('Custom SQL statement has failed: $e', '$runtimeType');
      }
    },
  );
}