PSA/server/migrations/20250410154408_refactor_company_tax_rates_schema.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

72 lines
2.5 KiB
JavaScript

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
await knex.schema.alterTable('company_tax_rates', (table) => {
// Add is_default column
table
.boolean('is_default')
.notNullable()
.defaultTo(false)
.comment('Indicates if this is the default tax rate for the company');
// Add location_id column (nullable)
table
.uuid('location_id')
.nullable()
.comment('Optional location this tax rate applies to');
// Add foreign key constraint to company_locations
table
.foreign('location_id')
.references('location_id')
.inTable('company_locations')
.onDelete('SET NULL'); // Or CASCADE/RESTRICT depending on desired behavior
// Add index on location_id (including tenant for CitusDB)
table.index(['tenant', 'location_id'], 'idx_company_tax_rates_tenant_location_id');
});
// Add unique constraint for only one default rate per company/tenant
// Note: Partial unique indexes require raw SQL
await knex.raw(`
CREATE UNIQUE INDEX company_tax_rates_company_id_tenant_is_default_unique
ON company_tax_rates (company_id, tenant, is_default)
WHERE is_default = true;
`);
// NOTE: Dropping company_tax_settings.tax_rate_id is deferred to a later migration
// after the data migration populates company_tax_rates.is_default.
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
// NOTE: Re-adding company_tax_settings.tax_rate_id is handled in the down function
// of the later migration that drops it.
// Drop unique constraint on company_tax_rates
await knex.raw(`
DROP INDEX IF EXISTS company_tax_rates_company_id_tenant_is_default_unique;
`);
// Drop foreign key constraint for location_id using raw SQL *before* altering the table
// Adjust constraint name 'company_tax_rates_location_id_fkey' if it's different in your DB.
await knex.raw('ALTER TABLE company_tax_rates DROP CONSTRAINT IF EXISTS company_tax_rates_location_id_fkey;');
// Reverse changes on company_tax_rates
await knex.schema.alterTable('company_tax_rates', (table) => {
// Drop index on location_id
table.dropIndex(['tenant', 'location_id'], 'idx_company_tax_rates_tenant_location_id');
// Drop location_id column (FK constraint already dropped above)
table.dropColumn('location_id');
// Drop is_default column
table.dropColumn('is_default');
});
};