Commit 774dd22

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2024-03-16 17:05:19
implement DatabaseManager
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 734f220
Changed files (2)
health_data_store
health_data_store/lib/src/database_manager.dart
@@ -0,0 +1,72 @@
+
+import 'package:sqflite_common/sqlite_api.dart';
+
+/// Manager for the database.
+/// 
+/// Responsible for setting up the database and performing schema and version 
+/// updates.
+class DatabaseManager {
+  DatabaseManager._create(this._db);
+
+  /// Initialize the manager from a database.
+  ///
+  /// If [db] doesn't contain a scheme or contains an outdated scheme, one will
+  /// be created.
+  static Future<DatabaseManager> load(Database db) async {
+    final dbMngr = DatabaseManager._create(db);
+
+    if (await dbMngr._db.getVersion() < 3) {
+      await dbMngr._setUpTables();
+      await dbMngr._db.setVersion(3);
+    }
+    // When updating the schema the update steps are maintained for ensured 
+    // compatability.
+    
+    return dbMngr;
+  }
+
+  final Database _db;
+
+  /// Get the database.
+  Database get db => _db.database;
+  
+  Future<void> _setUpTables() async {
+    await _db.execute('CREATE TABLE "Medicine" ('
+      '"medID"       INTEGER NOT NULL UNIQUE,'
+      '"designation" TEXT NOT NULL,'
+      '"defaultDose" REAL,'
+      'PRIMARY KEY("medID")'
+    ');');
+    await _db.execute('CREATE TABLE "Timestamps" ('
+      '"entryID"	      INTEGER NOT NULL UNIQUE,'
+      '"timestampUnixS"	INTEGER NOT NULL,'
+      'PRIMARY KEY("entryID")'
+    ');');
+    await _db.execute('CREATE TABLE "Intake" ('
+      '"entryID" INTEGER NOT NULL,'
+      '"medID"	 INTEGER NOT NULL,'
+      '"dosis"	 INTEGER NOT NULL,'
+      'PRIMARY KEY("entryID"),'
+      'FOREIGN KEY("entryID") REFERENCES "Timestamps"("entryID"),'
+      'FOREIGN KEY("medID") REFERENCES "Medicine"("medID")'
+    ');');
+    for (final intTable in ['Systolic', 'Diastolic', 'Pulse']) {
+      await _db.execute('CREATE TABLE "$intTable" ('
+        '"entryID"	INTEGER NOT NULL,'
+        '"value"    INTEGER,'
+        'FOREIGN KEY("entryID") REFERENCES "Timestamps"("entryID"),'
+        'PRIMARY KEY("entryID")'
+      ');');
+    }
+    await _db.execute('CREATE TABLE "Notes" ('
+      '"entryID"	INTEGER NOT NULL,'
+      '"note"     TEXT,'
+      '"color"    INTEGER,'
+      'FOREIGN KEY("entryID") REFERENCES "Timestamps"("entryID"),'
+      'PRIMARY KEY("entryID")'
+    ');');
+  }
+
+  /// Closes the database.
+  Future<void> close() => _db.close();
+}
health_data_store/test/src/database_manager_test.dart
@@ -0,0 +1,26 @@
+import 'package:health_data_store/src/database_manager.dart';
+import 'package:sqflite_common/sqflite.dart';
+import 'package:sqflite_common_ffi/sqflite_ffi.dart';
+import 'package:test/test.dart';
+
+void main() {
+  group('DatabaseManager', () {
+    setUpAll(() => databaseFactory = databaseFactoryFfi);
+    test('should initialize without issues', () async {
+      final db = await DatabaseManager.load(await openDatabase(
+        inMemoryDatabasePath,
+      ));
+      expect(db.db.isOpen, isTrue);
+    });
+    test('should close', () async {
+      final db = await DatabaseManager.load(await openDatabase(
+        inMemoryDatabasePath,
+      ));
+      expect(db.db.isOpen, isTrue);
+      await db.close();
+      expect(db.db.isOpen, isFalse);
+    });
+    // TODO: test more
+    throw UnimplementedError('TODO');
+  });
+}