Files
upstream-twenty/packages/twenty-server/test/integration/graphql/suites/upgrade/make-user-email-case-insensitive.integration-spec.ts
upstream-mirror e7da97194a
Some checks failed
App Prod-Parity E2E Dispatch / dispatch (push) Failing after 10s
CD Storybook UI / build (push) Failing after 3m25s
CD Storybook UI / dispatch (push) Has been skipped
CD deploy main / website-changed-files (push) Successful in 18m8s
CI Agent Skills / changed-files-check (push) Successful in 18m9s
CI Create App E2E minimal / changed-files-check (push) Successful in 24s
CI Create App / changed-files-check (push) Successful in 26s
CI Docs / changed-files-check (push) Successful in 26s
CI Emails / changed-files-check (push) Successful in 28s
CI Example App Hello World / changed-files-check (push) Successful in 23s
CI Front / changed-files-check (push) Has been skipped
CI Example App Postcard / changed-files-check (push) Successful in 27s
CI UI / changed-files-check (push) Has been skipped
CI Utils Test / changed-files-check (push) Successful in 45s
CI Twenty Apps / discover (push) Successful in 1m3s
CD deploy main / deploy-main (push) Failing after 3s
CI Agent Skills / agent-skills-validate (push) Has been skipped
CI Create App / create-app-test (lint) (push) Has been skipped
CI Emails / emails-test (push) Has been skipped
CI Front / front-task (lingui:compile) (push) Has been skipped
CI Front / front-task (lint) (push) Has been skipped
CI Example App Postcard / example-app-postcard (push) Has been skipped
CI UI / ui-task (lint) (push) Has been skipped
CI UI / ui-task (pack-size) (push) Has been skipped
CI UI / ui-task (size) (push) Has been skipped
CI UI / ui-task (test) (push) Has been skipped
CI UI / ui-task (typecheck) (push) Has been skipped
CI Utils Test / utils-test (push) Has been skipped
CI Twenty Apps / ${{ matrix.app.name }} (push) Has been skipped
CI Emails / ci-emails-status-check (push) Successful in 2s
CI Example App Hello World / ci-example-app-hello-world-status-check (push) Successful in 2s
CI Example App Postcard / ci-example-app-postcard-status-check (push) Successful in 3s
CI UI / ui-sb-test (push) Has been skipped
CI Utils Test / ci-utils-test-status-check (push) Successful in 2s
CI Twenty Apps / ci-twenty-apps-status-check (push) Successful in 2s
CI UI / ci-ui-status-check (push) Successful in 2s
CI Desktop / desktop (push) Failing after 1m8s
Push docs to Crowdin / Push documentation to Crowdin (push) Failing after 55s
Push translations to Crowdin / Extract and upload translations (push) Failing after 5s
Push website translations to Crowdin / Extract and upload website translations (push) Failing after 5s
CI Create App / create-app-test (test) (push) Has been skipped
CI Create App / create-app-test (typecheck) (push) Has been skipped
CI Docs / docs-lint (push) Has been skipped
CI Example App Hello World / example-app-hello-world (push) Has been skipped
CI Front / front-task (lingui:extract) (push) Has been skipped
CI Front / front-task (test) (push) Has been skipped
CI Front / front-task (typecheck) (push) Has been skipped
CI UI / ui-sb-build (push) Failing after 1m32s
CI Twenty Apps / ${{ matrix.app.name }} (${{ matrix.server-source }}) (push) Has been skipped
CI Agent Skills / agent-skills-publish (push) Has been skipped
CI Create App / ci-create-app-status-check (push) Successful in 2s
CI Agent Skills / agent-skills-remote-source (push) Has been skipped
CI Create App E2E minimal / create-app-e2e-minimal (push) Has been cancelled
CI E2E Main / e2e-test (push) Has been cancelled
CI Front / front-sb-build (push) Has been cancelled
CI Front / front-build (push) Has been cancelled
CI Create App E2E minimal / ci-create-app-e2e-minimal-status-check (push) Has been cancelled
CI E2E Main / ci-e2e-main-status-check (push) Has been cancelled
CI E2E Main / notify-main-ci-failure (push) Has been cancelled
CI E2E Main / qa-scout-comment (push) Has been cancelled
CI Front / front-sb-test (push) Has been cancelled
CI Front / front-sb-screenshots (push) Has been cancelled
CI Front / ci-front-status-check (push) Has been cancelled
DPA Sub-Processor Sync / sync-subprocessors (push) Failing after 3s
AI Catalog Sync / sync-catalog (push) Failing after 2s
snapshot of https://github.com/twentyhq/twenty.git (main, 2026-09-17; blobs > 131072B omitted)
2026-09-17 23:28:13 +05:00

