Skip to content

Commit

Permalink
added resetDatabase() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mikev-cw committed Apr 26, 2024
1 parent ad5917b commit 26ca899
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
29 changes: 22 additions & 7 deletions lib/database/sossoldi_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,30 @@ class SossoldiDatabase {
demoTransactions.add('''('$salaryDateTime', $fakeSalary, 'IN', 'Salary', 15, 70, null, 0, null, '$salaryDateTime', '$salaryDateTime')''');
}

// add some recurring payment too
// TODO TODO TODO TODO TODO TODO TODO
// demoTransactions.add('''(null, 7.99, 'OUT', 'Netflix', 14, 71, null, 1, 'monthly', 19, '2022-11-14', null, '2022-11-14 03:33:36.048611', '2022-11-14 03:33:36.048611')''');
// demoTransactions.add('''(null, 292.39, 'OUT', 'Car Loan', 13, 70, null, 1, 'monthly', 27, '2019-10-03', '2024-10-02', '2022-10-04 03:33:36.048611', '2022-10-04 03:33:36.048611')''');

// finalize query and write!
await _database?.execute("$insertDemoTransactionsQuery ${demoTransactions.join(",")};");
}

Future resetDatabase() async {
// delete database
try{
await _database?.transaction((txn) async {
var batch = txn.batch();
// drop tables
batch.execute('DROP TABLE IF EXISTS $bankAccountTable');
batch.execute('DROP TABLE IF EXISTS `$transactionTable`');
batch.execute('DROP TABLE IF EXISTS $recurringTransactionTable');
batch.execute('DROP TABLE IF EXISTS $categoryTransactionTable');
batch.execute('DROP TABLE IF EXISTS $budgetTable');
batch.execute('DROP TABLE IF EXISTS $currencyTable');
await batch.commit();
});
} catch(error){
throw Exception('DbBase.resetDatabase: $error');
}
await _createDB(_database!, 1);
}

Future clearDatabase() async {
try{
await _database?.transaction((txn) async {
Expand All @@ -267,7 +282,7 @@ class SossoldiDatabase {
await batch.commit();
});
} catch(error){
throw Exception('DbBase.cleanDatabase: $error');
// throw Exception('DbBase.cleanDatabase: $error');
}
}

Expand All @@ -279,7 +294,7 @@ class SossoldiDatabase {
// WARNING: FOR DEV/TEST PURPOSES ONLY!!
Future<void> deleteDatabase() async {
final databasePath = await getDatabasesPath();
final path = join(databasePath, 'sossoldi.db');
final path = join(databasePath, dbName);
databaseFactory.deleteDatabase(path);
}
}
4 changes: 2 additions & 2 deletions lib/pages/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
textAlign: TextAlign.center,
),
ElevatedButton(
child: const Text('CLEAR DB'),
child: const Text('RESET DB'),
onPressed: () async {
await SossoldiDatabase.instance.clearDatabase().then((v) {
await SossoldiDatabase.instance.resetDatabase().then((v) {
ref.refresh(accountsProvider);
ref.refresh(categoriesProvider);
ref.refresh(transactionsProvider);
Expand Down

0 comments on commit 26ca899

Please sign in to comment.