Idea for checking preconditions during updateSQL

This was going to be another possible extension contest entry, but day-job demands got in the way…

Here’s an idea for checking preconditions during updateSQL:

Create a DatabaseWrapper class which encapsulates a rolling DatabaseSnapshot and a regular Database.

Build an initial DatabaseSnapshot at startup.

Use new custom SqlGenerators to modify the rolling DatabaseSnapshot as appropriate for the statement (e.g. CreateTable, AddColumn, etc).

When a precondition asks for a DatabaseSnapshot, let the Wrapper serve up the rollingDatabaseSnapshot instead.

That way, if you have precondition dependencies, your updateSQL should take those into account.

For example,

                                                                                                                         

       
           
               
           

               
           
       

Chris Imershein

I have thought about this type of approach before.  The reason it is not in the core is that the first time you hit an or tag you will not know what should be done to the database snapshot.

It would be great as an extension, however, since then you can set requirements and expectations better.

Nathan

If I understand correctly, my extension would extend AbstractChange.


My question is:  how does my method implementing “generateStatements()” know whether this is “update” - make the changes to the database - or “updateSQL” - do not make database changes.


I’m guessing that the following test within my extension’s generateStatements() method will work but seems very un-clean:


  1.       if (ExecutorService.getInstance().getExecutor(database) instanceof LoggingExecutor) 
  2.       {
  3.            // generateSQL - do not change database
  4.       }
  5.       else
  6.       {
  7.           // generate - make database changes as side effect
  8.       }

The knowledge of whether this is “generate” vs. “generateSQL” seems to be completely contained in Main.