Skip to content

Commit

Permalink
Add revert script
Browse files Browse the repository at this point in the history
  • Loading branch information
tdraier committed Dec 13, 2024
1 parent e498e16 commit ed8478d
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions connectors/migrations/20241212_restore_gdrive_parents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as fs from "fs";
import * as readline from "readline";
import { makeScript } from "scripts/helpers";

import { dataSourceConfigFromConnector } from "@connectors/lib/api/data_source_config";
import { updateDocumentParentsField } from "@connectors/lib/data_sources";
import logger from "@connectors/logger/logger";
import { ConnectorModel } from "@connectors/resources/storage/models/connector_model";

interface LogEntry {
msg: string;
documentId: string;
parents: string[];
previousParents: string[];
}

async function processLogFile(
connector: ConnectorModel,
filePath: string,
execute: boolean
) {
const fileStream = fs.createReadStream(filePath);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});

const dataSourceConfig = dataSourceConfigFromConnector(connector);

for await (const line of rl) {
const entry = JSON.parse(line) as LogEntry;
const { msg, documentId, previousParents } = entry;
if (
msg === "Update parents for document" &&
documentId &&
previousParents
) {
logger.info(
{ documentId, previousParents },
"Restoring parent for document"
);
if (execute) {
await updateDocumentParentsField({
dataSourceConfig,
documentId: documentId,
parents: previousParents,
});
}
}
}
}

makeScript(
{
connectorId: { type: "number", required: true },
file: { type: "string", required: true },
},
async ({ connectorId, file, execute }) => {
const connector = await ConnectorModel.findByPk(connectorId);
if (connector) {
await processLogFile(connector, file, execute);
}
}
);

0 comments on commit ed8478d

Please sign in to comment.