fix: Migrations don't fail anymore if they were partly completed
This commit is contained in:
parent
84c27129d2
commit
631b28eab2
|
@ -11,6 +11,39 @@ import '../utils/logs.dart';
|
||||||
|
|
||||||
part 'database.g.dart';
|
part 'database.g.dart';
|
||||||
|
|
||||||
|
extension MigratorExtension on Migrator {
|
||||||
|
Future<void> createIndexIfNotExists(Index index) async {
|
||||||
|
try {
|
||||||
|
await createIndex(index);
|
||||||
|
} catch (err) {
|
||||||
|
if (!err.toString().toLowerCase().contains('already exists')) {
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> createTableIfNotExists(TableInfo<Table, DataClass> table) async {
|
||||||
|
try {
|
||||||
|
await createTable(table);
|
||||||
|
} catch (err) {
|
||||||
|
if (!err.toString().toLowerCase().contains('already exists')) {
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> addColumnIfNotExists(
|
||||||
|
TableInfo<Table, DataClass> table, GeneratedColumn column) async {
|
||||||
|
try {
|
||||||
|
await addColumn(table, column);
|
||||||
|
} catch (err) {
|
||||||
|
if (!err.toString().toLowerCase().contains('duplicate column name')) {
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@UseMoor(
|
@UseMoor(
|
||||||
include: {'database.moor'},
|
include: {'database.moor'},
|
||||||
)
|
)
|
||||||
|
@ -32,17 +65,17 @@ class Database extends _$Database {
|
||||||
onUpgrade: (Migrator m, int from, int to) async {
|
onUpgrade: (Migrator m, int from, int to) async {
|
||||||
// this appears to be only called once, so multiple consecutive upgrades have to be handled appropriately in here
|
// this appears to be only called once, so multiple consecutive upgrades have to be handled appropriately in here
|
||||||
if (from == 1) {
|
if (from == 1) {
|
||||||
await m.createIndex(userDeviceKeysIndex);
|
await m.createIndexIfNotExists(userDeviceKeysIndex);
|
||||||
await m.createIndex(userDeviceKeysKeyIndex);
|
await m.createIndexIfNotExists(userDeviceKeysKeyIndex);
|
||||||
await m.createIndex(olmSessionsIndex);
|
await m.createIndexIfNotExists(olmSessionsIndex);
|
||||||
await m.createIndex(outboundGroupSessionsIndex);
|
await m.createIndexIfNotExists(outboundGroupSessionsIndex);
|
||||||
await m.createIndex(inboundGroupSessionsIndex);
|
await m.createIndexIfNotExists(inboundGroupSessionsIndex);
|
||||||
await m.createIndex(roomsIndex);
|
await m.createIndexIfNotExists(roomsIndex);
|
||||||
await m.createIndex(eventsIndex);
|
await m.createIndexIfNotExists(eventsIndex);
|
||||||
await m.createIndex(roomStatesIndex);
|
await m.createIndexIfNotExists(roomStatesIndex);
|
||||||
await m.createIndex(accountDataIndex);
|
await m.createIndexIfNotExists(accountDataIndex);
|
||||||
await m.createIndex(roomAccountDataIndex);
|
await m.createIndexIfNotExists(roomAccountDataIndex);
|
||||||
await m.createIndex(presencesIndex);
|
await m.createIndexIfNotExists(presencesIndex);
|
||||||
from++;
|
from++;
|
||||||
}
|
}
|
||||||
if (from == 2) {
|
if (from == 2) {
|
||||||
|
@ -51,23 +84,23 @@ class Database extends _$Database {
|
||||||
from++;
|
from++;
|
||||||
}
|
}
|
||||||
if (from == 3) {
|
if (from == 3) {
|
||||||
await m.createTable(userCrossSigningKeys);
|
await m.createTableIfNotExists(userCrossSigningKeys);
|
||||||
await m.createTable(ssssCache);
|
await m.createTableIfNotExists(ssssCache);
|
||||||
// mark all keys as outdated so that the cross signing keys will be fetched
|
// mark all keys as outdated so that the cross signing keys will be fetched
|
||||||
await m.issueCustomQuery(
|
await m.issueCustomQuery(
|
||||||
'UPDATE user_device_keys SET outdated = true');
|
'UPDATE user_device_keys SET outdated = true');
|
||||||
from++;
|
from++;
|
||||||
}
|
}
|
||||||
if (from == 4) {
|
if (from == 4) {
|
||||||
await m.addColumn(olmSessions, olmSessions.lastReceived);
|
await m.addColumnIfNotExists(olmSessions, olmSessions.lastReceived);
|
||||||
from++;
|
from++;
|
||||||
}
|
}
|
||||||
if (from == 5) {
|
if (from == 5) {
|
||||||
await m.addColumn(
|
await m.addColumnIfNotExists(
|
||||||
inboundGroupSessions, inboundGroupSessions.uploaded);
|
inboundGroupSessions, inboundGroupSessions.uploaded);
|
||||||
await m.addColumn(
|
await m.addColumnIfNotExists(
|
||||||
inboundGroupSessions, inboundGroupSessions.senderKey);
|
inboundGroupSessions, inboundGroupSessions.senderKey);
|
||||||
await m.addColumn(
|
await m.addColumnIfNotExists(
|
||||||
inboundGroupSessions, inboundGroupSessions.senderClaimedKeys);
|
inboundGroupSessions, inboundGroupSessions.senderClaimedKeys);
|
||||||
from++;
|
from++;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue