PSA/ee/server/migrations/citus/20250805000001_distribute_basic_tables.cjs
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

126 lines
3.6 KiB
JavaScript

/**
* Distribute basic tables that only depend on tenants
* Complex tables with circular dependencies will be handled later
*/
exports.config = { transaction: false };
exports.up = async function(knex) {
// Check if Citus is enabled
const citusEnabled = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_extension WHERE extname = 'citus'
) as enabled
`);
if (!citusEnabled.rows[0].enabled) {
console.log('Citus not enabled, skipping table distribution');
return;
}
console.log('Distributing basic tables that only depend on tenants...');
// Helper function to safely distribute a table
async function distributeTable(tableName, distributionColumn = 'tenant') {
try {
// Check if table exists
const tableExists = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = ?
) as exists
`, [tableName]);
if (!tableExists.rows[0].exists) {
console.log(` Table ${tableName} does not exist, skipping`);
return false;
}
// Check if already distributed
const isDistributed = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_dist_partition
WHERE logicalrelid = ?::regclass
) as distributed
`, [tableName]);
if (isDistributed.rows[0].distributed) {
console.log(` Table ${tableName} already distributed, skipping`);
return true;
}
// Distribute the table with colocation
await knex.raw(`SELECT create_distributed_table('${tableName}', '${distributionColumn}', colocate_with => 'tenants')`);
console.log(` ✓ Distributed table: ${tableName} on column: ${distributionColumn}`);
return true;
} catch (error) {
console.error(` ✗ Failed to distribute table ${tableName}: ${error.message}`);
// Don't throw - continue with other tables
return false;
}
}
// Distribute tables that only have FK to tenants (no other dependencies)
// Only include tables that exist and have no complex dependencies
const basicTables = [
'roles',
'permissions',
'role_permissions',
'tenant_settings'
// Removed credit tables as they depend on invoices
];
for (const table of basicTables) {
await distributeTable(table);
}
console.log('Basic tables distribution completed');
};
exports.down = async function(knex) {
// Check if Citus is enabled
const citusEnabled = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_extension WHERE extname = 'citus'
) as enabled
`);
if (!citusEnabled.rows[0].enabled) {
console.log('Citus not enabled, nothing to undo');
return;
}
console.log('Undistributing basic tables...');
async function undistributeTable(tableName) {
try {
const isDistributed = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_dist_partition
WHERE logicalrelid = ?::regclass
) as distributed
`, [tableName]);
if (isDistributed.rows[0].distributed) {
await knex.raw(`SELECT undistribute_table('${tableName}')`);
console.log(` ✓ Undistributed table: ${tableName}`);
}
return true;
} catch (error) {
console.error(` ✗ Failed to undistribute table ${tableName}: ${error.message}`);
return false;
}
}
// Undistribute in reverse order
const basicTables = [
'tenant_settings',
'role_permissions',
'permissions',
'roles'
];
for (const table of basicTables) {
await undistributeTable(table);
}
};