From 52b69c15e76c58b5b8f97eb52b63cbfc212551cb Mon Sep 17 00:00:00 2001 From: Thiago Carvalho <32248947+thiagocarvalhodev@users.noreply.github.com> Date: Thu, 12 Sep 2024 15:22:27 -0300 Subject: [PATCH] refator: migrate instead of handle exception --- lib/main.dart | 19 ------------------- lib/models/database/database.dart | 14 +++++++++++++- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index e81c98c90..1c6556f79 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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(); - } }); } diff --git a/lib/models/database/database.dart b/lib/models/database/database.dart index 6bd51a4ca..3e152d366 100644 --- a/lib/models/database/database.dart +++ b/lib/models/database/database.dart @@ -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) { @@ -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',