mnemo_cards/mnemo_cards_backend/lib/database/tables/tests.dart

100 lines
3.8 KiB
Dart
Raw Normal View History

2025-12-13 13:27:05 +00:00
import 'package:drift/drift.dart';
2025-12-13 23:35:14 +00:00
import 'package:drift_postgres/drift_postgres.dart';
2025-12-13 20:55:50 +00:00
import '../converters.dart' show generateUuid, JsonMapConverter;
2025-12-13 13:27:05 +00:00
import 'packs.dart';
/// Таблица Tests - тесты
class Tests extends Table {
2025-12-13 23:35:14 +00:00
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
TextColumn get id => text()
.withDefault(const CustomExpression('gen_random_uuid()::text'))();
2025-12-13 13:27:05 +00:00
TextColumn get name => text()();
TextColumn get color => text().nullable()();
TextColumn get cover => text().nullable()();
TextColumn get version => text().nullable()();
TextColumn get time => text().nullable()();
TextColumn get timeSubtitle => text().nullable()();
// Audit
2025-12-13 23:35:14 +00:00
Column<PgDateTime> get createdAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
Column<PgDateTime> get updatedAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
2025-12-13 22:00:36 +00:00
BoolColumn get isDeleted => boolean()
2025-12-14 21:55:42 +00:00
.customConstraint('NOT NULL DEFAULT FALSE')();
2025-12-14 21:45:52 +00:00
// PostgreSQL использует нативный BOOLEAN
2025-12-14 20:36:05 +00:00
Column<PgDateTime> get deletedAt => customType(PgTypes.timestampWithTimezone)
.nullable()();
2025-12-13 20:55:50 +00:00
@override
Set<Column> get primaryKey => {id};
2025-12-13 13:27:05 +00:00
}
/// Таблица TestQuestions - вопросы тестов
class TestQuestions extends Table {
2025-12-13 23:35:14 +00:00
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
TextColumn get id => text()
.withDefault(const CustomExpression('gen_random_uuid()::text'))();
TextColumn get testId => text()
.references(Tests, #id, onDelete: KeyAction.cascade)();
2025-12-13 13:27:05 +00:00
// Тип вопроса (enum as string)
TextColumn get questionType => text()();
TextColumn get body => text()();
// Audit
2025-12-13 23:35:14 +00:00
Column<PgDateTime> get createdAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
Column<PgDateTime> get updatedAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
2025-12-14 20:36:05 +00:00
BoolColumn get isDeleted => boolean()
2025-12-14 21:55:42 +00:00
.customConstraint('NOT NULL DEFAULT FALSE')();
2025-12-14 21:45:52 +00:00
2025-12-14 20:36:05 +00:00
Column<PgDateTime> get deletedAt => customType(PgTypes.timestampWithTimezone)
.nullable()();
2025-12-13 20:55:50 +00:00
@override
Set<Column> get primaryKey => {id};
2025-12-13 13:27:05 +00:00
}
/// Таблица TestPackRelations - связь Tests с CardPacks (many-to-many)
class TestPackRelations extends Table {
2025-12-13 23:35:14 +00:00
TextColumn get testId => text()
.references(Tests, #id, onDelete: KeyAction.cascade)();
TextColumn get packId => text()
.references(CardPacks, #id, onDelete: KeyAction.cascade)();
2025-12-13 13:27:05 +00:00
@override
Set<Column> get primaryKey => {testId, packId};
}
/// Таблица TestStatistics - статистика прохождения тестов
class TestStatistics extends Table {
2025-12-13 23:35:14 +00:00
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
TextColumn get id => text()
.withDefault(const CustomExpression('gen_random_uuid()::text'))();
2025-12-13 20:55:50 +00:00
TextColumn get userId => text()(); // Reference to Users, but not FK to avoid circular deps
2025-12-13 23:35:14 +00:00
TextColumn get testId => text()
.references(Tests, #id, onDelete: KeyAction.cascade)();
2025-12-13 13:27:05 +00:00
// Результаты (JSON)
TextColumn get results => text()
.withDefault(const Constant('{}'))
.map(const JsonMapConverter())();
2025-12-13 23:35:14 +00:00
Column<PgDateTime> get completedAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
2025-12-13 13:27:05 +00:00
// Audit
2025-12-13 23:35:14 +00:00
Column<PgDateTime> get createdAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
Column<PgDateTime> get updatedAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
2025-12-13 20:55:50 +00:00
@override
Set<Column> get primaryKey => {id};
2025-12-13 13:27:05 +00:00
}
// Конвертеры импортированы из converters.dart