mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
tested and fixed
This commit is contained in:
parent
ac18a11256
commit
88e789c53e
@ -11,28 +11,46 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
|
||||
]
|
||||
const creationDate = new Date('2020-03-30 06:59:55')
|
||||
|
||||
queryFn(`UPDATE 'transactions' set amount = 1000, memo = ? WHERE id = 150`, [transactionMemos[0]])
|
||||
queryFn(
|
||||
`INSERT INTO 'transactions'(
|
||||
user_id, previous, type_id, amount, balance, balance_date, memo, creation_date
|
||||
await queryFn(`UPDATE \`transactions\` set \`amount\` = 1000, \`balance\` = 1000, \`memo\` = ? WHERE \`id\` = 150`, [
|
||||
transactionMemos[0],
|
||||
])
|
||||
|
||||
// [ RowDataPacket { 'MAX(`id`)': 6828 } ]
|
||||
const lastTransactionId = (await queryFn(`SELECT MAX(\`id\`) as max_id from \`transactions\``))[0].max_id
|
||||
// dummy id to insert two transactions before this (previous has an index on it)
|
||||
await queryFn(`UPDATE \`transactions\` set \`previous\` = ? WHERE \`id\` = 278`, [lastTransactionId + 30])
|
||||
|
||||
await queryFn(
|
||||
`INSERT INTO \`transactions\`(
|
||||
\`user_id\`, \`previous\`, \`type_id\`, \`amount\`, \`balance\`,
|
||||
\`balance_date\`, \`decay\`, \`memo\`, \`creation_date\`
|
||||
) VALUES(
|
||||
275, 150, 1, 1000, 2000, ?, ?, ?
|
||||
275, 150, 1, 1000, 2000,
|
||||
?, 0, ?, ?
|
||||
)`,
|
||||
[creationDate, transactionMemos[1], creationDate],
|
||||
)
|
||||
queryFn(
|
||||
`INSERT INTO 'transactions'(
|
||||
user_id, previous, type_id, amount, balance, balance_date, memo, creation_date
|
||||
await queryFn(
|
||||
`INSERT INTO \`transactions\`(
|
||||
\`user_id\`, \`previous\`, \`type_id\`, \`amount\`, \`balance\`,
|
||||
\`balance_date\`, \`decay\`, \`memo\`, \`creation_date\`
|
||||
) VALUES(
|
||||
275, LAST_INSERT_ID(), 1, 1000, 3000, ?, ?, ?
|
||||
275, LAST_INSERT_ID(), 1, 1000, 3000,
|
||||
?, 0, ?, ?
|
||||
)`,
|
||||
[creationDate, transactionMemos[2], creationDate],
|
||||
[creationDate, transactionMemos[2], creationDate],
|
||||
)
|
||||
await queryFn(`UPDATE \`transactions\` set \`previous\` = LAST_INSERT_ID() WHERE \`id\` = 278`)
|
||||
|
||||
// ----------------------------------------------------------------------------------------------
|
||||
// update creation_date for transactions with creation_date == balance_date
|
||||
// !cannot made be undone easily!
|
||||
|
||||
// make copy of transaction_creations table for easy debugging workflow
|
||||
await queryFn(`DROP TABLE IF EXISTS \`transactions_temp\``)
|
||||
await queryFn(`CREATE TABLE \`transactions_temp\` LIKE transactions`)
|
||||
await queryFn(`INSERT INTO \`transactions_temp\` SELECT * FROM \`transactions\``)
|
||||
|
||||
// update entries from which memo contain month name (most cases)
|
||||
interface ReplaceSet {
|
||||
monthName: string
|
||||
@ -40,30 +58,30 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
|
||||
yearValue: number
|
||||
}
|
||||
const replaceSets: ReplaceSet[] = [
|
||||
{ monthName: 'Dez', monthValue: 12, yearValue: 2019 },
|
||||
{ monthName: 'Jan', monthValue: 1, yearValue: 2020 },
|
||||
{ monthName: 'Feb', monthValue: 2, yearValue: 2020 },
|
||||
{ monthName: 'M_rz', monthValue: 3, yearValue: 2020 },
|
||||
{ monthName: 'April', monthValue: 4, yearValue: 2020 },
|
||||
{ monthName: '%Dez%', monthValue: 12, yearValue: 2019 },
|
||||
{ monthName: '%Jan%', monthValue: 1, yearValue: 2020 },
|
||||
{ monthName: '%Feb%', monthValue: 2, yearValue: 2020 },
|
||||
{ monthName: '%M_rz%', monthValue: 3, yearValue: 2020 },
|
||||
{ monthName: '%April%', monthValue: 4, yearValue: 2020 },
|
||||
]
|
||||
replaceSets.forEach((replaceSet) => {
|
||||
let sqlQuery = `update 'transactions'
|
||||
SET 'creation_date' = DATE_FORMAT('creation_date', CONCAT(?, '-', ?, '-', `
|
||||
if (replaceSet.monthName === 'Feb') {
|
||||
sqlQuery += "IF(DATE_FORMAT(creation_date, '%d') <= 28, '%d', 28)"
|
||||
for(const replaceSet of replaceSets) {
|
||||
let sqlQuery = `update \`transactions_temp\`
|
||||
SET \`creation_date\` = DATE_FORMAT(\`creation_date\`, CONCAT(?, '-', ?, '-', `
|
||||
if (replaceSet.monthName === '%Feb%') {
|
||||
sqlQuery += "IF(DATE_FORMAT(`creation_date`, '%d') <= 28, '%d', 28)"
|
||||
} else {
|
||||
sqlQuery += "'%d'"
|
||||
}
|
||||
sqlQuery += `, ' %H:%i:%s'))
|
||||
WHERE balance_date = creation_date
|
||||
AND type_id = 1
|
||||
AND memo LIKE '%?%'`
|
||||
WHERE \`balance_date\` = \`creation_date\`
|
||||
AND \`type_id\` = 1
|
||||
AND \`memo\` LIKE ?`
|
||||
|
||||
queryFn(sqlQuery, [replaceSet.yearValue, replaceSet.monthValue, replaceSet.monthName])
|
||||
})
|
||||
await queryFn(sqlQuery, [replaceSet.yearValue, replaceSet.monthValue, replaceSet.monthName])
|
||||
}
|
||||
|
||||
// update entries without month name in memo, simply move creation_date 1 month before balance_date
|
||||
queryFn(`UPDATE 'transactions'
|
||||
await queryFn(`UPDATE \`transactions_temp\`
|
||||
set creation_date = CAST(DATE_FORMAT(creation_date, CONCAT(
|
||||
IF(DATE_FORMAT(creation_date, '%m') = 1, DATE_FORMAT(creation_date, '%Y') - 1, '%Y'),
|
||||
'-',
|
||||
@ -71,17 +89,24 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
|
||||
'-',
|
||||
IF(DATE_FORMAT(creation_date, '%m') = 3, IF(DATE_FORMAT(creation_date, '%d') <= 28, '%d', 28), '%d'),
|
||||
' %H:%i:%s')) AS DATETIME)
|
||||
WHERE balance_date = creation_date
|
||||
WHERE balance_date = creation_date
|
||||
`)
|
||||
}
|
||||
|
||||
|
||||
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
|
||||
|
||||
// remove added transaction
|
||||
queryFn(
|
||||
"DELETE FROM 'transactions' WHERE 'user_id' = 275 AND 'balance' IN(2000, 3000) AND 'amount' = 1000",
|
||||
await queryFn(
|
||||
`DELETE FROM \`transactions\`
|
||||
WHERE \`user_id\` = 275 AND \`balance\` IN (2000, 3000) AND \`amount\` = 1000`,
|
||||
)
|
||||
|
||||
// rewind transaction to split
|
||||
queryFn(
|
||||
`UPDATE 'transactions' set amount = 3000, memo = 'Aktives Grundeinkommen für GL. Dez, Jan, Feb' WHERE id = 150`,
|
||||
await queryFn(
|
||||
`UPDATE \`transactions\` set \`amount\` = 3000, \`memo\` = 'Aktives Grundeinkommen für GL. Dez, Jan, Feb'
|
||||
WHERE \`id\` = 150`,
|
||||
)
|
||||
|
||||
await queryFn(`UPDATE \`transactions\` set \`previous\` = 150 WHERE \`id\` = 278`)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user