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