letusersTableExists=execSync(`${postgresCmd} -AXqtc "SELECT count(*) FROM information_schema.tables WHERE table_schema LIKE 'public' AND table_type LIKE 'BASE TABLE' AND table_name = 'users'"`);
if(usersTableExists==0){
console.log("DB hasn't been initialised yet. Nothing to migrate.");
process.exit(0);
}
letldapProfiles=execSync(`${postgresCmd} -AXqtc "SELECT count(*) FROM users u JOIN accounts a ON a.id=u.account_id LEFT JOIN identities i ON i.uid=a.username WHERE i.uid IS NULL"`);
console.log(`LDAP profiles in DB: ${ldapProfiles}`);
if(ldapProfiles==0){
console.log("Nothing to migrate. All users' profiles are up-to-date.");
letuid=execSync(`${postgresCmd} -AXqtc "SELECT u.id FROM users u JOIN accounts a ON a.id=u.account_id WHERE NOT EXISTS (SELECT 1 FROM identities WHERE provider='openid_connect' AND uid='${users[id]}') AND a.username='${users[id]}'"`).toString().trim();