Search code examples
phpdatabasecodeignitertransactionsassert

Prevent database commit on assert failure in php


For the sanity testing of my code I am putting asserts at various places in my code.

I want it to be the case that whenever an assert is hit, the database transaction should not be completed (i.e. the data should not be committed to database, instead it should be rolled back).

I could not find any clean way of achieving this.

I am using the code igniter framework.

Any idea how to achieve this?


Solution

  • The functionality should not be part of an assert. If it is the case then the functionality will be distorted in production mode when asserts are disabled.

    We can perform additional database operation on assert failure to record and track more details of the error, but should not abort/interfere with outside transactions.

    Assertions should be used as a debugging feature only. You may use them for sanity-checks that test for conditions that should always be TRUE and that indicate some programming errors if not or to check for the presence of certain features like extension functions or certain system limits and features.

    Assertions should not be used for normal runtime operations like input parameter checks. As a rule of thumb your code should always be able to work correctly if assertion checking is not activated.

    More details