Skip to content

Commit f61ee9b

Browse files
committed
[drizzle-kit] added tests
1 parent d65fe4b commit f61ee9b

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

drizzle-kit/tests/postgres/pg-columns.test.ts

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ test('alter column type to custom type', async (t) => {
211211
});
212212

213213
// https://github.com/drizzle-team/drizzle-orm/issues/4245
214-
test('alter text type to jsonb type', async (t) => {
214+
test('alter text type to jsonb type', async () => {
215215
const schema1 = {
216216
table1: pgTable('table1', {
217217
column1: text(),
@@ -245,6 +245,61 @@ test('alter text type to jsonb type', async (t) => {
245245
expect(res[0]).toBeDefined();
246246
});
247247

248+
// https://github.com/drizzle-team/drizzle-orm/issues/3589
249+
test('alter integer type to text type with fk constraints', async () => {
250+
const users1 = pgTable('users', {
251+
id: serial().primaryKey(),
252+
});
253+
254+
const schema1 = {
255+
users1,
256+
sessions: pgTable('sessions', {
257+
id: text().primaryKey(),
258+
userId: integer().notNull().references(() => users1.id),
259+
}),
260+
content: pgTable('content', {
261+
id: text().primaryKey(),
262+
userId: integer().notNull().references(() => users1.id),
263+
}),
264+
};
265+
266+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
267+
await push({ db, to: schema1 });
268+
await db.query('insert into "users" values (1);');
269+
await db.query('insert into "sessions" values (1,1);');
270+
await db.query('insert into "content" values (1,1);');
271+
272+
const users2 = pgTable('users', {
273+
id: text().primaryKey(),
274+
});
275+
const schema2 = {
276+
users2,
277+
sessions: pgTable('sessions', {
278+
id: text().primaryKey(),
279+
userId: text().notNull().references(() => users2.id),
280+
}),
281+
content: pgTable('content', {
282+
id: text().primaryKey(),
283+
userId: text().notNull().references(() => users2.id),
284+
}),
285+
};
286+
287+
const { sqlStatements: st2 } = await diff(n1, schema2, []);
288+
const { sqlStatements: pst2 } = await push({ db, to: schema2 });
289+
290+
const expectedSt2 = [
291+
'ALTER TABLE "sessions" DROP CONSTRAINT "sessions_userId_users_id_fkey";',
292+
'ALTER TABLE "content" DROP CONSTRAINT "content_userId_users_id_fkey";',
293+
'ALTER TABLE "users" ALTER COLUMN "id" SET DATA TYPE text;',
294+
'ALTER TABLE "sessions" ALTER COLUMN "userId" SET DATA TYPE text;',
295+
'ALTER TABLE "content" ALTER COLUMN "userId" SET DATA TYPE text;',
296+
'ALTER TABLE "sessions" ADD CONSTRAINT "sessions_userId_users_id_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id");',
297+
'ALTER TABLE "content" ADD CONSTRAINT "content_userId_users_id_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id");',
298+
];
299+
expect(st2).toStrictEqual(expectedSt2);
300+
expect(pst2).toStrictEqual(expectedSt2);
301+
});
302+
248303
test('alter table add composite pk', async (t) => {
249304
const schema1 = {
250305
table: pgTable('table', {
@@ -277,6 +332,43 @@ test('alter table add composite pk', async (t) => {
277332
expect(pst).toStrictEqual(st0);
278333
});
279334

335+
// https://github.com/drizzle-team/drizzle-orm/issues/3496
336+
test('remove/add pk', async (t) => {
337+
const Step = pgTable('Step', {
338+
id: bigint({ mode: 'number' }).primaryKey(),
339+
});
340+
const schema1 = {
341+
Step1: Step,
342+
Branch: pgTable('Branch', {
343+
id: bigint({ mode: 'number' }).primaryKey(),
344+
stepId: bigint({ mode: 'number' }).references(() => Step.id, { onDelete: 'cascade' }),
345+
}),
346+
};
347+
348+
const { next: n1 } = await diff({}, schema1, []);
349+
await push({ db, to: schema1 });
350+
351+
const schema2 = {
352+
Step,
353+
Branch: pgTable('Branch', {
354+
stepId: bigint({ mode: 'number' }).primaryKey().references(() => Step.id, { onDelete: 'cascade' }),
355+
}),
356+
};
357+
358+
const { sqlStatements: st2 } = await diff(n1, schema2, []);
359+
const { sqlStatements: pst2 } = await push({ db, to: schema2 });
360+
361+
const expectedSt2 = [
362+
[
363+
'ALTER TABLE "Branch" DROP CONSTRAINT "Branch_pkey";',
364+
'ALTER TABLE "Branch" DROP COLUMN "id";',
365+
'ALTER TABLE "Branch" ADD PRIMARY KEY ("stepId");',
366+
],
367+
];
368+
expect(st2).toStrictEqual(expectedSt2);
369+
expect(pst2).toStrictEqual(expectedSt2);
370+
});
371+
280372
test('rename table rename column #1', async (t) => {
281373
const schema1 = {
282374
users: pgTable('users', {

drizzle-kit/tests/postgres/pg-defaults.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ test('numeric arrays', async () => {
389389
expect.soft(res24).toStrictEqual([]);
390390
});
391391

392+
// https://github.com/drizzle-team/drizzle-orm/issues/3582
392393
test('real + real arrays', async () => {
393394
const res1 = await diffDefault(_, real().default(1000.123), '1000.123');
394395

drizzle-kit/tests/postgres/pull.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,14 @@ test('generated column: link to another jsonb column', async () => {
340340
expect(sqlStatements.length).toBe(0);
341341
});
342342

343+
// https://github.com/drizzle-team/drizzle-orm/issues/3593
343344
// https://github.com/drizzle-team/drizzle-orm/issues/4349
344345
// https://github.com/drizzle-team/drizzle-orm/issues/4632
345346
// https://github.com/drizzle-team/drizzle-orm/issues/4644
346347
// https://github.com/drizzle-team/drizzle-orm/issues/4730
347348
// https://github.com/drizzle-team/drizzle-orm/issues/4760
348349
// https://github.com/drizzle-team/drizzle-orm/issues/4916
349-
test('introspect all column types', async () => {
350+
test.only('introspect all column types', async () => {
350351
const myEnum = pgEnum('my_enum', ['a', 'b', 'c']);
351352
const schema = {
352353
enum_: myEnum,
@@ -366,6 +367,7 @@ test('introspect all column types', async () => {
366367
text3: text('text3').default(''),
367368
varchar: varchar('varchar', { length: 25 }).default('abc'),
368369
varchar1: varchar('varchar1', { length: 25 }).default(''),
370+
varchar2: varchar('varchar2').default(sql`md5((random())::text)`),
369371
char: char('char', { length: 3 }).default('abc'),
370372
char1: char('char1', { length: 3 }).default(''),
371373
serial: serial('serial'),

0 commit comments

Comments
 (0)