2024-11-05 11:12:42 +00:00
// @ts-check
const DB _HANDLER = require ( "../package-shared/utils/backend/global-db/DB_HANDLER" ) ;
const fs = require ( "fs" ) ;
require ( "dotenv" ) . config ( { path : "./../.env" } ) ;
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
async function updateChildrenTablesOnDb ( ) {
/ * *
* Grab Schema
*
* @ description Grab Schema
* /
try {
2024-11-05 14:18:40 +00:00
const rootDir = String ( process . env . DSQL _USER _DB _SCHEMA _PATH ) ;
2024-11-05 11:12:42 +00:00
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 ( ) ;