PSA/server/migrations/20260513120000_create_app_search_index.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

94 lines
2.6 KiB
JavaScript

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function(knex) {
await knex.raw('CREATE EXTENSION IF NOT EXISTS pg_trgm');
await knex.raw(`
CREATE TABLE app_search_index (
tenant uuid NOT NULL,
object_type text NOT NULL,
object_id text NOT NULL,
parent_type text,
parent_id text,
title text NOT NULL,
subtitle text,
body text,
url text NOT NULL,
metadata jsonb NOT NULL DEFAULT '{}'::jsonb,
visible_to_user_ids uuid[] NOT NULL DEFAULT '{}'::uuid[],
visible_to_roles text[] NOT NULL DEFAULT '{}'::text[],
is_internal_only boolean NOT NULL DEFAULT false,
is_private boolean NOT NULL DEFAULT false,
client_scope_id uuid,
required_permission text,
search_vector tsvector NOT NULL,
search_lang text NOT NULL DEFAULT 'english',
source_updated_at timestamptz NOT NULL,
indexed_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (tenant, object_type, object_id)
)
`);
const citusEnabled = await knex.raw(`
SELECT EXISTS (
SELECT 1
FROM pg_extension
WHERE extname = 'citus'
) AS enabled;
`);
if (citusEnabled.rows?.[0]?.enabled) {
const alreadyDistributed = await knex.raw(`
SELECT EXISTS (
SELECT 1
FROM pg_dist_partition
WHERE logicalrelid = 'app_search_index'::regclass
) AS is_distributed;
`);
if (!alreadyDistributed.rows?.[0]?.is_distributed) {
await knex.raw("SELECT create_distributed_table('app_search_index', 'tenant')");
}
} else {
console.warn('[create_app_search_index] Skipping create_distributed_table (Citus extension unavailable)');
}
await knex.raw(`
CREATE INDEX app_search_index_vector_gin
ON app_search_index USING gin (search_vector)
`);
await knex.raw(`
CREATE INDEX app_search_index_title_trgm
ON app_search_index USING gin (title gin_trgm_ops)
`);
await knex.raw(`
CREATE INDEX app_search_index_subtitle_trgm
ON app_search_index USING gin (subtitle gin_trgm_ops)
`);
await knex.raw(`
CREATE INDEX app_search_index_recent
ON app_search_index (tenant, source_updated_at DESC)
`);
await knex.raw(`
CREATE INDEX app_search_index_type
ON app_search_index (tenant, object_type)
`);
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
await knex.schema.dropTableIfExists('app_search_index');
};
// create_distributed_table cannot run inside a transaction block.
exports.config = { transaction: false };