113 lines
4.2 KiB
Dart
113 lines
4.2 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:drift/drift.dart';
|
|
import 'package:drift/native.dart';
|
|
import 'package:path/path.dart' as p;
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:uto_manager/infrastructure/database/daos/personnel_dao.dart';
|
|
import 'package:uto_manager/infrastructure/database/daos/position_slot_dao.dart';
|
|
import 'package:uto_manager/infrastructure/database/daos/template_dao.dart';
|
|
import 'package:uto_manager/infrastructure/database/daos/unit_dao.dart';
|
|
|
|
part 'app_database.g.dart';
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Table Definitions
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Drift table for organizational unit nodes.
|
|
class Units extends Table {
|
|
TextColumn get unitId => text().named('unit_id')();
|
|
TextColumn get echelon => text()();
|
|
TextColumn get branch => text()();
|
|
TextColumn get parentUnitId => text().named('parent_unit_id').nullable()();
|
|
TextColumn get name => text()();
|
|
BoolColumn get isKeyLeader => boolean().named('is_key_leader')();
|
|
|
|
@override
|
|
Set<Column> get primaryKey => {unitId};
|
|
}
|
|
|
|
/// Drift table for authorized duty position slots.
|
|
class PositionSlots extends Table {
|
|
TextColumn get slotId => text().named('slot_id')();
|
|
TextColumn get unitId => text().named('unit_id').references(Units, #unitId)();
|
|
TextColumn get positionTitle => text().named('position_title')();
|
|
TextColumn get dutyMos => text().named('duty_mos')();
|
|
TextColumn get authorizedRank => text().named('authorized_rank')();
|
|
BoolColumn get isKeyLeader => boolean().named('is_key_leader')();
|
|
TextColumn get assignedPersonnelId => text().named('assigned_personnel_id').nullable()();
|
|
|
|
@override
|
|
Set<Column> get primaryKey => {slotId};
|
|
}
|
|
|
|
/// Drift table for soldier personnel records.
|
|
class Personnel extends Table {
|
|
TextColumn get soldierId => text().named('soldier_id')();
|
|
TextColumn get name => text()();
|
|
TextColumn get primaryMos => text().named('primary_mos')();
|
|
TextColumn get payGrade => text().named('pay_grade')();
|
|
TextColumn get qualifications => text()(); // JSON-encoded list
|
|
TextColumn get readinessState => text().named('readiness_state')();
|
|
TextColumn get assignedSlotId => text().named('assigned_slot_id').nullable()();
|
|
|
|
@override
|
|
Set<Column> get primaryKey => {soldierId};
|
|
}
|
|
|
|
/// Drift table for serialized unit tree templates.
|
|
class Templates extends Table {
|
|
TextColumn get templateId => text().named('template_id')();
|
|
TextColumn get name => text()();
|
|
TextColumn get bctType => text().named('bct_type')();
|
|
TextColumn get jsonData => text().named('json_data')();
|
|
DateTimeColumn get createdAt => dateTime().named('created_at')();
|
|
DateTimeColumn get updatedAt => dateTime().named('updated_at')();
|
|
|
|
@override
|
|
Set<Column> get primaryKey => {templateId};
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Database
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// The main Drift database for the UTO Manager application.
|
|
///
|
|
/// Manages all four core tables: units, position_slots, personnel, templates.
|
|
/// Supports SQLite on Windows/Android and IndexedDB on Web via drift_web.
|
|
@DriftDatabase(
|
|
tables: [Units, PositionSlots, Personnel, Templates],
|
|
daos: [UnitDao, PersonnelDao, PositionSlotDao, TemplateDao],
|
|
)
|
|
class AppDatabase extends _$AppDatabase {
|
|
/// Creates the production database backed by SQLite on disk.
|
|
AppDatabase() : super(_openConnection());
|
|
|
|
/// Constructor for in-memory database (used in tests).
|
|
AppDatabase.forTesting(super.executor);
|
|
|
|
@override
|
|
int get schemaVersion => 1;
|
|
|
|
@override
|
|
MigrationStrategy get migration => MigrationStrategy(
|
|
onCreate: (m) async {
|
|
await m.createAll();
|
|
},
|
|
onUpgrade: (m, from, to) async {
|
|
// Future migration steps added here as schemaVersion increases.
|
|
},
|
|
);
|
|
}
|
|
|
|
/// Opens the SQLite database connection for native platforms.
|
|
LazyDatabase _openConnection() {
|
|
return LazyDatabase(() async {
|
|
final dbFolder = await getApplicationDocumentsDirectory();
|
|
final file = File(p.join(dbFolder.path, 'uto_manager.sqlite'));
|
|
return NativeDatabase.createInBackground(file);
|
|
});
|
|
}
|