Using Liquibase Java API to generate SQL file from ChangeLog

Hi everybody,

I am new to Liquibase and I try to use it in a Java Class.

With the Liquibase Object I can update a Database (Liquibase update method).

With the CommandLineUtils.doGenerateChangeLog(…) method I can also generate a ChangeLog file directly (great feature :wink: )

I would like to be able to generate a SQL Query File to be able to review the SQL Query. I’ve seen it is possible with the CommandLine tool, but I need a way to do it programmatically… 


Is there a way to do it?

If not I will try with a ProcessBuilder…


Thanks a lot in advance!

Emmanuel

The LIquibase.update method generates the SQL to run without executing it if you pass along the Writer parameter.


Nathan

Nathan,

Could you please explain what this writer parameter is?  I want to use updateSQL command without the Maven plugin using the command line tool.  I have already setup our db using Liquibase changelogs. 

I think, to generate SQL and save to a file,  I just need the same commands I used to create the db via LB,  but instead of update at the end of the command I need to use updateSQL > outfile.txt.  My concern however is that this would only generate the sql for the changelog that is specified in the command line.  How do I generate the sql for the entire db.  Can I simply exclude the changelog file,  but it is a required parameter.  Also, I believe the sql will be generated only for the specified user/schema, correct?

Thank you in advance for your response.

If you are using the command line, you wouldn’t have to worry about the writer parameter is. That is just if you are using the java API directly.


Yes, if you run updateSQL it runs liquibase update like it would have done against your given database with the passed changelog.  You need the changelog file for updateSQL because it needs to know what changesets to try to run.


If you have liquibase changesets for your whole database already, running updateSQL against a blank database will give the SQL to fully create the database. If you have an existing database that you want changeSets and/or SQL generated for, the generateChangeLog command will get you a changelog file that would match the current database (just a single user and schema, though). You can then run that through updateSQL to get the creation SQL.


Nathan