PSA/server/migrations/20250707120000_drop_deprecated_company_fields.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

86 lines
3.1 KiB
JavaScript

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function(knex) {
// Check if columns exist
const hasAddress = await knex.schema.hasColumn('companies', 'address');
const hasPhoneNo = await knex.schema.hasColumn('companies', 'phone_no');
const hasEmail = await knex.schema.hasColumn('companies', 'email');
// Only proceed if at least one column exists
if (hasAddress || hasPhoneNo || hasEmail) {
// First, migrate any remaining data to company_locations
// Loop through tenants to maintain CitusDB compatibility
const tenants = await knex('companies').distinct('tenant').pluck('tenant');
for (const tenant of tenants) {
// First, get companies that need migration for this tenant
const companiesNeedingMigration = await knex('companies')
.select('company_id', 'address', 'phone_no', 'email')
.where('tenant', tenant)
.andWhere(function() {
this.whereNotNull('address').andWhere('address', '!=', '')
.orWhereNotNull('phone_no').andWhere('phone_no', '!=', '')
.orWhereNotNull('email').andWhere('email', '!=', '');
});
// Then check which ones already have default locations
const existingDefaultLocations = await knex('company_locations')
.select('company_id')
.where('tenant', tenant)
.andWhere('is_default', true);
const existingCompanyIds = new Set(existingDefaultLocations.map(loc => loc.company_id));
// Filter out companies that already have default locations
const companiesToMigrate = companiesNeedingMigration.filter(
company => !existingCompanyIds.has(company.company_id)
);
// Now insert locations for companies that don't have them
for (const company of companiesToMigrate) {
await knex('company_locations').insert({
location_id: knex.raw('gen_random_uuid()'),
company_id: company.company_id,
tenant: tenant,
location_name: 'Main Location',
address_line1: company.address || 'N/A',
city: 'N/A',
state_province: null,
postal_code: null,
country_code: 'XX',
country_name: 'Unknown',
phone: company.phone_no,
email: company.email,
is_default: true,
is_billing_address: true,
is_shipping_address: true,
is_active: true,
created_at: knex.fn.now(),
updated_at: knex.fn.now()
});
}
}
// Now drop the deprecated columns
await knex.schema.alterTable('companies', function(table) {
if (hasAddress) table.dropColumn('address');
if (hasPhoneNo) table.dropColumn('phone_no');
if (hasEmail) table.dropColumn('email');
});
}
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
// Re-add the columns if rolling back
await knex.schema.alterTable('companies', function(table) {
table.string('address');
table.string('phone_no');
table.string('email');
});
};