PSA/ee/server/migrations/citus/20250805000003_distribute_invoice_templates.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

89 lines
2.5 KiB
JavaScript

/**
* Distribute invoice_templates table
* This must happen before companies since companies has FK to invoice_templates
*/
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 invoice_templates table...');
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 = 'invoice_templates'
) as exists
`);
if (!tableExists.rows[0].exists) {
console.log(' invoice_templates table does not exist, skipping');
return;
}
// Check if already distributed
const isDistributed = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_dist_partition
WHERE logicalrelid = 'invoice_templates'::regclass
) as distributed
`);
if (isDistributed.rows[0].distributed) {
console.log(' invoice_templates table already distributed');
return;
}
// Distribute the table
await knex.raw(`SELECT create_distributed_table('invoice_templates', 'tenant', colocate_with => 'tenants')`);
console.log(' ✓ Distributed invoice_templates table');
} catch (error) {
console.error(` ✗ Failed to distribute invoice_templates: ${error.message}`);
throw error;
}
};
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) {
return;
}
console.log('Undistributing invoice_templates table...');
try {
const isDistributed = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_dist_partition
WHERE logicalrelid = 'invoice_templates'::regclass
) as distributed
`);
if (isDistributed.rows[0].distributed) {
await knex.raw(`SELECT undistribute_table('invoice_templates')`);
console.log(' ✓ Undistributed invoice_templates table');
}
} catch (error) {
console.error(` ✗ Failed to undistribute invoice_templates: ${error.message}`);
}
};