-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathactions.dart
More file actions
28 lines (26 loc) · 966 Bytes
/
actions.dart
File metadata and controls
28 lines (26 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import 'package:drift/drift.dart';
import 'package:tattoo/database/database.dart';
/// Reusable database operations shared across repositories.
extension DatabaseActions on AppDatabase {
/// Drops and recreates all tables, fully resetting the database.
Future<void> deleteEverything() async {
await transaction(() async {
final m = Migrator(this);
final reversed = allSchemaEntities.toList().reversed;
for (final entity in reversed) {
await m.drop(entity);
}
await m.createAll();
});
}
/// Returns the ID of an existing semester row, or creates one if missing.
Future<int> getOrCreateSemester(int year, int term) async {
return (await into(semesters).insertReturning(
SemestersCompanion.insert(year: year, term: term),
onConflict: DoUpdate(
(old) => SemestersCompanion(year: Value(year), term: Value(term)),
target: [semesters.year, semesters.term],
),
)).id;
}
}