PSA/server/migrations/20241112031330_create_asset_management_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

100 lines
4.8 KiB
JavaScript

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
return knex.schema
.createTable('asset_types', table => {
table.uuid('tenant').notNullable().references('tenant').inTable('tenants');
table.uuid('type_id').defaultTo(knex.raw('gen_random_uuid()')).notNullable();
table.text('type_name').notNullable();
table.uuid('parent_type_id');
table.jsonb('attributes_schema');
table.timestamp('created_at').defaultTo(knex.raw('CURRENT_TIMESTAMP'));
table.timestamp('updated_at').defaultTo(knex.raw('CURRENT_TIMESTAMP'));
table.primary(['tenant', 'type_id']);
table.foreign(['tenant', 'parent_type_id']).references(['tenant', 'type_id']).inTable('asset_types');
})
.createTable('assets', table => {
table.uuid('tenant').notNullable().references('tenant').inTable('tenants');
table.uuid('asset_id').defaultTo(knex.raw('gen_random_uuid()')).notNullable();
table.uuid('type_id').notNullable();
table.uuid('company_id').notNullable();
table.text('asset_tag').notNullable();
table.text('serial_number');
table.text('name').notNullable();
table.text('status').notNullable();
table.text('location');
table.timestamp('purchase_date');
table.timestamp('warranty_end_date');
table.jsonb('attributes');
table.timestamp('created_at').defaultTo(knex.raw('CURRENT_TIMESTAMP'));
table.timestamp('updated_at').defaultTo(knex.raw('CURRENT_TIMESTAMP'));
table.primary(['tenant', 'asset_id']);
table.foreign(['tenant', 'type_id']).references(['tenant', 'type_id']).inTable('asset_types');
table.foreign(['tenant', 'company_id']).references(['tenant', 'company_id']).inTable('companies');
})
.createTable('asset_history', table => {
table.uuid('tenant').notNullable().references('tenant').inTable('tenants');
table.uuid('history_id').defaultTo(knex.raw('gen_random_uuid()')).notNullable();
table.uuid('asset_id').notNullable();
table.uuid('changed_by').notNullable();
table.text('change_type').notNullable();
table.jsonb('changes').notNullable();
table.timestamp('changed_at').defaultTo(knex.raw('CURRENT_TIMESTAMP'));
table.primary(['tenant', 'history_id']);
table.foreign(['tenant', 'asset_id']).references(['tenant', 'asset_id']).inTable('assets');
table.foreign(['tenant', 'changed_by']).references(['tenant', 'user_id']).inTable('users');
})
.raw(`
-- Enable RLS for asset_types
ALTER TABLE asset_types ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation_policy ON asset_types
USING (tenant::TEXT = current_setting('app.current_tenant')::TEXT);
CREATE POLICY tenant_isolation_insert_policy ON asset_types
FOR INSERT WITH CHECK (tenant::TEXT = current_setting('app.current_tenant')::TEXT);
-- Enable RLS for assets
ALTER TABLE assets ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation_policy ON assets
USING (tenant::TEXT = current_setting('app.current_tenant')::TEXT);
CREATE POLICY tenant_isolation_insert_policy ON assets
FOR INSERT WITH CHECK (tenant::TEXT = current_setting('app.current_tenant')::TEXT);
-- Enable RLS for asset_history
ALTER TABLE asset_history ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation_policy ON asset_history
USING (tenant::TEXT = current_setting('app.current_tenant')::TEXT);
CREATE POLICY tenant_isolation_insert_policy ON asset_history
FOR INSERT WITH CHECK (tenant::TEXT = current_setting('app.current_tenant')::TEXT);
`);
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
return knex.schema
.raw(`
DROP POLICY IF EXISTS tenant_isolation_policy ON asset_history;
DROP POLICY IF EXISTS tenant_isolation_insert_policy ON asset_history;
DROP POLICY IF EXISTS tenant_isolation_policy ON assets;
DROP POLICY IF EXISTS tenant_isolation_insert_policy ON assets;
DROP POLICY IF EXISTS tenant_isolation_policy ON asset_types;
DROP POLICY IF EXISTS tenant_isolation_insert_policy ON asset_types;
`)
.dropTableIfExists('asset_history')
.dropTableIfExists('assets')
.dropTableIfExists('asset_types');
};