Skip to content

Commit a8cbe1c

Browse files
Merge branch 'issues' of https://github.com/drizzle-team/drizzle-orm into issues
2 parents 91cf23d + 31c0dce commit a8cbe1c

File tree

3 files changed

+152
-2
lines changed

3 files changed

+152
-2
lines changed

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

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ test('alter column type to custom type', async (t) => {
216216
// now this won't happen
217217
// In the issue that is shown that data type was changed from text to jsonb, but it is needed to use "USING ..."
218218
// so it was tested with "text" -> "varchar"
219-
test('alter text type to jsonb type', async (t) => {
219+
test('alter text type to jsonb type', async () => {
220220
const schema1 = {
221221
table1: pgTable('table1', {
222222
column1: text(),
@@ -251,6 +251,116 @@ test('alter text type to jsonb type', async (t) => {
251251
expect(res[0].column1).toBe('{"b":2}');
252252
});
253253

254+
// https://github.com/drizzle-team/drizzle-orm/issues/3589
255+
test('alter integer type to text type with fk constraints', async () => {
256+
const users1 = pgTable('users', {
257+
id: serial().primaryKey(),
258+
});
259+
260+
const schema1 = {
261+
users1,
262+
sessions: pgTable('sessions', {
263+
id: text().primaryKey(),
264+
userId: integer().notNull().references(() => users1.id),
265+
}),
266+
content: pgTable('content', {
267+
id: text().primaryKey(),
268+
userId: integer().notNull().references(() => users1.id),
269+
}),
270+
};
271+
272+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
273+
await push({ db, to: schema1 });
274+
await db.query('insert into "users" values (1);');
275+
await db.query('insert into "sessions" values (1,1);');
276+
await db.query('insert into "content" values (1,1);');
277+
278+
const users2 = pgTable('users', {
279+
id: text().primaryKey(),
280+
});
281+
const schema2 = {
282+
users2,
283+
sessions: pgTable('sessions', {
284+
id: text().primaryKey(),
285+
userId: text().notNull().references(() => users2.id),
286+
}),
287+
content: pgTable('content', {
288+
id: text().primaryKey(),
289+
userId: text().notNull().references(() => users2.id),
290+
}),
291+
};
292+
293+
const { sqlStatements: st2 } = await diff(n1, schema2, []);
294+
const { sqlStatements: pst2 } = await push({ db, to: schema2 });
295+
296+
const expectedSt2 = [
297+
'ALTER TABLE "sessions" DROP CONSTRAINT "sessions_userId_users_id_fkey";',
298+
'ALTER TABLE "content" DROP CONSTRAINT "content_userId_users_id_fkey";',
299+
'ALTER TABLE "users" ALTER COLUMN "id" SET DATA TYPE text;',
300+
'ALTER TABLE "sessions" ALTER COLUMN "userId" SET DATA TYPE text;',
301+
'ALTER TABLE "content" ALTER COLUMN "userId" SET DATA TYPE text;',
302+
'ALTER TABLE "sessions" ADD CONSTRAINT "sessions_userId_users_id_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id");',
303+
'ALTER TABLE "content" ADD CONSTRAINT "content_userId_users_id_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id");',
304+
];
305+
expect(st2).toStrictEqual(expectedSt2);
306+
expect(pst2).toStrictEqual(expectedSt2);
307+
});
308+
309+
// https://github.com/drizzle-team/drizzle-orm/issues/3589
310+
test('alter integer type to text type with fk constraints', async () => {
311+
const users1 = pgTable('users', {
312+
id: serial().primaryKey(),
313+
});
314+
315+
const schema1 = {
316+
users1,
317+
sessions: pgTable('sessions', {
318+
id: text().primaryKey(),
319+
userId: integer().notNull().references(() => users1.id),
320+
}),
321+
content: pgTable('content', {
322+
id: text().primaryKey(),
323+
userId: integer().notNull().references(() => users1.id),
324+
}),
325+
};
326+
327+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
328+
await push({ db, to: schema1 });
329+
await db.query('insert into "users" values (1);');
330+
await db.query('insert into "sessions" values (1,1);');
331+
await db.query('insert into "content" values (1,1);');
332+
333+
const users2 = pgTable('users', {
334+
id: text().primaryKey(),
335+
});
336+
const schema2 = {
337+
users2,
338+
sessions: pgTable('sessions', {
339+
id: text().primaryKey(),
340+
userId: text().notNull().references(() => users2.id),
341+
}),
342+
content: pgTable('content', {
343+
id: text().primaryKey(),
344+
userId: text().notNull().references(() => users2.id),
345+
}),
346+
};
347+
348+
const { sqlStatements: st2 } = await diff(n1, schema2, []);
349+
const { sqlStatements: pst2 } = await push({ db, to: schema2 });
350+
351+
const expectedSt2 = [
352+
'ALTER TABLE "sessions" DROP CONSTRAINT "sessions_userId_users_id_fkey";',
353+
'ALTER TABLE "content" DROP CONSTRAINT "content_userId_users_id_fkey";',
354+
'ALTER TABLE "users" ALTER COLUMN "id" SET DATA TYPE text;',
355+
'ALTER TABLE "sessions" ALTER COLUMN "userId" SET DATA TYPE text;',
356+
'ALTER TABLE "content" ALTER COLUMN "userId" SET DATA TYPE text;',
357+
'ALTER TABLE "sessions" ADD CONSTRAINT "sessions_userId_users_id_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id");',
358+
'ALTER TABLE "content" ADD CONSTRAINT "content_userId_users_id_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id");',
359+
];
360+
expect(st2).toStrictEqual(expectedSt2);
361+
expect(pst2).toStrictEqual(expectedSt2);
362+
});
363+
254364
test('alter table add composite pk', async (t) => {
255365
const schema1 = {
256366
table: pgTable('table', {
@@ -283,6 +393,43 @@ test('alter table add composite pk', async (t) => {
283393
expect(pst).toStrictEqual(st0);
284394
});
285395

396+
// https://github.com/drizzle-team/drizzle-orm/issues/3496
397+
test('remove/add pk', async (t) => {
398+
const Step = pgTable('Step', {
399+
id: bigint({ mode: 'number' }).primaryKey(),
400+
});
401+
const schema1 = {
402+
Step1: Step,
403+
Branch: pgTable('Branch', {
404+
id: bigint({ mode: 'number' }).primaryKey(),
405+
stepId: bigint({ mode: 'number' }).references(() => Step.id, { onDelete: 'cascade' }),
406+
}),
407+
};
408+
409+
const { next: n1 } = await diff({}, schema1, []);
410+
await push({ db, to: schema1 });
411+
412+
const schema2 = {
413+
Step,
414+
Branch: pgTable('Branch', {
415+
stepId: bigint({ mode: 'number' }).primaryKey().references(() => Step.id, { onDelete: 'cascade' }),
416+
}),
417+
};
418+
419+
const { sqlStatements: st2 } = await diff(n1, schema2, []);
420+
const { sqlStatements: pst2 } = await push({ db, to: schema2 });
421+
422+
const expectedSt2 = [
423+
[
424+
'ALTER TABLE "Branch" DROP CONSTRAINT "Branch_pkey";',
425+
'ALTER TABLE "Branch" DROP COLUMN "id";',
426+
'ALTER TABLE "Branch" ADD PRIMARY KEY ("stepId");',
427+
],
428+
];
429+
expect(st2).toStrictEqual(expectedSt2);
430+
expect(pst2).toStrictEqual(expectedSt2);
431+
});
432+
286433
test('rename table rename column #1', async (t) => {
287434
const schema1 = {
288435
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
@@ -341,13 +341,14 @@ test('generated column: link to another jsonb column', async () => {
341341
expect(sqlStatements.length).toBe(0);
342342
});
343343

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

0 commit comments

Comments
 (0)