diff --git a/src/internal.ts b/src/internal.ts index 27faf21..87fadc4 100644 --- a/src/internal.ts +++ b/src/internal.ts @@ -1,7 +1,10 @@ -import { processScans } from '#src/internal/index'; +import { migrateMessagesAndTags, processScans } from '#src/internal/index'; export const internal = async (event) => { if (event.path.endsWith('/processScans')) { return processScans(event); } + else if (event.path.endsWith('/migrateMessagesAndTags')) { + return migrateMessagesAndTags(event); + } } diff --git a/src/internal/index.ts b/src/internal/index.ts index ac61cf2..07cadba 100644 --- a/src/internal/index.ts +++ b/src/internal/index.ts @@ -1 +1,2 @@ -export * from './processScans' \ No newline at end of file +export * from './processScans' +export * from './migrateMessagesAndTags' \ No newline at end of file diff --git a/src/internal/migrateMessagesAndTags.ts b/src/internal/migrateMessagesAndTags.ts new file mode 100644 index 0000000..534f8ad --- /dev/null +++ b/src/internal/migrateMessagesAndTags.ts @@ -0,0 +1,33 @@ +import { db, hashStringToUuid } from '#src/utils'; + +export const migrateMessagesAndTags = async () => { + await db.connect(); + // Make sure we have distinct message/tag rows + const distinctMessages = (await db.query(`SELECT DISTINCT "message", "type" FROM "messages"`)).rows; + for (const row of distinctMessages) { + const messageId = hashStringToUuid(row.message); + try { + await db.query({ + text: `INSERT INTO "messages" ("id","message","type") VALUES ($1, $2, $3)`, + values: [messageId, row.message, row.type] + }); + } + catch (err) { } + } + const distinctTags = (await db.query(`SELECT DISTINCT "tag" FROM "tags"`)).rows; + for (const row of distinctTags) { + const tagId = hashStringToUuid(row.tag); + try { + await db.query({ + text: `INSERT INTO "tags" ("id","tag") VALUES ($1, $2)`, + values: [tagId, row.tag], + }); + } + catch (err) { } + } + + // Now, find all old messages and adjust. + + await db.clean(); + return; +} \ No newline at end of file diff --git a/src/internal/processScans.ts b/src/internal/processScans.ts index 4450269..b6023ab 100644 --- a/src/internal/processScans.ts +++ b/src/internal/processScans.ts @@ -1,5 +1,4 @@ -import { chunk, db, isStaging, sleep } from '#src/utils'; -import { hashStringToUuid } from '#src/utils/hashStringToUuid'; +import { chunk, db, isStaging, sleep, hashStringToUuid } from '#src/utils'; export const processScans = async (event) => { console.log(`START PROCESS SCANS`); @@ -160,7 +159,7 @@ const scanProcessor = async ({ result, jobId, userId, propertyId }) => { const tagId = hashStringToUuid(row.tag); row.id = (await db.query({ - text: `SELECT "id" FROM "tags" WHERE "tag"=$1`, + text: `SELECT "id" FROM "tags" WHERE "id"=$1`, values: [tagId], })).rows?.[0]?.id ?? @@ -171,10 +170,11 @@ const scanProcessor = async ({ result, jobId, userId, propertyId }) => { } for (const row of result.messages) { const messageId = hashStringToUuid(row.message); - row.id = (await db.query({ + const existingMessageId = (await db.query({ text: `SELECT "id" FROM "messages" WHERE "id"=$1`, values: [messageId], - })).rows?.[0]?.id ?? + })).rows?.[0]?.id; + row.id = existingMessageId ?? (await db.query({ text: `INSERT INTO "messages" ("id", "message", "type") VALUES ($1, $2, $3) RETURNING "id"`, values: [messageId, row.message, row.type], @@ -198,21 +198,17 @@ const scanProcessor = async ({ result, jobId, userId, propertyId }) => { } } - for (const relatedTagId of row.relatedTagIds) { - try { - const messageTagExists = (await db.query({ - text: `SELECT "id" FROM "message_tags" WHERE "user_id"=$1 AND "message_id"=$2 AND "tag_id"=$3`, - values: [userId, row.id, result.nodes.find(obj => obj.nodeId === relatedTagId)?.id], - })).rows?.[0]?.id; - if (!messageTagExists) { + if (!existingMessageId) { + for (const relatedTagId of row.relatedTagIds) { + try { await db.query({ - text: `INSERT INTO "message_tags" ("user_id", "message_id", "tag_id") VALUES ($1, $2, $3)`, - values: [userId, row.id, result.tags.find(obj => obj.tagId === relatedTagId)?.id] - }) + text: `INSERT INTO "message_tags" ("message_id", "tag_id") VALUES ($1, $2)`, + values: [messageId, result.tags.find(obj => obj.tagId === relatedTagId)?.id] + }); + } + catch (err) { + console.log(err, `messageTag error`, JSON.stringify({ row })); } - } - catch (err) { - console.log(err, `messageTag error`, JSON.stringify({ row })); } } } diff --git a/src/utils/index.ts b/src/utils/index.ts index d156779..f13c46c 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -9,4 +9,5 @@ export * from './getMode' export * from './isStaging' export * from './graphql' export * from './sleep' -export * from './chunk' \ No newline at end of file +export * from './chunk' +export * from './hashStringToUuid' \ No newline at end of file