correced down query,

delete satte_balances table and restore it
This commit is contained in:
Ulf Gebhardt 2022-02-26 00:11:26 +01:00
parent e3c96463f1
commit 850be8a890
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9

View File

@ -37,11 +37,35 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
// rename column `dec_decay` to `decay`
await queryFn('ALTER TABLE `transactions` RENAME COLUMN `dec_decay` to `decay`;')
// Drop tables
// drop `state_balances`
await queryFn('DROP TABLE `state_balances`;')
}
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
// Not all data is recoverable here, this data is simulated,
// We lose all incorrect balances and wrongly rounded amounts.
await queryFn(`
CREATE TABLE \`state_balances\` (
\`id\` int(10) unsigned NOT NULL AUTO_INCREMENT,
\`state_user_id\` int(10) unsigned NOT NULL,
\`modified\` datetime NOT NULL,
\`record_date\` datetime DEFAULT NULL,
\`amount\` bigint(20) NOT NULL,
PRIMARY KEY (\`id\`)
) ENGINE=InnoDB AUTO_INCREMENT=568 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
`)
await queryFn(`
INSERT INTO \`state_balances\`
(state_user_id, modified, record_date, amount)
SELECT user_id as state_user_id, balance_date as modified, balance_date as record_date, amount * 10000 as amount FROM
(SELECT user_id as uid, MAX(balance_date) AS date FROM transactions GROUP BY uid) AS t
LEFT JOIN transactions ON t.uid = transactions.user_id AND t.date = transactions.balance_date;
`)
await queryFn('ALTER TABLE `transactions` RENAME COLUMN `decay` to `dec_decay`;')
await queryFn('ALTER TABLE `transactions` RENAME COLUMN `balance` to `dec_balance`;')
await queryFn('ALTER TABLE `transactions` RENAME COLUMN `amount` to `dec_amount`;')
@ -60,22 +84,22 @@ export async function downgrade(queryFn: (query: string, values?: any[]) => Prom
)
await queryFn('ALTER TABLE `transactions` ADD COLUMN `balance` bigint(20) DEFAULT 0 AFTER memo;')
await queryFn(
'ALTER TABLE `transactions` ADD COLUMN `send_sender_final_balance` bigint(20) DEFAULT NULL memo;',
'ALTER TABLE `transactions` ADD COLUMN `send_sender_final_balance` bigint(20) DEFAULT NULL AFTER memo;',
)
await queryFn(
'ALTER TABLE `transactions` ADD COLUMN `amount` bigint(20) DEFAULT NULL AFTER decay_start;',
)
await queryFn(`
UPDATE transaction SET
UPDATE transactions SET
temp_dec_diff_balance = 0,
temp_dec_old_balance = dec_balance,
temp_dec_diff_send_sender_final_balance = 0,
temp_dec_send_sender_final_balance = dec_balance
balance = dec_balance * 10000
send_sender_final_balance = dec_balance * 10000
amount = dec_amount * 10000
temp_dec_send_sender_final_balance = dec_balance,
balance = dec_balance * 10000,
send_sender_final_balance = dec_balance * 10000,
amount = dec_amount * 10000;
`)
await queryFn('ALTER TABLE `transactions` MODIFY COLUMN `amount` bigint(20) NOT NULL AFTER;')
await queryFn('ALTER TABLE `transactions` MODIFY COLUMN `amount` bigint(20) NOT NULL;')
}