214 lines
7.0 KiB
TypeScript

import { type QueryRunner } from 'typeorm';
import { MakeUserEmailCaseInsensitiveFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-37/2-37-instance-command-fast-1787836741000-make-user-email-case-insensitive';
const PROBE_PREFIX = 'upgrade-probe';
const columnType = async (): Promise<string> => {
const [row] = await global.testDataSource.query(
`SELECT udt_name FROM information_schema.columns
WHERE table_schema = 'core' AND table_name = 'user' AND column_name = 'email'`,
);
return row.udt_name;
};
const uniqueIndexDefinition = async (): Promise<string | undefined> => {
const [row] = await global.testDataSource.query(
`SELECT indexdef FROM pg_indexes
WHERE schemaname = 'core' AND tablename = 'user' AND indexname = 'UQ_USER_EMAIL'`,
);
return row?.indexdef;
};
const forceVarcharBaseline = async (): Promise<void> => {
await global.testDataSource.query(
`DROP INDEX IF EXISTS core."UQ_USER_EMAIL"`,
);
await global.testDataSource.query(
`ALTER TABLE core."user" ALTER COLUMN email TYPE character varying USING email::character varying`,
);
await global.testDataSource.query(
`CREATE UNIQUE INDEX "UQ_USER_EMAIL" ON core."user" (email) WHERE "deletedAt" IS NULL`,
);
};
const insertUser = async ({
email,
deletedAt = null,
}: {
email: string;
deletedAt?: string | null;
}): Promise<void> => {
await global.testDataSource.query(
`INSERT INTO core."user" ("firstName", "lastName", "email", "deletedAt")
VALUES ('probe', 'user', $1, $2)`,
[email, deletedAt],
);
};
const deleteProbeUsers = async (): Promise<void> => {
await global.testDataSource.query(
`DELETE FROM core."user" WHERE email ILIKE $1`,
[`${PROBE_PREFIX}%`],
);
};
// Mirrors instance-command-runner.service.ts: up() runs inside a transaction
// that is rolled back if the command throws.
const runCommandLikeUpgradeRunner = async (
direction: 'up' | 'down',
): Promise<{ threw: boolean; message?: string }> => {
const command = new MakeUserEmailCaseInsensitiveFastInstanceCommand();
const queryRunner: QueryRunner = global.testDataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
await command[direction](queryRunner);
await queryRunner.commitTransaction();
return { threw: false };
} catch (error) {
if (queryRunner.isTransactionActive) {
await queryRunner.rollbackTransaction();
}
return { threw: true, message: (error as Error).message };
} finally {
await queryRunner.release();
}
};
describe('MakeUserEmailCaseInsensitive instance command (integration)', () => {
beforeEach(async () => {
await deleteProbeUsers();
await forceVarcharBaseline();
});
afterAll(async () => {
await deleteProbeUsers();
await forceVarcharBaseline();
await runCommandLikeUpgradeRunner('up');
});
it('converts the column and keeps the unique index on a clean database', async () => {
expect(await columnType()).toBe('varchar');
const result = await runCommandLikeUpgradeRunner('up');
expect(result.threw).toBe(false);
expect(await columnType()).toBe('citext');
expect(await uniqueIndexDefinition()).toContain('UNIQUE INDEX');
expect(await uniqueIndexDefinition()).toContain('"deletedAt" IS NULL');
});
it('skips without failing the upgrade when live addresses differ only by casing', async () => {
await insertUser({ email: `${PROBE_PREFIX}-clash@example.com` });
await insertUser({ email: `${PROBE_PREFIX}-CLASH@example.com` });
const before = await uniqueIndexDefinition();
const result = await runCommandLikeUpgradeRunner('up');
expect(result.threw).toBe(false);
expect(await columnType()).toBe('varchar');
expect(await uniqueIndexDefinition()).toBe(before);
});
it('converts on a later run once the colliding addresses are gone', async () => {
await insertUser({ email: `${PROBE_PREFIX}-retry@example.com` });
await insertUser({ email: `${PROBE_PREFIX}-RETRY@example.com` });
expect((await runCommandLikeUpgradeRunner('up')).threw).toBe(false);
expect(await columnType()).toBe('varchar');
await global.testDataSource.query(
`DELETE FROM core."user" WHERE email = $1`,
[`${PROBE_PREFIX}-RETRY@example.com`],
);
expect((await runCommandLikeUpgradeRunner('up')).threw).toBe(false);
expect(await columnType()).toBe('citext');
});
it('ignores soft-deleted addresses that collide with a live one', async () => {
await insertUser({ email: `${PROBE_PREFIX}-soft@example.com` });
await insertUser({
email: `${PROBE_PREFIX}-SOFT@example.com`,
deletedAt: new Date().toISOString(),
});
const result = await runCommandLikeUpgradeRunner('up');
expect(result.threw).toBe(false);
expect(await columnType()).toBe('citext');
});
it('restores the original column type and index on down', async () => {
await runCommandLikeUpgradeRunner('up');
expect(await columnType()).toBe('citext');
const result = await runCommandLikeUpgradeRunner('down');
expect(result.threw).toBe(false);
expect(await columnType()).toBe('varchar');
expect(await uniqueIndexDefinition()).toContain('UNIQUE INDEX');
});
it('survives a full up down up cycle', async () => {
expect((await runCommandLikeUpgradeRunner('up')).threw).toBe(false);
expect((await runCommandLikeUpgradeRunner('down')).threw).toBe(false);
expect((await runCommandLikeUpgradeRunner('up')).threw).toBe(false);
expect(await columnType()).toBe('citext');
expect(await uniqueIndexDefinition()).toContain('UNIQUE INDEX');
});
it('still converts and rebuilds the index when it is missing beforehand', async () => {
await global.testDataSource.query(
`DROP INDEX IF EXISTS core."UQ_USER_EMAIL"`,
);
const result = await runCommandLikeUpgradeRunner('up');
expect(result.threw).toBe(false);
expect(await columnType()).toBe('citext');
expect(await uniqueIndexDefinition()).toContain('UNIQUE INDEX');
});
it('enforces uniqueness across casing once converted', async () => {
await insertUser({ email: `${PROBE_PREFIX}-unique@example.com` });
await runCommandLikeUpgradeRunner('up');
await expect(
insertUser({ email: `${PROBE_PREFIX}-UNIQUE@example.com` }),
).rejects.toThrow(/UQ_USER_EMAIL/);
});
it('still allows a soft-deleted row to share an address with a live one', async () => {
await insertUser({ email: `${PROBE_PREFIX}-revive@example.com` });
await runCommandLikeUpgradeRunner('up');
await expect(
insertUser({
email: `${PROBE_PREFIX}-REVIVE@example.com`,
deletedAt: new Date().toISOString(),
}),
).resolves.toBeUndefined();
});
it('keeps admin panel ILIKE searches working after conversion', async () => {
await insertUser({ email: `${PROBE_PREFIX}-search@example.com` });
await runCommandLikeUpgradeRunner('up');
const rows = await global.testDataSource.query(
`SELECT email FROM core."user" WHERE email ILIKE $1`,
[`${PROBE_PREFIX}-SEARCH%`],
);
expect(rows).toHaveLength(1);
});
});