From 0bf32ae1ad9cb59d0402198058af813caa38144c Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Thu, 19 Mar 2026 15:27:03 +0100 Subject: [PATCH] refactor(database): remove unused table mapping and data classes - Deleted table `map` methods, data classes, and companions in schema verification code. - Updated migrations for more precise version checks. --- lib/core/database/database.dart | 2 +- .../household_keeper/generated/schema_v2.dart | 779 +----------------- 2 files changed, 10 insertions(+), 771 deletions(-) diff --git a/lib/core/database/database.dart b/lib/core/database/database.dart index bb356de..5ef3f8a 100644 --- a/lib/core/database/database.dart +++ b/lib/core/database/database.dart @@ -69,7 +69,7 @@ class AppDatabase extends _$AppDatabase { await m.createTable(tasks); await m.createTable(taskCompletions); } - if (from < 3) { + if (from == 2) { await m.addColumn(tasks, tasks.isActive); } }, diff --git a/test/drift/household_keeper/generated/schema_v2.dart b/test/drift/household_keeper/generated/schema_v2.dart index c65fd8d..1bfc63c 100644 --- a/test/drift/household_keeper/generated/schema_v2.dart +++ b/test/drift/household_keeper/generated/schema_v2.dart @@ -4,7 +4,7 @@ // import 'package:drift/drift.dart'; -class Rooms extends Table with TableInfo { +class Rooms extends Table with TableInfo { @override final GeneratedDatabase attachedDatabase; final String? _alias; @@ -67,30 +67,8 @@ class Rooms extends Table with TableInfo { @override Set get $primaryKey => {id}; @override - RoomsData map(Map data, {String? tablePrefix}) { - final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : ''; - return RoomsData( - id: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}id'], - )!, - name: attachedDatabase.typeMapping.read( - DriftSqlType.string, - data['${effectivePrefix}name'], - )!, - iconName: attachedDatabase.typeMapping.read( - DriftSqlType.string, - data['${effectivePrefix}icon_name'], - )!, - sortOrder: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}sort_order'], - )!, - createdAt: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}created_at'], - )!, - ); + Never map(Map data, {String? tablePrefix}) { + throw UnsupportedError('TableInfo.map in schema verification code'); } @override @@ -102,202 +80,7 @@ class Rooms extends Table with TableInfo { bool get dontWriteConstraints => true; } -class RoomsData extends DataClass implements Insertable { - final int id; - final String name; - final String iconName; - final int sortOrder; - final int createdAt; - const RoomsData({ - required this.id, - required this.name, - required this.iconName, - required this.sortOrder, - required this.createdAt, - }); - @override - Map toColumns(bool nullToAbsent) { - final map = {}; - map['id'] = Variable(id); - map['name'] = Variable(name); - map['icon_name'] = Variable(iconName); - map['sort_order'] = Variable(sortOrder); - map['created_at'] = Variable(createdAt); - return map; - } - - RoomsCompanion toCompanion(bool nullToAbsent) { - return RoomsCompanion( - id: Value(id), - name: Value(name), - iconName: Value(iconName), - sortOrder: Value(sortOrder), - createdAt: Value(createdAt), - ); - } - - factory RoomsData.fromJson( - Map json, { - ValueSerializer? serializer, - }) { - serializer ??= driftRuntimeOptions.defaultSerializer; - return RoomsData( - id: serializer.fromJson(json['id']), - name: serializer.fromJson(json['name']), - iconName: serializer.fromJson(json['iconName']), - sortOrder: serializer.fromJson(json['sortOrder']), - createdAt: serializer.fromJson(json['createdAt']), - ); - } - @override - Map toJson({ValueSerializer? serializer}) { - serializer ??= driftRuntimeOptions.defaultSerializer; - return { - 'id': serializer.toJson(id), - 'name': serializer.toJson(name), - 'iconName': serializer.toJson(iconName), - 'sortOrder': serializer.toJson(sortOrder), - 'createdAt': serializer.toJson(createdAt), - }; - } - - RoomsData copyWith({ - int? id, - String? name, - String? iconName, - int? sortOrder, - int? createdAt, - }) => RoomsData( - id: id ?? this.id, - name: name ?? this.name, - iconName: iconName ?? this.iconName, - sortOrder: sortOrder ?? this.sortOrder, - createdAt: createdAt ?? this.createdAt, - ); - RoomsData copyWithCompanion(RoomsCompanion data) { - return RoomsData( - id: data.id.present ? data.id.value : this.id, - name: data.name.present ? data.name.value : this.name, - iconName: data.iconName.present ? data.iconName.value : this.iconName, - sortOrder: data.sortOrder.present ? data.sortOrder.value : this.sortOrder, - createdAt: data.createdAt.present ? data.createdAt.value : this.createdAt, - ); - } - - @override - String toString() { - return (StringBuffer('RoomsData(') - ..write('id: $id, ') - ..write('name: $name, ') - ..write('iconName: $iconName, ') - ..write('sortOrder: $sortOrder, ') - ..write('createdAt: $createdAt') - ..write(')')) - .toString(); - } - - @override - int get hashCode => Object.hash(id, name, iconName, sortOrder, createdAt); - @override - bool operator ==(Object other) => - identical(this, other) || - (other is RoomsData && - other.id == this.id && - other.name == this.name && - other.iconName == this.iconName && - other.sortOrder == this.sortOrder && - other.createdAt == this.createdAt); -} - -class RoomsCompanion extends UpdateCompanion { - final Value id; - final Value name; - final Value iconName; - final Value sortOrder; - final Value createdAt; - const RoomsCompanion({ - this.id = const Value.absent(), - this.name = const Value.absent(), - this.iconName = const Value.absent(), - this.sortOrder = const Value.absent(), - this.createdAt = const Value.absent(), - }); - RoomsCompanion.insert({ - this.id = const Value.absent(), - required String name, - required String iconName, - this.sortOrder = const Value.absent(), - required int createdAt, - }) : name = Value(name), - iconName = Value(iconName), - createdAt = Value(createdAt); - static Insertable custom({ - Expression? id, - Expression? name, - Expression? iconName, - Expression? sortOrder, - Expression? createdAt, - }) { - return RawValuesInsertable({ - if (id != null) 'id': id, - if (name != null) 'name': name, - if (iconName != null) 'icon_name': iconName, - if (sortOrder != null) 'sort_order': sortOrder, - if (createdAt != null) 'created_at': createdAt, - }); - } - - RoomsCompanion copyWith({ - Value? id, - Value? name, - Value? iconName, - Value? sortOrder, - Value? createdAt, - }) { - return RoomsCompanion( - id: id ?? this.id, - name: name ?? this.name, - iconName: iconName ?? this.iconName, - sortOrder: sortOrder ?? this.sortOrder, - createdAt: createdAt ?? this.createdAt, - ); - } - - @override - Map toColumns(bool nullToAbsent) { - final map = {}; - if (id.present) { - map['id'] = Variable(id.value); - } - if (name.present) { - map['name'] = Variable(name.value); - } - if (iconName.present) { - map['icon_name'] = Variable(iconName.value); - } - if (sortOrder.present) { - map['sort_order'] = Variable(sortOrder.value); - } - if (createdAt.present) { - map['created_at'] = Variable(createdAt.value); - } - return map; - } - - @override - String toString() { - return (StringBuffer('RoomsCompanion(') - ..write('id: $id, ') - ..write('name: $name, ') - ..write('iconName: $iconName, ') - ..write('sortOrder: $sortOrder, ') - ..write('createdAt: $createdAt') - ..write(')')) - .toString(); - } -} - -class Tasks extends Table with TableInfo { +class Tasks extends Table with TableInfo { @override final GeneratedDatabase attachedDatabase; final String? _alias; @@ -405,50 +188,8 @@ class Tasks extends Table with TableInfo { @override Set get $primaryKey => {id}; @override - TasksData map(Map data, {String? tablePrefix}) { - final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : ''; - return TasksData( - id: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}id'], - )!, - roomId: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}room_id'], - )!, - name: attachedDatabase.typeMapping.read( - DriftSqlType.string, - data['${effectivePrefix}name'], - )!, - description: attachedDatabase.typeMapping.read( - DriftSqlType.string, - data['${effectivePrefix}description'], - ), - intervalType: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}interval_type'], - )!, - intervalDays: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}interval_days'], - )!, - anchorDay: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}anchor_day'], - ), - effortLevel: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}effort_level'], - )!, - nextDueDate: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}next_due_date'], - )!, - createdAt: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}created_at'], - )!, - ); + Never map(Map data, {String? tablePrefix}) { + throw UnsupportedError('TableInfo.map in schema verification code'); } @override @@ -460,345 +201,7 @@ class Tasks extends Table with TableInfo { bool get dontWriteConstraints => true; } -class TasksData extends DataClass implements Insertable { - final int id; - final int roomId; - final String name; - final String? description; - final int intervalType; - final int intervalDays; - final int? anchorDay; - final int effortLevel; - final int nextDueDate; - final int createdAt; - const TasksData({ - required this.id, - required this.roomId, - required this.name, - this.description, - required this.intervalType, - required this.intervalDays, - this.anchorDay, - required this.effortLevel, - required this.nextDueDate, - required this.createdAt, - }); - @override - Map toColumns(bool nullToAbsent) { - final map = {}; - map['id'] = Variable(id); - map['room_id'] = Variable(roomId); - map['name'] = Variable(name); - if (!nullToAbsent || description != null) { - map['description'] = Variable(description); - } - map['interval_type'] = Variable(intervalType); - map['interval_days'] = Variable(intervalDays); - if (!nullToAbsent || anchorDay != null) { - map['anchor_day'] = Variable(anchorDay); - } - map['effort_level'] = Variable(effortLevel); - map['next_due_date'] = Variable(nextDueDate); - map['created_at'] = Variable(createdAt); - return map; - } - - TasksCompanion toCompanion(bool nullToAbsent) { - return TasksCompanion( - id: Value(id), - roomId: Value(roomId), - name: Value(name), - description: description == null && nullToAbsent - ? const Value.absent() - : Value(description), - intervalType: Value(intervalType), - intervalDays: Value(intervalDays), - anchorDay: anchorDay == null && nullToAbsent - ? const Value.absent() - : Value(anchorDay), - effortLevel: Value(effortLevel), - nextDueDate: Value(nextDueDate), - createdAt: Value(createdAt), - ); - } - - factory TasksData.fromJson( - Map json, { - ValueSerializer? serializer, - }) { - serializer ??= driftRuntimeOptions.defaultSerializer; - return TasksData( - id: serializer.fromJson(json['id']), - roomId: serializer.fromJson(json['roomId']), - name: serializer.fromJson(json['name']), - description: serializer.fromJson(json['description']), - intervalType: serializer.fromJson(json['intervalType']), - intervalDays: serializer.fromJson(json['intervalDays']), - anchorDay: serializer.fromJson(json['anchorDay']), - effortLevel: serializer.fromJson(json['effortLevel']), - nextDueDate: serializer.fromJson(json['nextDueDate']), - createdAt: serializer.fromJson(json['createdAt']), - ); - } - @override - Map toJson({ValueSerializer? serializer}) { - serializer ??= driftRuntimeOptions.defaultSerializer; - return { - 'id': serializer.toJson(id), - 'roomId': serializer.toJson(roomId), - 'name': serializer.toJson(name), - 'description': serializer.toJson(description), - 'intervalType': serializer.toJson(intervalType), - 'intervalDays': serializer.toJson(intervalDays), - 'anchorDay': serializer.toJson(anchorDay), - 'effortLevel': serializer.toJson(effortLevel), - 'nextDueDate': serializer.toJson(nextDueDate), - 'createdAt': serializer.toJson(createdAt), - }; - } - - TasksData copyWith({ - int? id, - int? roomId, - String? name, - Value description = const Value.absent(), - int? intervalType, - int? intervalDays, - Value anchorDay = const Value.absent(), - int? effortLevel, - int? nextDueDate, - int? createdAt, - }) => TasksData( - id: id ?? this.id, - roomId: roomId ?? this.roomId, - name: name ?? this.name, - description: description.present ? description.value : this.description, - intervalType: intervalType ?? this.intervalType, - intervalDays: intervalDays ?? this.intervalDays, - anchorDay: anchorDay.present ? anchorDay.value : this.anchorDay, - effortLevel: effortLevel ?? this.effortLevel, - nextDueDate: nextDueDate ?? this.nextDueDate, - createdAt: createdAt ?? this.createdAt, - ); - TasksData copyWithCompanion(TasksCompanion data) { - return TasksData( - id: data.id.present ? data.id.value : this.id, - roomId: data.roomId.present ? data.roomId.value : this.roomId, - name: data.name.present ? data.name.value : this.name, - description: data.description.present - ? data.description.value - : this.description, - intervalType: data.intervalType.present - ? data.intervalType.value - : this.intervalType, - intervalDays: data.intervalDays.present - ? data.intervalDays.value - : this.intervalDays, - anchorDay: data.anchorDay.present ? data.anchorDay.value : this.anchorDay, - effortLevel: data.effortLevel.present - ? data.effortLevel.value - : this.effortLevel, - nextDueDate: data.nextDueDate.present - ? data.nextDueDate.value - : this.nextDueDate, - createdAt: data.createdAt.present ? data.createdAt.value : this.createdAt, - ); - } - - @override - String toString() { - return (StringBuffer('TasksData(') - ..write('id: $id, ') - ..write('roomId: $roomId, ') - ..write('name: $name, ') - ..write('description: $description, ') - ..write('intervalType: $intervalType, ') - ..write('intervalDays: $intervalDays, ') - ..write('anchorDay: $anchorDay, ') - ..write('effortLevel: $effortLevel, ') - ..write('nextDueDate: $nextDueDate, ') - ..write('createdAt: $createdAt') - ..write(')')) - .toString(); - } - - @override - int get hashCode => Object.hash( - id, - roomId, - name, - description, - intervalType, - intervalDays, - anchorDay, - effortLevel, - nextDueDate, - createdAt, - ); - @override - bool operator ==(Object other) => - identical(this, other) || - (other is TasksData && - other.id == this.id && - other.roomId == this.roomId && - other.name == this.name && - other.description == this.description && - other.intervalType == this.intervalType && - other.intervalDays == this.intervalDays && - other.anchorDay == this.anchorDay && - other.effortLevel == this.effortLevel && - other.nextDueDate == this.nextDueDate && - other.createdAt == this.createdAt); -} - -class TasksCompanion extends UpdateCompanion { - final Value id; - final Value roomId; - final Value name; - final Value description; - final Value intervalType; - final Value intervalDays; - final Value anchorDay; - final Value effortLevel; - final Value nextDueDate; - final Value createdAt; - const TasksCompanion({ - this.id = const Value.absent(), - this.roomId = const Value.absent(), - this.name = const Value.absent(), - this.description = const Value.absent(), - this.intervalType = const Value.absent(), - this.intervalDays = const Value.absent(), - this.anchorDay = const Value.absent(), - this.effortLevel = const Value.absent(), - this.nextDueDate = const Value.absent(), - this.createdAt = const Value.absent(), - }); - TasksCompanion.insert({ - this.id = const Value.absent(), - required int roomId, - required String name, - this.description = const Value.absent(), - required int intervalType, - this.intervalDays = const Value.absent(), - this.anchorDay = const Value.absent(), - required int effortLevel, - required int nextDueDate, - required int createdAt, - }) : roomId = Value(roomId), - name = Value(name), - intervalType = Value(intervalType), - effortLevel = Value(effortLevel), - nextDueDate = Value(nextDueDate), - createdAt = Value(createdAt); - static Insertable custom({ - Expression? id, - Expression? roomId, - Expression? name, - Expression? description, - Expression? intervalType, - Expression? intervalDays, - Expression? anchorDay, - Expression? effortLevel, - Expression? nextDueDate, - Expression? createdAt, - }) { - return RawValuesInsertable({ - if (id != null) 'id': id, - if (roomId != null) 'room_id': roomId, - if (name != null) 'name': name, - if (description != null) 'description': description, - if (intervalType != null) 'interval_type': intervalType, - if (intervalDays != null) 'interval_days': intervalDays, - if (anchorDay != null) 'anchor_day': anchorDay, - if (effortLevel != null) 'effort_level': effortLevel, - if (nextDueDate != null) 'next_due_date': nextDueDate, - if (createdAt != null) 'created_at': createdAt, - }); - } - - TasksCompanion copyWith({ - Value? id, - Value? roomId, - Value? name, - Value? description, - Value? intervalType, - Value? intervalDays, - Value? anchorDay, - Value? effortLevel, - Value? nextDueDate, - Value? createdAt, - }) { - return TasksCompanion( - id: id ?? this.id, - roomId: roomId ?? this.roomId, - name: name ?? this.name, - description: description ?? this.description, - intervalType: intervalType ?? this.intervalType, - intervalDays: intervalDays ?? this.intervalDays, - anchorDay: anchorDay ?? this.anchorDay, - effortLevel: effortLevel ?? this.effortLevel, - nextDueDate: nextDueDate ?? this.nextDueDate, - createdAt: createdAt ?? this.createdAt, - ); - } - - @override - Map toColumns(bool nullToAbsent) { - final map = {}; - if (id.present) { - map['id'] = Variable(id.value); - } - if (roomId.present) { - map['room_id'] = Variable(roomId.value); - } - if (name.present) { - map['name'] = Variable(name.value); - } - if (description.present) { - map['description'] = Variable(description.value); - } - if (intervalType.present) { - map['interval_type'] = Variable(intervalType.value); - } - if (intervalDays.present) { - map['interval_days'] = Variable(intervalDays.value); - } - if (anchorDay.present) { - map['anchor_day'] = Variable(anchorDay.value); - } - if (effortLevel.present) { - map['effort_level'] = Variable(effortLevel.value); - } - if (nextDueDate.present) { - map['next_due_date'] = Variable(nextDueDate.value); - } - if (createdAt.present) { - map['created_at'] = Variable(createdAt.value); - } - return map; - } - - @override - String toString() { - return (StringBuffer('TasksCompanion(') - ..write('id: $id, ') - ..write('roomId: $roomId, ') - ..write('name: $name, ') - ..write('description: $description, ') - ..write('intervalType: $intervalType, ') - ..write('intervalDays: $intervalDays, ') - ..write('anchorDay: $anchorDay, ') - ..write('effortLevel: $effortLevel, ') - ..write('nextDueDate: $nextDueDate, ') - ..write('createdAt: $createdAt') - ..write(')')) - .toString(); - } -} - -class TaskCompletions extends Table - with TableInfo { +class TaskCompletions extends Table with TableInfo { @override final GeneratedDatabase attachedDatabase; final String? _alias; @@ -838,22 +241,8 @@ class TaskCompletions extends Table @override Set get $primaryKey => {id}; @override - TaskCompletionsData map(Map data, {String? tablePrefix}) { - final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : ''; - return TaskCompletionsData( - id: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}id'], - )!, - taskId: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}task_id'], - )!, - completedAt: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}completed_at'], - )!, - ); + Never map(Map data, {String? tablePrefix}) { + throw UnsupportedError('TableInfo.map in schema verification code'); } @override @@ -865,156 +254,6 @@ class TaskCompletions extends Table bool get dontWriteConstraints => true; } -class TaskCompletionsData extends DataClass - implements Insertable { - final int id; - final int taskId; - final int completedAt; - const TaskCompletionsData({ - required this.id, - required this.taskId, - required this.completedAt, - }); - @override - Map toColumns(bool nullToAbsent) { - final map = {}; - map['id'] = Variable(id); - map['task_id'] = Variable(taskId); - map['completed_at'] = Variable(completedAt); - return map; - } - - TaskCompletionsCompanion toCompanion(bool nullToAbsent) { - return TaskCompletionsCompanion( - id: Value(id), - taskId: Value(taskId), - completedAt: Value(completedAt), - ); - } - - factory TaskCompletionsData.fromJson( - Map json, { - ValueSerializer? serializer, - }) { - serializer ??= driftRuntimeOptions.defaultSerializer; - return TaskCompletionsData( - id: serializer.fromJson(json['id']), - taskId: serializer.fromJson(json['taskId']), - completedAt: serializer.fromJson(json['completedAt']), - ); - } - @override - Map toJson({ValueSerializer? serializer}) { - serializer ??= driftRuntimeOptions.defaultSerializer; - return { - 'id': serializer.toJson(id), - 'taskId': serializer.toJson(taskId), - 'completedAt': serializer.toJson(completedAt), - }; - } - - TaskCompletionsData copyWith({int? id, int? taskId, int? completedAt}) => - TaskCompletionsData( - id: id ?? this.id, - taskId: taskId ?? this.taskId, - completedAt: completedAt ?? this.completedAt, - ); - TaskCompletionsData copyWithCompanion(TaskCompletionsCompanion data) { - return TaskCompletionsData( - id: data.id.present ? data.id.value : this.id, - taskId: data.taskId.present ? data.taskId.value : this.taskId, - completedAt: data.completedAt.present - ? data.completedAt.value - : this.completedAt, - ); - } - - @override - String toString() { - return (StringBuffer('TaskCompletionsData(') - ..write('id: $id, ') - ..write('taskId: $taskId, ') - ..write('completedAt: $completedAt') - ..write(')')) - .toString(); - } - - @override - int get hashCode => Object.hash(id, taskId, completedAt); - @override - bool operator ==(Object other) => - identical(this, other) || - (other is TaskCompletionsData && - other.id == this.id && - other.taskId == this.taskId && - other.completedAt == this.completedAt); -} - -class TaskCompletionsCompanion extends UpdateCompanion { - final Value id; - final Value taskId; - final Value completedAt; - const TaskCompletionsCompanion({ - this.id = const Value.absent(), - this.taskId = const Value.absent(), - this.completedAt = const Value.absent(), - }); - TaskCompletionsCompanion.insert({ - this.id = const Value.absent(), - required int taskId, - required int completedAt, - }) : taskId = Value(taskId), - completedAt = Value(completedAt); - static Insertable custom({ - Expression? id, - Expression? taskId, - Expression? completedAt, - }) { - return RawValuesInsertable({ - if (id != null) 'id': id, - if (taskId != null) 'task_id': taskId, - if (completedAt != null) 'completed_at': completedAt, - }); - } - - TaskCompletionsCompanion copyWith({ - Value? id, - Value? taskId, - Value? completedAt, - }) { - return TaskCompletionsCompanion( - id: id ?? this.id, - taskId: taskId ?? this.taskId, - completedAt: completedAt ?? this.completedAt, - ); - } - - @override - Map toColumns(bool nullToAbsent) { - final map = {}; - if (id.present) { - map['id'] = Variable(id.value); - } - if (taskId.present) { - map['task_id'] = Variable(taskId.value); - } - if (completedAt.present) { - map['completed_at'] = Variable(completedAt.value); - } - return map; - } - - @override - String toString() { - return (StringBuffer('TaskCompletionsCompanion(') - ..write('id: $id, ') - ..write('taskId: $taskId, ') - ..write('completedAt: $completedAt') - ..write(')')) - .toString(); - } -} - class DatabaseAtV2 extends GeneratedDatabase { DatabaseAtV2(QueryExecutor e) : super(e); late final Rooms rooms = Rooms(this);