PSA/ee/server/migrations/20251216120000_create_custom_reports.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.8 KiB
JavaScript

/**
* Creates the custom_reports table for storing platform-wide and tenant-specific report definitions.
* Reports use the existing ReportDefinition shape from server/src/lib/reports/core/types.ts.
*
* Platform reports (cross-tenant) use MASTER_BILLING_TENANT_ID as the tenant value.
* Future tenant-specific reports will use the tenant's own ID.
*/
exports.up = async function up(knex) {
await knex.schema.createTable('custom_reports', (table) => {
table.uuid('tenant').notNullable(); // MASTER_BILLING_TENANT_ID for platform reports
table.uuid('report_id').notNullable().defaultTo(knex.raw('gen_random_uuid()'));
table.string('name', 255).notNullable();
table.text('description').nullable();
table.string('category', 100).nullable(); // 'tenants', 'users', 'billing', 'activity'
// Uses existing ReportDefinition shape from server/src/lib/reports/core/types.ts
table.jsonb('report_definition').notNullable();
// Access control - less transparent naming for security
table.boolean('platform_access').notNullable().defaultTo(false);
// Display configuration (column widths, sorting, etc.)
table.jsonb('display_config').nullable();
// Metadata
table.uuid('created_by').nullable();
table.timestamp('created_at').notNullable().defaultTo(knex.fn.now());
table.timestamp('updated_at').notNullable().defaultTo(knex.fn.now());
table.boolean('is_active').notNullable().defaultTo(true);
// Single composite primary key including tenant for Citus distribution
table.primary(['tenant', 'report_id'], {
constraintName: 'custom_reports_pk',
});
});
// Citus distribution with colocation (matches existing table pattern)
const citusEnabled = await knex.raw(`
SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'citus') as enabled
`);
if (citusEnabled.rows?.[0]?.enabled) {
// Check if already distributed (idempotent)
const isDistributed = await knex.raw(`
SELECT EXISTS (
SELECT 1 FROM pg_dist_partition
WHERE logicalrelid = 'custom_reports'::regclass
) as distributed
`);
if (!isDistributed.rows?.[0]?.distributed) {
// Colocate with tenants table for efficient cross-table queries
await knex.raw(`
SELECT create_distributed_table('custom_reports', 'tenant', colocate_with => 'tenants')
`);
}
}
// Indexes for common queries
await knex.schema.raw(`
CREATE INDEX IF NOT EXISTS custom_reports_tenant_category_idx
ON custom_reports (tenant, category) WHERE is_active = true;
`);
await knex.schema.raw(`
CREATE INDEX IF NOT EXISTS custom_reports_tenant_name_idx
ON custom_reports (tenant, name);
`);
};
exports.down = async function down(knex) {
await knex.schema.dropTableIfExists('custom_reports');
};
exports.config = { transaction: false }; // Required for Citus DDL