PSA/server/migrations/20250408220700_modify_companies_for_regions.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

77 lines
2.4 KiB
JavaScript

'use strict';
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function(knex) {
// Step 1: Add the new column, but keep the old one for data migration
await knex.schema.alterTable('companies', (table) => {
table.string('region_code', 255).nullable();
});
// Add the composite foreign key constraint using raw SQL
// References tax_regions(tenant, region_code)
// ON DELETE SET NULL because region_code is nullable in companies
await knex.raw(`
ALTER TABLE companies
ADD CONSTRAINT companies_tenant_region_code_fkey
FOREIGN KEY (tenant, region_code)
REFERENCES tax_regions (tenant, region_code)
ON DELETE SET NULL;
`);
// Step 2: Populate the new region_code column using the old tax_region
// Assumes tax_regions table is populated by the preceding migration.
await knex.raw(`
UPDATE companies c
SET region_code = tr.region_code
FROM tax_regions tr
WHERE c.tenant = tr.tenant
AND c.tax_region = tr.region_name
AND c.region_code IS NULL;
`);
// Step 3: Add index and drop the old column now that data is migrated
await knex.schema.alterTable('companies', (table) => {
// Add an index on the new column (including tenant) for performance
table.index(['tenant', 'region_code'], 'idx_companies_tenant_region_code');
// Drop the old tax_region column
table.dropColumn('tax_region');
});
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
// Drop the foreign key constraint first
await knex.raw(`
ALTER TABLE companies
DROP CONSTRAINT IF EXISTS companies_tenant_region_code_fkey;
`);
// Step 1 for Down: Add back old column, drop index
await knex.schema.alterTable('companies', (table) => {
// Add the old 'tax_region' column back
table.string('tax_region', 255).nullable();
// Drop the index
table.dropIndex(['tenant', 'region_code'], 'idx_companies_tenant_region_code');
});
// Step 2 for Down: Populate old column from new column before dropping it
await knex.raw(`
UPDATE companies c
SET tax_region = tr.region_name
FROM tax_regions tr
WHERE c.tenant = tr.tenant
AND c.region_code = tr.region_code
AND c.tax_region IS NULL;
`);
// Step 3 for Down: Drop the new column
await knex.schema.alterTable('companies', (table) => {
table.dropColumn('region_code');
});
};