Skip to content

Commit

Permalink
Migration for data_sources core table
Browse files Browse the repository at this point in the history
  • Loading branch information
spolu committed Nov 16, 2023
1 parent 8c28032 commit de4afc8
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions front/migrations/20231115_update_core_data_source_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Sequelize } from "sequelize";

// To be run from connectors with `CORE_DATABASE_URI`
const { CORE_DATABASE_URI, LIVE = false } = process.env;

async function main() {
const core_sequelize = new Sequelize(CORE_DATABASE_URI as string, {
logging: false,
});

const dataSourcesData = await core_sequelize.query(
`SELECT id, config_json FROM data_sources`
);

const dataSources = dataSourcesData[0] as {
id: number;
config_json: string;
}[];

console.log(`Found ${dataSources.length} data sources to process`);

const chunks = [];
for (let i = 0; i < dataSources.length; i += 32) {
chunks.push(dataSources.slice(i, i + 32));
}

for (let i = 0; i < chunks.length; i++) {
console.log(`Processing chunk ${i}/${chunks.length}...`);
const chunk = chunks[i];
await Promise.all(
chunk.map(async (d) => {
return processDataSource(core_sequelize, d);
})
);
}
}

async function processDataSource(
core_sequelize: Sequelize,
d: { id: number; config_json: string }
) {
console.log(">", d.config_json);

const config = JSON.parse(d.config_json);

delete config.use_cache;
config.qdrant_config = null;

console.log("<", JSON.stringify(config));

if (LIVE) {
await core_sequelize.query(
`UPDATE data_sources SET config_json = :config WHERE id = :id`,
{
replacements: {
id: d.id,
config: JSON.stringify(config),
},
}
);
}
}

main()
.then(() => {
console.log("Done");
process.exit(0);
})
.catch((err) => {
console.error(err);
process.exit(1);
});

0 comments on commit de4afc8

Please sign in to comment.