PSA/server/migrations/20250323194900_create_plan_bundles_tables.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

128 lines
4.4 KiB
JavaScript

/**
* ⚠️ RENAMED: Tables in this file have been renamed.
* See 20251008000001_rename_billing_to_contracts.cjs for current names:
* - plan_bundles → contracts
* - company_plan_bundles → client_contracts
* - bundle_billing_plans → contract_line_mappings
*/
exports.up = function(knex) {
return knex.schema
// Create plan_bundles table
.createTable('plan_bundles', function(table) {
// Foreign key to tenants
table.uuid('tenant').notNullable();
table.foreign('tenant').references('tenant').inTable('tenants');
// Primary key
table.uuid('bundle_id').primary().defaultTo(knex.raw('gen_random_uuid()'));
// Bundle details
table.string('bundle_name', 255).notNullable();
table.text('bundle_description').nullable();
table.boolean('is_active').defaultTo(true);
// Timestamps
table.timestamp('created_at').defaultTo(knex.fn.now());
table.timestamp('updated_at').defaultTo(knex.fn.now());
// Constraints
table.unique(['tenant', 'bundle_id']);
// Indexes
table.index(['tenant']);
})
// Create bundle_billing_plans table
.createTable('bundle_billing_plans', function(table) {
// Foreign key to tenants
table.uuid('tenant').notNullable();
table.foreign('tenant').references('tenant').inTable('tenants');
// Foreign key to plan_bundles
table.uuid('bundle_id').notNullable();
table.foreign(['tenant', 'bundle_id']).references(['tenant', 'bundle_id']).inTable('plan_bundles').onDelete('CASCADE');
// Foreign key to billing_plans
table.uuid('plan_id').notNullable();
table.foreign(['tenant', 'plan_id']).references(['tenant', 'plan_id']).inTable('billing_plans').onDelete('CASCADE');
// Additional fields
table.integer('display_order').defaultTo(0);
// Timestamp
table.timestamp('created_at').defaultTo(knex.fn.now());
// Primary key
table.primary(['tenant', 'bundle_id', 'plan_id']);
// Indexes
table.index(['tenant']);
table.index(['bundle_id']);
table.index(['plan_id']);
})
// Create company_plan_bundles table
.createTable('company_plan_bundles', function(table) {
// Foreign key to tenants
table.uuid('tenant').notNullable();
table.foreign('tenant').references('tenant').inTable('tenants');
// Primary key
table.uuid('company_bundle_id').primary().defaultTo(knex.raw('gen_random_uuid()'));
// Foreign key to companies
table.uuid('company_id').notNullable();
table.foreign(['tenant', 'company_id']).references(['tenant', 'company_id']).inTable('companies').onDelete('CASCADE');
// Foreign key to plan_bundles
table.uuid('bundle_id').notNullable();
table.foreign(['tenant', 'bundle_id']).references(['tenant', 'bundle_id']).inTable('plan_bundles').onDelete('CASCADE');
// Bundle assignment details
table.timestamp('start_date').notNullable();
table.timestamp('end_date').nullable();
table.boolean('is_active').defaultTo(true);
// Timestamps
table.timestamp('created_at').defaultTo(knex.fn.now());
table.timestamp('updated_at').defaultTo(knex.fn.now());
// Constraints
table.unique(['tenant', 'company_bundle_id']);
// Indexes
table.index(['tenant']);
table.index(['company_id']);
table.index(['bundle_id']);
})
// Add company_bundle_id to company_billing_plans
.alterTable('company_billing_plans', function(table) {
// Add company_bundle_id column
table.uuid('company_bundle_id').nullable();
// Add foreign key constraint
table.foreign(['tenant', 'company_bundle_id'])
.references(['tenant', 'company_bundle_id'])
.inTable('company_plan_bundles')
.onDelete('SET NULL');
// Add index
table.index(['company_bundle_id']);
});
};
exports.down = function(knex) {
return knex.schema
// Remove company_bundle_id from company_billing_plans
.alterTable('company_billing_plans', function(table) {
table.dropForeign(['tenant', 'company_bundle_id']);
table.dropIndex(['company_bundle_id']);
table.dropColumn('company_bundle_id');
})
// Drop tables in reverse order
.dropTableIfExists('company_plan_bundles')
.dropTableIfExists('bundle_billing_plans')
.dropTableIfExists('plan_bundles');
};