PSA/server/migrations/20250217202724_update_comment_columns.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

93 lines
3.2 KiB
JavaScript

exports.up = async function(knex) {
await knex.transaction(async (trx) => {
// Check if new column exists
const newColumnExists = await trx.raw(`
SELECT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'comments' AND column_name = 'author_type_new'
);
`);
if (!newColumnExists.rows[0].exists) {
// Add new column
await trx.raw('ALTER TABLE comments ADD COLUMN author_type_new comment_author_type_new');
// Copy data to new column
await trx.raw(`
UPDATE comments
SET author_type_new = subquery.new_author_type
FROM (
SELECT comment_id,
CASE
WHEN author_type::text = 'user' THEN 'internal'::comment_author_type_new
WHEN author_type::text = 'contact' THEN 'client'::comment_author_type_new
ELSE 'unknown'::comment_author_type_new
END AS new_author_type
FROM comments
) AS subquery
WHERE comments.comment_id = subquery.comment_id;
`);
}
// Check if old column exists before dropping
const oldColumnExists = await trx.raw(`
SELECT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'comments' AND column_name = 'author_type'
);
`);
if (oldColumnExists.rows[0].exists) {
// Drop old column and rename new one
await trx.raw('ALTER TABLE comments DROP COLUMN author_type');
await trx.raw('ALTER TABLE comments RENAME COLUMN author_type_new TO author_type');
// Set default on new column
await trx.raw("ALTER TABLE comments ALTER COLUMN author_type SET DEFAULT 'unknown'::comment_author_type_new");
}
});
};
exports.down = async function(knex) {
await knex.transaction(async (trx) => {
// Check if new column exists
const newColumnExists = await trx.raw(`
SELECT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'comments' AND column_name = 'author_type_old'
);
`);
if (!newColumnExists.rows[0].exists) {
// Add new column with old type
await trx.raw('ALTER TABLE comments ADD COLUMN author_type_old comment_author_type_old');
// Copy data back
await trx.raw(`
UPDATE comments
SET author_type_old = CASE
WHEN author_type::text = 'internal' THEN 'user'::comment_author_type_old
WHEN author_type::text = 'client' THEN 'contact'::comment_author_type_old
ELSE 'user'::comment_author_type_old
END`);
}
// Check if old column exists before dropping
const oldColumnExists = await trx.raw(`
SELECT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'comments' AND column_name = 'author_type'
);
`);
if (oldColumnExists.rows[0].exists) {
// Drop new column and rename old one
await trx.raw('ALTER TABLE comments DROP COLUMN author_type');
await trx.raw('ALTER TABLE comments RENAME COLUMN author_type_old TO author_type');
// Set default on old column
await trx.raw("ALTER TABLE comments ALTER COLUMN author_type SET DEFAULT 'user'::comment_author_type_old");
}
});
};