Skip to content

Commit

Permalink
Added script to migrate from user ID based messages/tags to global rows
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisischris committed Sep 23, 2024
1 parent 2790247 commit 1632ab2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/internal.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 2 additions & 1 deletion src/internal/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './processScans'
export * from './processScans'
export * from './migrateMessagesAndTags'
33 changes: 33 additions & 0 deletions src/internal/migrateMessagesAndTags.ts
Original file line number Diff line number Diff line change
@@ -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;
}
32 changes: 14 additions & 18 deletions src/internal/processScans.ts
Original file line number Diff line number Diff line change
@@ -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`);
Expand Down Expand Up @@ -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
??
Expand All @@ -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],
Expand All @@ -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 }));
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from './getMode'
export * from './isStaging'
export * from './graphql'
export * from './sleep'
export * from './chunk'
export * from './chunk'
export * from './hashStringToUuid'

0 comments on commit 1632ab2

Please sign in to comment.