From 5bb6fd048de04dd740d94a5fc65d115685e617f1 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 28 Sep 2022 22:15:21 +0200 Subject: [PATCH] get email optin in a better way --- database/migrations/0049-add_user_contacts_table.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/database/migrations/0049-add_user_contacts_table.ts b/database/migrations/0049-add_user_contacts_table.ts index 0a6d4c89f..964285420 100644 --- a/database/migrations/0049-add_user_contacts_table.ts +++ b/database/migrations/0049-add_user_contacts_table.ts @@ -44,7 +44,10 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis INSERT INTO \`user_contacts\` (type, user_id, email, email_verification_code, email_opt_in_type_id, email_resend_count, email_checked, created_at, updated_at, deleted_at) SELECT 'EMAIL', \`users\`.id, \`users\`.email, optin.\`verification_code\`, optin.\`email_opt_in_type_id\`, optin.\`resend_count\`, users.\`email_checked\`, users.created, null, users.deletedAt - FROM users LEFT JOIN (SELECT * FROM \`login_email_opt_in\` ORDER BY created DESC LIMIT 1) AS optin ON users.id = optin.\`user_id\`;`) + FROM users LEFT JOIN + (SELECT le.id, le.user_id, le.verification_code, le.email_opt_in_type_id, le.resend_count, le.created, le.updated, + ROW_NUMBER() OVER (PARTITION BY le.user_id ORDER BY le.created DESC) AS row_num + FROM login_email_opt_in as le) AS optin ON users.id = optin.\`user_id\` AND row_num = 1;`) // insert in users table the email_id of the new created email-contacts const contacts = await queryFn(`SELECT c.id, c.user_id FROM user_contacts as c`)