This commit is contained in:
2026-03-23 11:44:21 +03:00
parent 4f87aa7598
commit b19b7fac4f
11 changed files with 254 additions and 161 deletions
+2 -3
View File
@@ -13,7 +13,7 @@ class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openConnection());
@override
int get schemaVersion => 3;
int get schemaVersion => 4;
@override
MigrationStrategy get migration => MigrationStrategy(
@@ -23,11 +23,10 @@ class AppDatabase extends _$AppDatabase {
await customStatement(
'INSERT INTO accounts (name, is_main, currency, sort_order, created_at) '
'VALUES (?, ?, ?, ?, ?)',
['Main', 1, 'USD', 0, DateTime.now().millisecondsSinceEpoch],
['main', 1, 'USD', 0, DateTime.now().millisecondsSinceEpoch],
);
}
if (from == 2) {
// Add currency column to existing accounts table
await customStatement(
'ALTER TABLE accounts ADD COLUMN currency TEXT NOT NULL DEFAULT "USD"',
);
+68 -1
View File
@@ -113,6 +113,17 @@ class $TransactionsTable extends Transactions
requiredDuringInsert: false,
defaultValue: const Constant('USD'),
);
static const VerificationMeta _accountIdMeta = const VerificationMeta(
'accountId',
);
@override
late final GeneratedColumn<int> accountId = GeneratedColumn<int>(
'account_id',
aliasedName,
false,
type: DriftSqlType.int,
requiredDuringInsert: true,
);
static const VerificationMeta _createdAtMeta = const VerificationMeta(
'createdAt',
);
@@ -137,6 +148,7 @@ class $TransactionsTable extends Transactions
lastOccurrence,
currency,
currencyCode,
accountId,
createdAt,
];
@override
@@ -224,6 +236,14 @@ class $TransactionsTable extends Transactions
),
);
}
if (data.containsKey('account_id')) {
context.handle(
_accountIdMeta,
accountId.isAcceptableOrUnknown(data['account_id']!, _accountIdMeta),
);
} else if (isInserting) {
context.missing(_accountIdMeta);
}
if (data.containsKey('created_at')) {
context.handle(
_createdAtMeta,
@@ -279,6 +299,10 @@ class $TransactionsTable extends Transactions
DriftSqlType.string,
data['${effectivePrefix}currency_code'],
)!,
accountId: attachedDatabase.typeMapping.read(
DriftSqlType.int,
data['${effectivePrefix}account_id'],
)!,
createdAt: attachedDatabase.typeMapping.read(
DriftSqlType.dateTime,
data['${effectivePrefix}created_at'],
@@ -303,6 +327,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
final DateTime? lastOccurrence;
final String currency;
final String currencyCode;
final int accountId;
final DateTime createdAt;
const Transaction({
required this.id,
@@ -315,6 +340,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
this.lastOccurrence,
required this.currency,
required this.currencyCode,
required this.accountId,
required this.createdAt,
});
@override
@@ -334,6 +360,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
}
map['currency'] = Variable<String>(currency);
map['currency_code'] = Variable<String>(currencyCode);
map['account_id'] = Variable<int>(accountId);
map['created_at'] = Variable<DateTime>(createdAt);
return map;
}
@@ -352,6 +379,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
: Value(lastOccurrence),
currency: Value(currency),
currencyCode: Value(currencyCode),
accountId: Value(accountId),
createdAt: Value(createdAt),
);
}
@@ -372,6 +400,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
lastOccurrence: serializer.fromJson<DateTime?>(json['lastOccurrence']),
currency: serializer.fromJson<String>(json['currency']),
currencyCode: serializer.fromJson<String>(json['currencyCode']),
accountId: serializer.fromJson<int>(json['accountId']),
createdAt: serializer.fromJson<DateTime>(json['createdAt']),
);
}
@@ -389,6 +418,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
'lastOccurrence': serializer.toJson<DateTime?>(lastOccurrence),
'currency': serializer.toJson<String>(currency),
'currencyCode': serializer.toJson<String>(currencyCode),
'accountId': serializer.toJson<int>(accountId),
'createdAt': serializer.toJson<DateTime>(createdAt),
};
}
@@ -404,6 +434,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
Value<DateTime?> lastOccurrence = const Value.absent(),
String? currency,
String? currencyCode,
int? accountId,
DateTime? createdAt,
}) => Transaction(
id: id ?? this.id,
@@ -418,6 +449,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
: this.lastOccurrence,
currency: currency ?? this.currency,
currencyCode: currencyCode ?? this.currencyCode,
accountId: accountId ?? this.accountId,
createdAt: createdAt ?? this.createdAt,
);
Transaction copyWithCompanion(TransactionsCompanion data) {
@@ -438,6 +470,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
currencyCode: data.currencyCode.present
? data.currencyCode.value
: this.currencyCode,
accountId: data.accountId.present ? data.accountId.value : this.accountId,
createdAt: data.createdAt.present ? data.createdAt.value : this.createdAt,
);
}
@@ -455,6 +488,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
..write('lastOccurrence: $lastOccurrence, ')
..write('currency: $currency, ')
..write('currencyCode: $currencyCode, ')
..write('accountId: $accountId, ')
..write('createdAt: $createdAt')
..write(')'))
.toString();
@@ -472,6 +506,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
lastOccurrence,
currency,
currencyCode,
accountId,
createdAt,
);
@override
@@ -488,6 +523,7 @@ class Transaction extends DataClass implements Insertable<Transaction> {
other.lastOccurrence == this.lastOccurrence &&
other.currency == this.currency &&
other.currencyCode == this.currencyCode &&
other.accountId == this.accountId &&
other.createdAt == this.createdAt);
}
@@ -502,6 +538,7 @@ class TransactionsCompanion extends UpdateCompanion<Transaction> {
final Value<DateTime?> lastOccurrence;
final Value<String> currency;
final Value<String> currencyCode;
final Value<int> accountId;
final Value<DateTime> createdAt;
final Value<int> rowid;
const TransactionsCompanion({
@@ -515,6 +552,7 @@ class TransactionsCompanion extends UpdateCompanion<Transaction> {
this.lastOccurrence = const Value.absent(),
this.currency = const Value.absent(),
this.currencyCode = const Value.absent(),
this.accountId = const Value.absent(),
this.createdAt = const Value.absent(),
this.rowid = const Value.absent(),
});
@@ -529,13 +567,15 @@ class TransactionsCompanion extends UpdateCompanion<Transaction> {
this.lastOccurrence = const Value.absent(),
this.currency = const Value.absent(),
this.currencyCode = const Value.absent(),
required int accountId,
this.createdAt = const Value.absent(),
this.rowid = const Value.absent(),
}) : id = Value(id),
amount = Value(amount),
category = Value(category),
type = Value(type),
date = Value(date);
date = Value(date),
accountId = Value(accountId);
static Insertable<Transaction> custom({
Expression<String>? id,
Expression<double>? amount,
@@ -547,6 +587,7 @@ class TransactionsCompanion extends UpdateCompanion<Transaction> {
Expression<DateTime>? lastOccurrence,
Expression<String>? currency,
Expression<String>? currencyCode,
Expression<int>? accountId,
Expression<DateTime>? createdAt,
Expression<int>? rowid,
}) {
@@ -561,6 +602,7 @@ class TransactionsCompanion extends UpdateCompanion<Transaction> {
if (lastOccurrence != null) 'last_occurrence': lastOccurrence,
if (currency != null) 'currency': currency,
if (currencyCode != null) 'currency_code': currencyCode,
if (accountId != null) 'account_id': accountId,
if (createdAt != null) 'created_at': createdAt,
if (rowid != null) 'rowid': rowid,
});
@@ -577,6 +619,7 @@ class TransactionsCompanion extends UpdateCompanion<Transaction> {
Value<DateTime?>? lastOccurrence,
Value<String>? currency,
Value<String>? currencyCode,
Value<int>? accountId,
Value<DateTime>? createdAt,
Value<int>? rowid,
}) {
@@ -591,6 +634,7 @@ class TransactionsCompanion extends UpdateCompanion<Transaction> {
lastOccurrence: lastOccurrence ?? this.lastOccurrence,
currency: currency ?? this.currency,
currencyCode: currencyCode ?? this.currencyCode,
accountId: accountId ?? this.accountId,
createdAt: createdAt ?? this.createdAt,
rowid: rowid ?? this.rowid,
);
@@ -629,6 +673,9 @@ class TransactionsCompanion extends UpdateCompanion<Transaction> {
if (currencyCode.present) {
map['currency_code'] = Variable<String>(currencyCode.value);
}
if (accountId.present) {
map['account_id'] = Variable<int>(accountId.value);
}
if (createdAt.present) {
map['created_at'] = Variable<DateTime>(createdAt.value);
}
@@ -651,6 +698,7 @@ class TransactionsCompanion extends UpdateCompanion<Transaction> {
..write('lastOccurrence: $lastOccurrence, ')
..write('currency: $currency, ')
..write('currencyCode: $currencyCode, ')
..write('accountId: $accountId, ')
..write('createdAt: $createdAt, ')
..write('rowid: $rowid')
..write(')'))
@@ -2276,6 +2324,7 @@ typedef $$TransactionsTableCreateCompanionBuilder =
Value<DateTime?> lastOccurrence,
Value<String> currency,
Value<String> currencyCode,
required int accountId,
Value<DateTime> createdAt,
Value<int> rowid,
});
@@ -2291,6 +2340,7 @@ typedef $$TransactionsTableUpdateCompanionBuilder =
Value<DateTime?> lastOccurrence,
Value<String> currency,
Value<String> currencyCode,
Value<int> accountId,
Value<DateTime> createdAt,
Value<int> rowid,
});
@@ -2354,6 +2404,11 @@ class $$TransactionsTableFilterComposer
builder: (column) => ColumnFilters(column),
);
ColumnFilters<int> get accountId => $composableBuilder(
column: $table.accountId,
builder: (column) => ColumnFilters(column),
);
ColumnFilters<DateTime> get createdAt => $composableBuilder(
column: $table.createdAt,
builder: (column) => ColumnFilters(column),
@@ -2419,6 +2474,11 @@ class $$TransactionsTableOrderingComposer
builder: (column) => ColumnOrderings(column),
);
ColumnOrderings<int> get accountId => $composableBuilder(
column: $table.accountId,
builder: (column) => ColumnOrderings(column),
);
ColumnOrderings<DateTime> get createdAt => $composableBuilder(
column: $table.createdAt,
builder: (column) => ColumnOrderings(column),
@@ -2470,6 +2530,9 @@ class $$TransactionsTableAnnotationComposer
builder: (column) => column,
);
GeneratedColumn<int> get accountId =>
$composableBuilder(column: $table.accountId, builder: (column) => column);
GeneratedColumn<DateTime> get createdAt =>
$composableBuilder(column: $table.createdAt, builder: (column) => column);
}
@@ -2515,6 +2578,7 @@ class $$TransactionsTableTableManager
Value<DateTime?> lastOccurrence = const Value.absent(),
Value<String> currency = const Value.absent(),
Value<String> currencyCode = const Value.absent(),
Value<int> accountId = const Value.absent(),
Value<DateTime> createdAt = const Value.absent(),
Value<int> rowid = const Value.absent(),
}) => TransactionsCompanion(
@@ -2528,6 +2592,7 @@ class $$TransactionsTableTableManager
lastOccurrence: lastOccurrence,
currency: currency,
currencyCode: currencyCode,
accountId: accountId,
createdAt: createdAt,
rowid: rowid,
),
@@ -2543,6 +2608,7 @@ class $$TransactionsTableTableManager
Value<DateTime?> lastOccurrence = const Value.absent(),
Value<String> currency = const Value.absent(),
Value<String> currencyCode = const Value.absent(),
required int accountId,
Value<DateTime> createdAt = const Value.absent(),
Value<int> rowid = const Value.absent(),
}) => TransactionsCompanion.insert(
@@ -2556,6 +2622,7 @@ class $$TransactionsTableTableManager
lastOccurrence: lastOccurrence,
currency: currency,
currencyCode: currencyCode,
accountId: accountId,
createdAt: createdAt,
rowid: rowid,
),
+1
View File
@@ -12,6 +12,7 @@ class Transactions extends Table {
DateTimeColumn get lastOccurrence => dateTime().nullable()();
TextColumn get currency => text().withDefault(const Constant('\$'))();
TextColumn get currencyCode => text().withDefault(const Constant('USD'))();
IntColumn get accountId => integer()();
DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
@override
+3 -13
View File
@@ -64,11 +64,8 @@ class AccountRepository {
..orderBy([(a) => OrderingTerm.asc(a.sortOrder)]))
.get();
print('AccountRepository.getAll(): rows.length = ${rows.length}');
// Fallback: insert default account if none exists
if (rows.isEmpty) {
print('AccountRepository.getAll(): inserting default account');
try {
await _db.into(_db.accounts).insert(
AccountsCompanion.insert(
@@ -78,18 +75,16 @@ class AccountRepository {
sortOrder: const Value(0),
),
);
print('AccountRepository.getAll(): default account inserted');
} catch (e) {
print('AccountRepository.getAll(): insert error: $e');
// Ignore if already exists
}
// Re-query after insert
rows = await (_db.select(_db.accounts)
..orderBy([(a) => OrderingTerm.asc(a.sortOrder)]))
.get();
print('AccountRepository.getAll(): after insert, rows.length = ${rows.length}');
}
final accounts = rows
return rows
.map((row) => model.Account(
id: row.id,
name: row.name,
@@ -99,12 +94,7 @@ class AccountRepository {
createdAt: row.createdAt,
))
.toList();
print('AccountRepository.getAll(): returning ${accounts.length} accounts');
return accounts;
} catch (e, stack) {
print('AccountRepository.getAll(): error: $e');
print('Stack: $stack');
} catch (e) {
// Return empty list on error
return [];
}
@@ -148,6 +148,7 @@ class TransactionRepository {
lastOccurrence: dbTransaction.lastOccurrence as DateTime?,
currency: dbTransaction.currency as String,
currencyCode: dbTransaction.currencyCode as String,
accountId: dbTransaction.accountId as int,
);
}
@@ -165,6 +166,7 @@ class TransactionRepository {
lastOccurrence: Value(transaction.lastOccurrence),
currency: Value(transaction.currency),
currencyCode: Value(transaction.currencyCode),
accountId: Value(transaction.accountId),
createdAt: Value(DateTime.now()),
);
}
@@ -182,6 +184,7 @@ class TransactionRepository {
lastOccurrence: Value(transaction.lastOccurrence),
currency: Value(transaction.currency),
currencyCode: Value(transaction.currencyCode),
accountId: Value(transaction.accountId),
);
}