So, I have had a lot of success with this; I now have configurations being created on the fly for on-demand builds, copied from a template (and even updated if the template changes). The next step is to have them automatically cleaned up when they are no longer needed.
We already have retention policies set so that builds are deleted after a week. Now I am trying to have the configuration delete itself automatically once the last build is removed. I have set this up as an "After Build Delete Script":
if (! configuration.getLatestBuild()) system.configurationManager.delete( configuration );
Now, when I delete the last build in the configuration, I get thrown into an error page saying "Database constraint violated: could not execute statement". However, I can navigate back to the dashboard from this page and see that the configuration was deleted as I wished.
I tried putting the above code in a try/catch block, but I think the error is coming from someplace else. Is there a better way to do this, or a way to block this message from coming up? This is the stack dump:
java.sql.SQLIntegrityConstraintViolationException: (conn=249) Cannot add or update a child row: a foreign key constraint fails (`quickbuild_2022_07`.`QB_AUDIT`, CONSTRAINT `FK_AUDIT_CONF` FOREIGN KEY (`QB_CONFIGURATION_ID`) REFERENCES `QB_CONFIGURATION` (`QB_ID`))