Merge branch 'master' into semaphore

This commit is contained in:
Ulf Gebhardt 2022-12-15 12:41:59 +01:00 committed by GitHub
commit f741986b2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View File

@ -10,7 +10,7 @@ Decimal.set({
})
const constants = {
DB_VERSION: '0055-consistent_deleted_users',
DB_VERSION: '0056-consistent_transactions_table',
DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0
LOG4JS_CONFIG: 'log4js-config.json',
// default log level on production should be info

View File

@ -0,0 +1,39 @@
/* MIGRATION TO add users that have a transaction but do not exist */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { v4 as uuidv4 } from 'uuid'
import { OkPacket } from 'mysql'
export async function upgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
const missingUserIds = await queryFn(`
SELECT user_id FROM transactions
WHERE NOT EXISTS (SELECT id FROM users WHERE id = user_id) GROUP BY user_id;`)
for (let i = 0; i < missingUserIds.length; i++) {
let gradidoId = ''
let countIds: any[] = []
do {
gradidoId = uuidv4()
countIds = await queryFn(
`SELECT COUNT(*) FROM \`users\` WHERE \`gradido_id\` = "${gradidoId}"`,
)
} while (countIds[0] > 0)
const userContact = (await queryFn(`
INSERT INTO user_contacts
(type, user_id, email, email_checked, created_at, deleted_at)
VALUES
('EMAIL', ${missingUserIds[i].user_id}, 'deleted.user${missingUserIds[i].user_id}@gradido.net', 0, NOW(), NOW());`)) as unknown as OkPacket
await queryFn(`
INSERT INTO users
(id, gradido_id, email_id, first_name, last_name, deleted_at, password_encryption_type, created_at, language)
VALUES
(${missingUserIds[i].user_id}, '${gradidoId}', ${userContact.insertId}, 'DELETED', 'USER', NOW(), 0, NOW(), 'de');`)
}
}
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {}