Spaces:
Build error
Build error
File size: 6,952 Bytes
d9494a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | import { Test } from '@nestjs/testing';
import { getDataSourceToken } from '@nestjs/typeorm';
import { InstanceCommandGenerationService } from 'src/database/commands/instance-command-generation.service';
import { type TwentyAllVersion } from 'src/engine/core-modules/upgrade/constants/twenty-all-versions.constant';
const VERSION_A = '10.20.0' as TwentyAllVersion;
const VERSION_B = '30.40.0' as TwentyAllVersion;
const FIXED_TIMESTAMP = 1775000000000;
const buildMockDataSource = (
upQueries: { query: string; parameters?: unknown[] }[],
downQueries: { query: string; parameters?: unknown[] }[],
) => ({
driver: {
createSchemaBuilder: () => ({
log: jest.fn().mockResolvedValue({ upQueries, downQueries }),
}),
},
});
describe('InstanceCommandGenerationService', () => {
const buildService = async (
upQueries: { query: string; parameters?: unknown[] }[] = [],
downQueries: { query: string; parameters?: unknown[] }[] = [],
) => {
const module = await Test.createTestingModule({
providers: [
InstanceCommandGenerationService,
{
provide: getDataSourceToken(),
useValue: buildMockDataSource(upQueries, downQueries),
},
],
}).compile();
return module.get(InstanceCommandGenerationService);
};
it('should return null when no schema changes are detected', async () => {
const service = await buildService();
const result = await service.generateInstanceCommand({
migrationName: 'no-changes',
version: VERSION_A,
timestamp: FIXED_TIMESTAMP,
});
expect(result).toBeNull();
});
it('should generate a migration with a single up/down query', async () => {
const service = await buildService(
[{ query: 'ALTER TABLE "core"."user" ADD "foo" varchar' }],
[{ query: 'ALTER TABLE "core"."user" DROP COLUMN "foo"' }],
);
const result = await service.generateInstanceCommand({
migrationName: 'add-foo-column',
version: VERSION_A,
timestamp: FIXED_TIMESTAMP,
});
expect(result).toMatchSnapshot();
});
it('should generate a migration with multiple queries', async () => {
const service = await buildService(
[
{
query:
'CREATE TABLE "core"."task" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "name" varchar NOT NULL)',
},
{
query:
'ALTER TABLE "core"."task" ADD CONSTRAINT "PK_task" PRIMARY KEY ("id")',
},
],
[
{ query: 'ALTER TABLE "core"."task" DROP CONSTRAINT "PK_task"' },
{ query: 'DROP TABLE "core"."task"' },
],
);
const result = await service.generateInstanceCommand({
migrationName: 'create-task-table',
version: VERSION_A,
timestamp: FIXED_TIMESTAMP,
});
expect(result).toMatchSnapshot();
});
it('should generate a migration with query parameters', async () => {
const service = await buildService(
[
{
query:
'INSERT INTO "core"."setting" ("key", "value") VALUES ($1, $2)',
parameters: ['theme', 'dark'],
},
],
[
{
query: 'DELETE FROM "core"."setting" WHERE "key" = $1',
parameters: ['theme'],
},
],
);
const result = await service.generateInstanceCommand({
migrationName: 'seed-setting',
version: VERSION_A,
timestamp: FIXED_TIMESTAMP,
});
expect(result).toMatchSnapshot();
});
it('should escape single quotes in SQL queries', async () => {
const service = await buildService(
[{ query: 'UPDATE "core"."config" SET "value" = \'it\'\'s done\'' }],
[{ query: 'UPDATE "core"."config" SET "value" = \'original\'' }],
);
const result = await service.generateInstanceCommand({
migrationName: 'update-config',
version: VERSION_A,
timestamp: FIXED_TIMESTAMP,
});
expect(result).toMatchSnapshot();
});
it('should escape backslashes in SQL queries', async () => {
const service = await buildService(
[
{
query: 'UPDATE "core"."config" SET "value" = E\'path\\\\to\\\\file\'',
},
],
[{ query: 'UPDATE "core"."config" SET "value" = NULL' }],
);
const result = await service.generateInstanceCommand({
migrationName: 'update-path',
version: VERSION_A,
timestamp: FIXED_TIMESTAMP,
});
expect(result).toMatchSnapshot();
});
it('should use default migration name in class and file names', async () => {
const service = await buildService(
[{ query: 'ALTER TABLE "core"."user" ADD "bar" integer' }],
[{ query: 'ALTER TABLE "core"."user" DROP COLUMN "bar"' }],
);
const result = await service.generateInstanceCommand({
migrationName: 'auto-generated',
version: VERSION_A,
timestamp: FIXED_TIMESTAMP,
});
expect(result).toMatchSnapshot();
});
it('should encode version correctly in file and class names', async () => {
const service = await buildService(
[{ query: 'SELECT 1' }],
[{ query: 'SELECT 1' }],
);
const result = await service.generateInstanceCommand({
migrationName: 'test',
version: VERSION_B,
timestamp: FIXED_TIMESTAMP,
});
expect(result).toMatchSnapshot();
});
it('should return null for slow type when no schema changes are detected', async () => {
const service = await buildService();
const result = await service.generateInstanceCommand({
migrationName: 'no-changes',
version: VERSION_A,
timestamp: FIXED_TIMESTAMP,
type: 'slow',
});
expect(result).toBeNull();
});
it('should generate a slow instance command with populated up/down', async () => {
const service = await buildService(
[
{
query: 'ALTER TABLE "core"."user" ALTER COLUMN "email" SET NOT NULL',
},
],
[
{
query: 'ALTER TABLE "core"."user" ALTER COLUMN "email" DROP NOT NULL',
},
],
);
const result = await service.generateInstanceCommand({
migrationName: 'make-column-not-nullable',
version: VERSION_A,
timestamp: FIXED_TIMESTAMP,
type: 'slow',
});
expect(result).toMatchSnapshot();
});
it('should use correct file naming for slow instance commands', async () => {
const service = await buildService(
[{ query: 'SELECT 1' }],
[{ query: 'SELECT 1' }],
);
const result = await service.generateInstanceCommand({
migrationName: 'backfill-data',
version: VERSION_B,
timestamp: FIXED_TIMESTAMP,
type: 'slow',
});
const versionSlug = VERSION_B.split('.').slice(0, 2).join('-');
expect(result?.fileName).toBe(
`${versionSlug}-instance-command-slow-${FIXED_TIMESTAMP}-backfill-data.ts`,
);
expect(result?.className).toBe('BackfillDataSlowInstanceCommand');
});
});
|