Identify and Apply Transfers Historically
These SQL scripts modify transactions as to apply transfers historically over migrated data without duplicating transactions. This is useful when you have migrated multiple accounts.
Before executing any actions, make sure you have a complete backup.
This process will only apply when the below conditions are met
-
The two transactions are related to different accounts
-
The amounts are exactly the same but inverted e.g.
-1.00and1.00 -
The transaction dates are within 3 days of each other
-
The match only occurs once. This means transfers of equal value following the pattern below will not be applied.
Account A->Account B->Account A/CAs we cannot reliably tell the order of the transfers.
How To
-
Create a second copy of the backup
-
Extract the backup
-
Open the
db.sqlitefile with your preferred tool SQLite3 cli, heidiSQL, etc. -
Run the below query to first view the impacted transactions
SELECT t.id,acct,a.name,amount,t.date,imported_description,t.description,t.transferred_id,(SELECT idFROM transactions sWHERE s.tombstone = 0AND s.id != t.idAND starting_balance_flag = 0AND s.amount = (t.amount * -1)AND s.acct != t.acctAND ((s.date >= t.dateAND s.date <= (t.date + 3))OR (s.date <= t.dateAND s.date >= (t.date -3)))) AS "transferred_id_new",(SELECT pa.idFROM transactions sLEFT JOIN payees pa ON s.acct = pa.transfer_acctWHERE s.tombstone = 0AND s.id != t.idAND starting_balance_flag = 0AND s.amount = (t.amount * -1)AND s.acct != t.acctAND ((s.date >= t.dateAND s.date <= (t.date + 3))OR (s.date <= t.dateAND s.date >= (t.date -3)))) AS "description_new"FROM transactions tLEFT JOIN accounts a ON t.acct = a.idLEFT JOIN payees p ON t.description = p.idLEFT JOIN accounts ta ON p.transfer_acct = ta.idWHERE t.tombstone = 0AND starting_balance_flag = 0AND (SELECT COUNT(*)FROM transactions sWHERE s.tombstone = 0AND s.id != t.idAND starting_balance_flag = 0AND s.amount = (t.amount * -1)AND s.acct != t.acctAND ((s.date >= t.dateAND s.date <= (t.date + 3))OR (s.date <= t.dateAND s.date >= (t.date -3)))) = 1ORDER BY DATE DESC; -
Run the below query to update the transactions
UPDATE transactionsSET transferred_id = (SELECT s.idFROM transactions sWHERE s.tombstone = 0AND s.id != transactions.idAND starting_balance_flag = 0AND s.amount = (transactions.amount * -1)AND s.acct != transactions.acctAND ((s.date >= transactions.dateAND s.date <= (transactions.date + 3))OR (s.date <= transactions.dateAND s.date >= (transactions.date -3)))),description = (SELECT pa.idFROM transactions sLEFT JOIN payees pa ON s.acct = pa.transfer_acctWHERE s.tombstone = 0AND s.id != transactions.idAND starting_balance_flag = 0AND s.amount = (transactions.amount * -1)AND s.acct != transactions.acctAND ((s.date >= transactions.dateAND s.date <= (transactions.date + 3))OR (s.date <= transactions.dateAND s.date >= (transactions.date -3))))WHERE tombstone = 0AND starting_balance_flag = 0AND (SELECT COUNT(*)FROM transactions sWHERE s.tombstone = 0AND s.id != transactions.idAND starting_balance_flag = 0AND s.amount = (transactions.amount * -1)AND s.acct != transactions.acctAND ((s.date >= transactions.dateAND s.date <= (transactions.date + 3))OR (s.date <= transactions.dateAND s.date >= (transactions.date -3)))) = 1; -
Zip the
db.sqlitefile with the originalmetadata.jsonfile -
Follow the restore process to apply these into your Actual Server instance
-
Verify your balances are correct and you see the correct transactions marked as transfers!