PSA/server/migrations/20260303100000_tenant_tier_system.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

64 lines
2.1 KiB
JavaScript

/**
* Tenant tier system migration:
* 1. Backfill all NULL/empty tenant plans to 'pro' (existing tenants grandfathered)
* 2. Create tenant_addons table for per-tenant add-on activations
* 3. Distribute tenant_addons on Citus if available
*
* @param {import('knex').Knex} knex
*/
exports.up = async function(knex) {
// Backfill: existing tenants with NULL or empty plan become 'pro'
await knex('tenants')
.whereNull('plan')
.orWhere('plan', '')
.update({ plan: 'pro' });
// Create tenant_addons table
const hasTable = await knex.schema.hasTable('tenant_addons');
if (!hasTable) {
await knex.schema.createTable('tenant_addons', (table) => {
table.uuid('tenant').notNullable();
table.text('addon_key').notNullable();
table.timestamp('activated_at', { useTz: true }).notNullable().defaultTo(knex.fn.now());
table.timestamp('expires_at', { useTz: true }).nullable();
table.jsonb('metadata').nullable();
table.primary(['tenant', 'addon_key']);
table.foreign('tenant').references('tenants.tenant');
});
}
// Distribute on Citus if available
const citusFn = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_proc
WHERE proname = 'create_distributed_table'
) AS exists;
`);
if (citusFn.rows?.[0]?.exists) {
const alreadyDistributed = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_dist_partition
WHERE logicalrelid = 'tenant_addons'::regclass
) AS is_distributed;
`);
if (!alreadyDistributed.rows?.[0]?.is_distributed) {
await knex.raw("SELECT create_distributed_table('tenant_addons', 'tenant')");
}
} else {
console.warn('[tenant_tier_system] Skipping create_distributed_table (function unavailable)');
}
};
/**
* @param {import('knex').Knex} knex
*/
exports.down = async function(knex) {
await knex.schema.dropTableIfExists('tenant_addons');
// Cannot rollback plan backfill - we don't know which tenants had NULL originally
console.log('Rollback: tenant_addons dropped. Plan backfill not reversed (intentional).');
};
exports.config = { transaction: false };