81 lines
3.4 KiB
JavaScript
81 lines
3.4 KiB
JavaScript
|
// @ts-check
|
||
|
|
||
|
const DB_HANDLER = require("../utils/backend/global-db/DB_HANDLER");
|
||
|
const fs = require("fs");
|
||
|
require("dotenv").config({ path: "./../.env" });
|
||
|
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
|
||
|
async function updateChildrenTablesOnDb() {
|
||
|
/**
|
||
|
* Grab Schema
|
||
|
*
|
||
|
* @description Grab Schema
|
||
|
*/
|
||
|
try {
|
||
|
const rootDir = String(process.env.DSQL_USER_DB_SCHEMA_PATH);
|
||
|
const userFolders = fs.readdirSync(rootDir);
|
||
|
|
||
|
for (let i = 0; i < userFolders.length; i++) {
|
||
|
const folder = userFolders[i];
|
||
|
const userId = folder.replace(/user-/, "");
|
||
|
const databases = JSON.parse(
|
||
|
fs.readFileSync(`${rootDir}/${folder}/main.json`, "utf-8")
|
||
|
);
|
||
|
|
||
|
for (let j = 0; j < databases.length; j++) {
|
||
|
const db = databases[j];
|
||
|
const dbTables = db.tables;
|
||
|
for (let k = 0; k < dbTables.length; k++) {
|
||
|
const table = dbTables[k];
|
||
|
|
||
|
if (table?.childTable) {
|
||
|
const originTableName = table.childTableName;
|
||
|
const originDbName = table.childTableDbFullName;
|
||
|
|
||
|
const WHERE_CLAUSE = `WHERE user_id='${userId}' AND db_slug='${db.dbSlug}' AND table_slug='${table.tableName}'`;
|
||
|
|
||
|
const existingTableInDb = await DB_HANDLER(
|
||
|
`SELECT * FROM user_database_tables ${WHERE_CLAUSE}`
|
||
|
);
|
||
|
|
||
|
if (existingTableInDb && existingTableInDb[0]) {
|
||
|
const updateChildrenTablesInfo = await DB_HANDLER(
|
||
|
`UPDATE user_database_tables SET child_table='1',child_table_parent_database='${originDbName}',child_table_parent_table='${originTableName}' WHERE id='${existingTableInDb[0].id}'`
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.log(error);
|
||
|
}
|
||
|
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
|
||
|
process.exit();
|
||
|
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
}
|
||
|
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
|
||
|
// const userArg = process.argv[process.argv.indexOf("--user")];
|
||
|
// const externalUser = process.argv[process.argv.indexOf("--user") + 1];
|
||
|
|
||
|
updateChildrenTablesOnDb();
|