@@ -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+
254364test ( '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+
286433test ( 'rename table rename column #1' , async ( t ) => {
287434 const schema1 = {
288435 users : pgTable ( 'users' , {
0 commit comments