sourcebot/packages/db/tools/scripts/migrate-duplicate-connections.ts
Michael Sukkarieh 3b36ffa17e
Add support for structured logs (#323)
* wip on refactoring docs

* wip

* initial structured logs impl

* structured log docs

* create logger package

* add news entry for structured logging

* add logger package to dockerfile and cleanup

* add gh workflow for catching broken links

* further wip

* fix

* further wip on docs

* review feedback

* remove logger dep from mcp package

* fix build errors

* add back auth_url warning

* fix sidebar title consistency

---------

Co-authored-by: bkellam <bshizzle1234@gmail.com>
2025-06-02 11:16:01 -07:00

55 lines
1.8 KiB
TypeScript

import { Script } from "../scriptRunner";
import { PrismaClient } from "../../dist";
import { confirmAction } from "../utils";
import { createLogger } from "@sourcebot/logger";
const logger = createLogger('migrate-duplicate-connections');
// Handles duplicate connections by renaming them to be unique.
// @see: 20250320215449_unique_connection_name_constraint_within_org
export const migrateDuplicateConnections: Script = {
run: async (prisma: PrismaClient) => {
// Find all duplicate connections based on name and orgId
const duplicates = (await prisma.connection.groupBy({
by: ['name', 'orgId'],
_count: {
_all: true,
},
})).filter(({ _count }) => _count._all > 1);
logger.info(`Found ${duplicates.reduce((acc, { _count }) => acc + _count._all, 0)} duplicate connections.`);
confirmAction();
let migrated = 0;
for (const duplicate of duplicates) {
const { name, orgId } = duplicate;
const connections = await prisma.connection.findMany({
where: {
name,
orgId,
},
orderBy: {
createdAt: 'asc',
},
});
for (let i = 0; i < connections.length; i++) {
const connection = connections[i];
const newName = `${name}-${i + 1}`;
logger.info(`Migrating connection with id ${connection.id} from name=${name} to name=${newName}`);
await prisma.connection.update({
where: { id: connection.id },
data: { name: newName },
});
migrated++;
}
}
logger.info(`Migrated ${migrated} connections.`);
},
};