@@ -765,7 +765,7 @@ postgresTest(
765765 }
766766) ;
767767
768- describe ( "EnvironmentVariablesRepository.deleteValues " , ( ) => {
768+ describe ( "EnvironmentVariablesRepository value deletes " , ( ) => {
769769 const vercel = { type : "integration" as const , integration : "vercel" } ;
770770
771771 async function createBranchWithParent (
@@ -954,12 +954,13 @@ describe("EnvironmentVariablesRepository.deleteValues", () => {
954954 id : value . id ,
955955 version : value . variable . key === "STALE" ? value . version + 1 : value . version ,
956956 variableId : value . variableId ,
957+ environmentId : branch . id ,
957958 key : value . variable . key ,
958959 secretReferenceKey : value . valueReference ?. key ,
959960 } ) ) ;
960961
961962 const result = await prisma . $transaction ( ( tx ) =>
962- deleteEnvironmentVariableValueRows ( tx , project . id , branch . id , rows )
963+ deleteEnvironmentVariableValueRows ( tx , project . id , rows )
963964 ) ;
964965
965966 expect ( result . deleted . map ( ( r ) => r . key ) ) . toEqual ( [ "FRESH" ] ) ;
@@ -994,4 +995,87 @@ describe("EnvironmentVariablesRepository.deleteValues", () => {
994995 ) . toEqual ( { deleted : [ ] , skipped : [ "SHARED" ] } ) ;
995996 expect ( await theirs . ownKeys ( theirs . branch . id ) ) . toEqual ( [ "SHARED" ] ) ;
996997 } ) ;
998+ postgresTest ( "skips duplicate keys and keys whose row is already gone" , async ( { prisma } ) => {
999+ const { project, branch, repository, write, ownKeys } = await createBranchWithParent ( prisma ) ;
1000+ await write ( branch . id , { GONE : "g" , KEPT : "k" } , vercel ) ;
1001+ await prisma . environmentVariableValue . deleteMany ( {
1002+ where : { environmentId : branch . id , variable : { key : "GONE" } } ,
1003+ } ) ;
1004+
1005+ expect (
1006+ await repository . deleteValues ( project . id , {
1007+ environmentId : branch . id ,
1008+ keys : [ "GONE" , "GONE" , "KEPT" , "KEPT" , "NEVER" ] ,
1009+ } )
1010+ ) . toEqual ( { deleted : [ "KEPT" ] , skipped : [ "GONE" , "NEVER" ] } ) ;
1011+ expect ( await ownKeys ( branch . id ) ) . toEqual ( [ ] ) ;
1012+ } ) ;
1013+
1014+ postgresTest (
1015+ "deleteValue removes the variable with its last value and its secret rows" ,
1016+ async ( { prisma } ) => {
1017+ const { project, branch, repository, write, variableKeys, secretRows } =
1018+ await createBranchWithParent ( prisma ) ;
1019+ await write ( branch . id , { ONLY : "o" , OTHER : "x" } , vercel ) ;
1020+ const variable = await prisma . environmentVariable . findFirstOrThrow ( {
1021+ where : { projectId : project . id , key : "ONLY" } ,
1022+ } ) ;
1023+
1024+ expect (
1025+ await repository . deleteValue ( project . id , { id : variable . id , environmentId : branch . id } )
1026+ ) . toEqual ( { success : true } ) ;
1027+ expect ( await variableKeys ( ) ) . toEqual ( [ "OTHER" ] ) ;
1028+ expect ( await secretRows ( branch . id ) ) . toEqual ( { store : [ "OTHER" ] , references : [ "OTHER" ] } ) ;
1029+ expect (
1030+ await repository . deleteValue ( project . id , { id : variable . id , environmentId : branch . id } )
1031+ ) . toEqual ( { success : false , error : "Environment variable not found" } ) ;
1032+ }
1033+ ) ;
1034+
1035+ postgresTest (
1036+ "deleteValue keeps the value the variable has in another environment" ,
1037+ async ( { prisma } ) => {
1038+ const { project, parent, branch, repository, write, ownKeys, variableKeys, secretRows } =
1039+ await createBranchWithParent ( prisma ) ;
1040+ await write ( branch . id , { BOTH : "branch" } , vercel ) ;
1041+ await write ( parent . id , { BOTH : "root" } , vercel ) ;
1042+ const variable = await prisma . environmentVariable . findFirstOrThrow ( {
1043+ where : { projectId : project . id , key : "BOTH" } ,
1044+ } ) ;
1045+
1046+ expect (
1047+ await repository . deleteValue ( project . id , { id : variable . id , environmentId : branch . id } )
1048+ ) . toEqual ( { success : true } ) ;
1049+ expect ( await variableKeys ( ) ) . toEqual ( [ "BOTH" ] ) ;
1050+ expect ( await ownKeys ( branch . id ) ) . toEqual ( [ ] ) ;
1051+ expect ( await ownKeys ( parent . id ) ) . toEqual ( [ "BOTH" ] ) ;
1052+ expect ( await secretRows ( branch . id ) ) . toEqual ( { store : [ ] , references : [ ] } ) ;
1053+ expect ( await secretRows ( parent . id ) ) . toEqual ( { store : [ "BOTH" ] , references : [ "BOTH" ] } ) ;
1054+ expect ( await repository . getEnvironmentVariables ( project . id , branch . id , parent . id ) ) . toEqual ( [
1055+ { key : "BOTH" , value : "root" } ,
1056+ ] ) ;
1057+ expect (
1058+ await repository . deleteValue ( project . id , { id : variable . id , environmentId : branch . id } )
1059+ ) . toEqual ( { success : false , error : "Environment variable value not found" } ) ;
1060+ }
1061+ ) ;
1062+
1063+ postgresTest ( "deleteValue handles a value without a secret reference" , async ( { prisma } ) => {
1064+ const { project, branch, repository, write, variableKeys, secretRows } =
1065+ await createBranchWithParent ( prisma ) ;
1066+ await write ( branch . id , { UNLINKED : "u" } , vercel ) ;
1067+ const variable = await prisma . environmentVariable . findFirstOrThrow ( {
1068+ where : { projectId : project . id , key : "UNLINKED" } ,
1069+ } ) ;
1070+ await prisma . environmentVariableValue . updateMany ( {
1071+ where : { variableId : variable . id , environmentId : branch . id } ,
1072+ data : { valueReferenceId : null } ,
1073+ } ) ;
1074+
1075+ expect (
1076+ await repository . deleteValue ( project . id , { id : variable . id , environmentId : branch . id } )
1077+ ) . toEqual ( { success : true } ) ;
1078+ expect ( await variableKeys ( ) ) . toEqual ( [ ] ) ;
1079+ expect ( ( await secretRows ( branch . id ) ) . store ) . toEqual ( [ ] ) ;
1080+ } ) ;
9971081} ) ;
0 commit comments