PSA/server/migrations/20241111204243_migrate_document_associations.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

102 lines
3.0 KiB
JavaScript

exports.up = async function(knex) {
// Get all documents with existing associations
const documents = await knex('documents')
.select('document_id', 'tenant', 'ticket_id', 'company_id', 'contact_name_id', 'schedule_id')
.whereNotNull('ticket_id')
.orWhereNotNull('company_id')
.orWhereNotNull('contact_name_id')
.orWhereNotNull('schedule_id');
// Create associations for each document
const associations = [];
for (const doc of documents) {
if (doc.ticket_id) {
associations.push({
document_id: doc.document_id,
tenant: doc.tenant,
entity_id: doc.ticket_id,
entity_type: 'ticket'
});
}
if (doc.company_id) {
associations.push({
document_id: doc.document_id,
tenant: doc.tenant,
entity_id: doc.company_id,
entity_type: 'company'
});
}
if (doc.contact_name_id) {
associations.push({
document_id: doc.document_id,
tenant: doc.tenant,
entity_id: doc.contact_name_id,
entity_type: 'contact'
});
}
if (doc.schedule_id) {
associations.push({
document_id: doc.document_id,
tenant: doc.tenant,
entity_id: doc.schedule_id,
entity_type: 'schedule'
});
}
}
// Insert all associations
if (associations.length > 0) {
await knex('document_associations').insert(associations);
}
// Remove the old association columns from documents table
return knex.schema.alterTable('documents', function(table) {
table.dropColumn('ticket_id');
table.dropColumn('company_id');
table.dropColumn('contact_name_id');
table.dropColumn('schedule_id');
});
};
exports.down = async function(knex) {
// Add back the association columns
await knex.schema.alterTable('documents', function(table) {
table.uuid('ticket_id').references('ticket_id').inTable('tickets').onDelete('CASCADE');
table.uuid('company_id').references('company_id').inTable('companies').onDelete('CASCADE');
table.uuid('contact_name_id').references('contact_name_id').inTable('contact_names').onDelete('CASCADE');
table.uuid('schedule_id').references('schedule_id').inTable('schedules').onDelete('CASCADE');
});
// Get all associations
const associations = await knex('document_associations')
.select('document_id', 'tenant', 'entity_id', 'entity_type');
// Update documents with their associations
for (const assoc of associations) {
const update = {};
switch (assoc.entity_type) {
case 'ticket':
update.ticket_id = assoc.entity_id;
break;
case 'company':
update.company_id = assoc.entity_id;
break;
case 'contact':
update.contact_name_id = assoc.entity_id;
break;
case 'schedule':
update.schedule_id = assoc.entity_id;
break;
}
await knex('documents')
.where({
document_id: assoc.document_id,
tenant: assoc.tenant
})
.update(update);
}
// Drop the associations table
return knex.schema.dropTable('document_associations');
};