This commit is contained in:
2026-03-23 12:17:04 +03:00
parent 598254556c
commit 8f03af4b0a
6 changed files with 164 additions and 38 deletions
+34 -1
View File
@@ -13,12 +13,15 @@ class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openConnection());
@override
int get schemaVersion => 4;
int get schemaVersion => 5;
@override
MigrationStrategy get migration => MigrationStrategy(
onUpgrade: (migrator, from, to) async {
print('--- DATABASE MIGRATION: from=$from to=$to ---');
if (from == 1) {
print('Migration: Creating accounts table');
await migrator.createTable(accounts);
await customStatement(
'INSERT INTO accounts (name, is_main, currency, sort_order, created_at) '
@@ -26,11 +29,41 @@ class AppDatabase extends _$AppDatabase {
['main', 1, 'USD', 0, DateTime.now().millisecondsSinceEpoch],
);
}
if (from == 2) {
print('Migration: Adding currency column to accounts');
await customStatement(
'ALTER TABLE accounts ADD COLUMN currency TEXT NOT NULL DEFAULT "USD"',
);
}
// Add account_id column to transactions if upgrading from version < 5
if (from < 5) {
print('Migration: Adding account_id column to transactions');
try {
// Check if column exists first
final result = await customSelect(
'PRAGMA table_info(transactions)',
).get();
final hasAccountId = result.any((row) => row.data['name'] == 'account_id');
if (!hasAccountId) {
print('Migration: account_id column does not exist, adding it now');
await customStatement(
'ALTER TABLE transactions ADD COLUMN account_id INTEGER NOT NULL DEFAULT 1',
);
print('Migration: account_id column added successfully');
} else {
print('Migration: account_id column already exists, skipping');
}
} catch (e) {
print('Migration: Error adding account_id column: $e');
// If the column already exists, this will fail, which is fine
}
}
print('--- DATABASE MIGRATION: COMPLETE ---');
},
);
+6 -8
View File
@@ -122,7 +122,8 @@ class $TransactionsTable extends Transactions
aliasedName,
false,
type: DriftSqlType.int,
requiredDuringInsert: true,
requiredDuringInsert: false,
defaultValue: const Constant(1),
);
static const VerificationMeta _createdAtMeta = const VerificationMeta(
'createdAt',
@@ -241,8 +242,6 @@ class $TransactionsTable extends Transactions
_accountIdMeta,
accountId.isAcceptableOrUnknown(data['account_id']!, _accountIdMeta),
);
} else if (isInserting) {
context.missing(_accountIdMeta);
}
if (data.containsKey('created_at')) {
context.handle(
@@ -567,15 +566,14 @@ class TransactionsCompanion extends UpdateCompanion<Transaction> {
this.lastOccurrence = const Value.absent(),
this.currency = const Value.absent(),
this.currencyCode = const Value.absent(),
required int accountId,
this.accountId = const Value.absent(),
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),
accountId = Value(accountId);
date = Value(date);
static Insertable<Transaction> custom({
Expression<String>? id,
Expression<double>? amount,
@@ -2324,7 +2322,7 @@ typedef $$TransactionsTableCreateCompanionBuilder =
Value<DateTime?> lastOccurrence,
Value<String> currency,
Value<String> currencyCode,
required int accountId,
Value<int> accountId,
Value<DateTime> createdAt,
Value<int> rowid,
});
@@ -2608,7 +2606,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<int> accountId = const Value.absent(),
Value<DateTime> createdAt = const Value.absent(),
Value<int> rowid = const Value.absent(),
}) => TransactionsCompanion.insert(
+1 -1
View File
@@ -12,7 +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()();
IntColumn get accountId => integer().withDefault(const Constant(1))();
DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
@override