PSA/server/migrations/20250613190110_add_location_to_tickets.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

124 lines
4.4 KiB
JavaScript

exports.up = async function(knex) {
// Add the location_id column
await knex.schema.table('tickets', function(table) {
table.uuid('location_id').nullable();
});
// Check if company_locations is a distributed table
let isDistributed = false;
let hasCitus = false;
try {
const citusCheck = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_extension WHERE extname = 'citus'
) as has_citus
`);
hasCitus = citusCheck.rows[0].has_citus;
if (hasCitus) {
try {
const result = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_dist_partition
WHERE logicalrelid = 'company_locations'::regclass
) as is_distributed
`);
isDistributed = result.rows[0].is_distributed;
} catch (error) {
console.log('Error checking distribution status:', error.message);
}
} else {
console.log('Citus extension not installed - proceeding with foreign key');
}
} catch (error) {
console.log('Error checking for Citus extension - proceeding with foreign key');
}
if (!isDistributed) {
console.log('company_locations is not a distributed table - adding foreign key constraint');
// Add both foreign key and index for non-distributed tables
await knex.schema.table('tickets', function(table) {
// For CitusDB compatibility, reference both location_id and tenant
// Note: We use RESTRICT instead of SET NULL because PostgreSQL doesn't support
// SET NULL on composite foreign keys where only one column should be nulled
table.foreign(['location_id', 'tenant']).references(['location_id', 'tenant']).inTable('company_locations').onDelete('RESTRICT');
table.index(['location_id', 'tenant'], 'idx_tickets_location_tenant');
});
} else {
console.log('company_locations is a distributed table - checking if tickets is also distributed');
// Check if tickets is also distributed
let ticketsDistributed = false;
try {
const ticketsResult = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_dist_partition
WHERE logicalrelid = 'tickets'::regclass
) as is_distributed
`);
ticketsDistributed = ticketsResult.rows[0].is_distributed;
} catch (error) {
// Ignore error
}
if (ticketsDistributed) {
console.log('Both tables are distributed - adding foreign key constraint');
// Both tables are distributed, can add foreign key
await knex.schema.table('tickets', function(table) {
table.foreign(['location_id', 'tenant']).references(['location_id', 'tenant']).inTable('company_locations').onDelete('RESTRICT');
table.index(['location_id', 'tenant'], 'idx_tickets_location_tenant');
});
} else {
console.log('Tables have incompatible distribution - skipping foreign key, adding index only');
// Incompatible distribution, skip foreign key
await knex.schema.table('tickets', function(table) {
table.index(['location_id', 'tenant'], 'idx_tickets_location_tenant');
});
}
}
};
exports.down = async function(knex) {
// We need to determine if we added a foreign key in the up migration
// This depends on whether both tables were distributed or not
let shouldDropForeignKey = true;
try {
// Check if company_locations is distributed
const locResult = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_dist_partition
WHERE logicalrelid = 'company_locations'::regclass
) as is_distributed
`);
if (locResult.rows[0].is_distributed) {
// If company_locations is distributed, check if tickets is too
const ticketsResult = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_dist_partition
WHERE logicalrelid = 'tickets'::regclass
) as is_distributed
`);
// Only skip foreign key drop if tables have incompatible distribution
if (!ticketsResult.rows[0].is_distributed) {
shouldDropForeignKey = false;
}
}
} catch (error) {
// pg_dist_partition doesn't exist, not using Citus
// In this case we would have added the foreign key
}
await knex.schema.table('tickets', function(table) {
table.dropIndex(['location_id', 'tenant'], 'idx_tickets_location_tenant');
if (shouldDropForeignKey) {
table.dropForeign(['location_id', 'tenant']);
}
table.dropColumn('location_id');
});
};