Skip to content

Commit

Permalink
Devtools extension: Button to clear database
Browse files Browse the repository at this point in the history
This drops contents and then calls beforeOpen, reinitializing the
database with the current migration.

Closes #3276
  • Loading branch information
simolus3 committed Oct 10, 2024
1 parent 4cadb06 commit bd80a46
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions drift/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- To infer whether serialization is required for inter-isolate communication,
drift now sends a test message instead serializing by default.
- The DevTools extension can now clear drift databases.

## 2.20.2

Expand Down
22 changes: 22 additions & 0 deletions drift/lib/src/runtime/devtools/service_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ class DriftServiceExtension {
});

return executor.statements;
case 'clear':
final database = tracked.database;
await database.exclusively(() async {
// https://stackoverflow.com/a/65743498/25690041
await database.customStatement('PRAGMA writable_schema = 1;');
await database.customStatement('DELETE FROM sqlite_master;');
await database.customStatement('VACUUM;');
await database.customStatement('PRAGMA writable_schema = 0;');
await database.customStatement('PRAGMA integrity_check');

await database.customStatement('PRAGMA user_version = 0');
await database.beforeOpen(database.resolvedEngine.executor,
OpeningDetails(null, database.schemaVersion));
await database.customStatement(
'PRAGMA user_version = ${database.schemaVersion}');

// Refresh all stream queries
database.notifyUpdates({
for (final table in database.allTables) TableUpdate.onTable(table)
});
});
return true;
default:
throw UnsupportedError('Method $action');
}
Expand Down
60 changes: 60 additions & 0 deletions extras/drift_devtools_extension/lib/src/clear_button.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:devtools_app_shared/ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'details.dart';

class ClearDatabaseButton extends ConsumerStatefulWidget {
const ClearDatabaseButton({super.key});

@override
ConsumerState<ClearDatabaseButton> createState() =>
_ClearDatabaseButtonState();
}

class _ClearDatabaseButtonState extends ConsumerState<ClearDatabaseButton> {
Future<void>? _pendingClear = null;

@override
Widget build(BuildContext context) {
return DevToolsButton(
onPressed: () {
showDevToolsDialog(
context: context,
title: 'Confirm deletion',
content: const Text(
'This will delete contents of the database and then re-create it. '
'All current database data will be lost. Continue?',
),
actions: [
DevToolsButton(
onPressed: _pendingClear != null
? null
: () {
setState(() {
_pendingClear = Future(() async {
final database = ref.read(loadedDatabase);
await database.value!.clear();
})
.whenComplete(
() => setState(() => _pendingClear = null))
.then((_) {
if (mounted) {
// ignore: use_build_context_synchronously
Navigator.pop(context);
}
});
});
},
label: 'Confirm deletion',
color: Colors.redAccent,
),
],
);
},
label: 'Clear database',
color: Colors.redAccent,
icon: Icons.delete,
);
}
}
9 changes: 9 additions & 0 deletions extras/drift_devtools_extension/lib/src/details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:drift_devtools_extension/src/schema_validator.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'clear_button.dart';
import 'db_viewer/viewer.dart';
import 'list.dart';
import 'remote_database.dart';
Expand Down Expand Up @@ -65,6 +66,14 @@ class _DatabaseDetailsState extends ConsumerState<DatabaseDetails> {
padding: EdgeInsets.all(8),
child: DatabaseSchemaCheck(),
),
const Padding(
padding: EdgeInsets.all(8),
child: Row(
children: [
ClearDatabaseButton(),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
Expand Down
4 changes: 4 additions & 0 deletions extras/drift_devtools_extension/lib/src/remote_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class RemoteDatabase {
return (res as List).cast();
}

Future<void> clear() async {
await _driftRequest('clear');
}

Future<int> _newTableSubscription() async {
final result = await _driftRequest('subscribe-to-tables');
return result as int;
Expand Down

0 comments on commit bd80a46

Please sign in to comment.