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
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz Source: /opt/alga-psa on psa.joliet.tech
121 lines
3.6 KiB
JavaScript
121 lines
3.6 KiB
JavaScript
/**
|
|
* Distribute users table
|
|
* Companies must be distributed first (users has FK to companies)
|
|
*/
|
|
const {
|
|
dropAndCaptureForeignKeys,
|
|
recreateForeignKeys
|
|
} = require('./utils/foreign_key_manager.cjs');
|
|
|
|
exports.config = { transaction: false };
|
|
|
|
exports.up = async function(knex) {
|
|
// Check if Citus is enabled
|
|
const citusEnabled = await knex.raw(`
|
|
SELECT EXISTS (
|
|
SELECT 1 FROM pg_extension WHERE extname = 'citus'
|
|
) as enabled
|
|
`);
|
|
|
|
if (!citusEnabled.rows[0].enabled) {
|
|
console.log('Citus not enabled, skipping table distribution');
|
|
return;
|
|
}
|
|
|
|
console.log('Distributing users table...');
|
|
|
|
try {
|
|
// Check if already distributed
|
|
const isDistributed = await knex.raw(`
|
|
SELECT EXISTS (
|
|
SELECT 1 FROM pg_dist_partition
|
|
WHERE logicalrelid = 'users'::regclass
|
|
) as distributed
|
|
`);
|
|
|
|
if (isDistributed.rows[0].distributed) {
|
|
console.log(' Users table already distributed');
|
|
return;
|
|
}
|
|
|
|
// Step 1: Capture and drop foreign key constraints
|
|
console.log(' Step 1: Capturing and dropping foreign key constraints...');
|
|
const capturedFKs = await dropAndCaptureForeignKeys(knex, 'users');
|
|
|
|
// Step 2: Drop unique constraints that don't include tenant
|
|
console.log(' Step 2: Dropping unique constraints...');
|
|
const uniqueConstraints = await knex.raw(`
|
|
SELECT conname
|
|
FROM pg_constraint
|
|
WHERE conrelid = 'users'::regclass
|
|
AND contype = 'u'
|
|
`);
|
|
|
|
for (const constraint of uniqueConstraints.rows) {
|
|
try {
|
|
await knex.raw(`ALTER TABLE users DROP CONSTRAINT ${constraint.conname}`);
|
|
console.log(` ✓ Dropped constraint: ${constraint.conname}`);
|
|
} catch (e) {
|
|
console.log(` - Could not drop ${constraint.conname}: ${e.message}`);
|
|
}
|
|
}
|
|
|
|
// Step 3: Distribute the table
|
|
console.log(' Step 3: Distributing users table...');
|
|
await knex.raw(`SELECT create_distributed_table('users', 'tenant', colocate_with => 'tenants')`);
|
|
console.log(' ✓ Distributed users table');
|
|
|
|
// Step 4: Recreate all valid foreign keys
|
|
console.log(' Step 4: Recreating foreign keys...');
|
|
await recreateForeignKeys(knex, 'users', capturedFKs);
|
|
|
|
// Step 5: Recreate unique constraint on email within tenant
|
|
console.log(' Step 5: Recreating unique constraints...');
|
|
try {
|
|
await knex.raw(`
|
|
ALTER TABLE users
|
|
ADD CONSTRAINT users_tenant_email_unique
|
|
UNIQUE (tenant, email)
|
|
`);
|
|
console.log(' ✓ Recreated unique constraint on (tenant, email)');
|
|
} catch (e) {
|
|
console.log(` - Could not recreate unique constraint: ${e.message}`);
|
|
}
|
|
|
|
console.log(' ✓ Users table distributed successfully');
|
|
|
|
} catch (error) {
|
|
console.error(` ✗ Failed to distribute users: ${error.message}`);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
exports.down = async function(knex) {
|
|
const citusEnabled = await knex.raw(`
|
|
SELECT EXISTS (
|
|
SELECT 1 FROM pg_extension WHERE extname = 'citus'
|
|
) as enabled
|
|
`);
|
|
|
|
if (!citusEnabled.rows[0].enabled) {
|
|
return;
|
|
}
|
|
|
|
console.log('Undistributing users table...');
|
|
|
|
try {
|
|
const isDistributed = await knex.raw(`
|
|
SELECT EXISTS (
|
|
SELECT 1 FROM pg_dist_partition
|
|
WHERE logicalrelid = 'users'::regclass
|
|
) as distributed
|
|
`);
|
|
|
|
if (isDistributed.rows[0].distributed) {
|
|
await knex.raw(`SELECT undistribute_table('users')`);
|
|
console.log(' ✓ Undistributed users table');
|
|
}
|
|
} catch (error) {
|
|
console.error(` ✗ Failed to undistribute users: ${error.message}`);
|
|
}
|
|
}; |