Skip to content

Commit

Permalink
refator: migrate instead of handle exception
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagocarvalhodev committed Sep 12, 2024
1 parent 5554b4a commit 52b69c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
19 changes: 0 additions & 19 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,6 @@ void main() async {
await _startApp();
}, (error, stackTrace) async {
logger.e('Error caught.', error, stackTrace);

if (error.toString().contains('snapshot_entries')) {
logger.d('Handling snapshot_entries exception');
WidgetsFlutterBinding.ensureInitialized();

final database = Database();
logger.d('Dropping snapshot_entries table');

await database.customStatement('''
DROP TABLE IF EXISTS snapshot_entries;
''');

logger.d('Re-initializing services');

await _initializeServices();

logger.d('Restarting app');
_startApp();
}
});
}

Expand Down
14 changes: 13 additions & 1 deletion lib/models/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Database extends _$Database {
Database([QueryExecutor? e]) : super(e ?? openConnection());

@override
int get schemaVersion => 21;
int get schemaVersion => 22;
@override
MigrationStrategy get migration => MigrationStrategy(
onCreate: (Migrator m) {
Expand Down Expand Up @@ -130,6 +130,18 @@ class Database extends _$Database {
await m.createTable(arnsRecords);
await m.createTable(antRecords);
}

if (from < 22) {
logger.d('Migrating schema from v21 to v22');

logger.d('Dropping snapshot_entries table');

await customStatement('''
DROP TABLE IF EXISTS snapshot_entries;
''');

logger.d('snapshot_entries table dropped');
}
} catch (e, stacktrace) {
logger.e(
'CRITICAL! Failed to migrate database from $from to $to',
Expand Down

0 comments on commit 52b69c1

Please sign in to comment.