How to make RPM not write to RPM database if RPM fails to deploy?

How to make RPM not write to RPM database if RPM fails to deploy?

IE I create an rpm spec file that contains the following

if [ ! -f /test ]
then
exit 1
fi

My rpm will fail at deployment, but if I do rpm -qa , I can see the rpm in the rpm db

Where are you putting that code ? In the "preinstall" section ?
Also instead of the above script, you could just define that dir in the spec file and get the rpm to create it. If its created by another package (rpm -qf /dir), then name that package in the "requires" list in the spec file.

I am just using the folder test an a example. The real test was, I execute a binary file and if the error code is not 0 I use exit 1, but the RPM entry is still in the database.

And do you run that test from the preinstall section of the rpm spec ?