onUpgrade static method

FutureOr<void> onUpgrade(
  1. Database db,
  2. int oldVersion,
  3. int newVersion
)

Implementation

static FutureOr<void> onUpgrade(
  Database db,
  int oldVersion,
  int newVersion,
) async {
  if (oldVersion < 6) {
    await db.execute(
      'create table $_table('
      ' $_columnId INT NOT NULL'
      ',$_columnType TEXT NOT NULL'
      ',$_columnLongitude REAL NOT NULL'
      ',$_columnLatitude REAL NOT NULL'
      ',$_columnName TEXT'
      ',$_columnStreet TEXT'
      ',$_columnCity TEXT'
      ',$_columnPostCode TEXT'
      ',$_columnCountry TEXT'
      ',$_columnCountryCode TEXT'
      ',$_columnLastAccess INT NOT NULL'
      // cf. https://www.sqlite.org/lang_conflict.html
      ',PRIMARY KEY($_columnId,$_columnType) on conflict replace'
      ')',
    );
  }

  /// Not brilliant, but for historical reasons we have to catch that here.
  bool isDuplicateColumnException(
    DatabaseException e,
    String column,
  ) => e.toString().startsWith(
    'DatabaseException(duplicate column name: $column (code 1 SQLITE_ERROR',
  );

  if (oldVersion < 7) {
    try {
      await db.execute('alter table $_table add column $_columnOsmKey TEXT');
    } on DatabaseException catch (e) {
      if (!isDuplicateColumnException(e, _columnOsmKey)) {
        rethrow;
      }
    }
    try {
      await db.execute(
        'alter table $_table add column $_columnOsmValue TEXT',
      );
    } on DatabaseException catch (e) {
      if (!isDuplicateColumnException(e, _columnOsmValue)) {
        rethrow;
      }
    }
  }
}