3

I am new to Liquibase and have successfully generated the ddl scripts for the given change log.

I used the change set as an xml and generated the ddl scripts using the maven goal liquibase:updateSQl.I also used the liquibase.properies for specifying the url,driver,dialect etc

This worked fine for me and I used the liquibase version 3.5.5.

I was trying to do the same using the java code with the liquibase 3.5.5 added as my maven dependency. But I can't achieve the same using java code. Can someone put light in my path ?

How can I do updateSQL through java?

2
  • what is the question here? Do you want to execute updateSQL from java or what? Commented Apr 4, 2019 at 9:59
  • Ya, How to do updateSQL through java.. I have updated the question. Commented Apr 4, 2019 at 10:17

3 Answers 3

2

An updated API would be:

try (var connection = DriverManager.getConnection("jdbc:h2:mem:liqui;DB_CLOSE_DELAY=-1")) {
  var jdbcConnection = new JdbcConnection(connection);
  var db = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);

  new CommandScope(UpdateCommandStep.COMMAND_NAME)
      .addArgumentValue(DbUrlConnectionCommandStep.DATABASE_ARG, db)
      .addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, "db/root.yaml")
      .execute();
}
Sign up to request clarification or add additional context in comments.

Comments

1

I did a little research in the liquibase Main class and I came up with a solution . For the below code input is databaseChangeLog and output is ddl script flush.

 public class DDLScriptGenerator {

  protected ClassLoader classLoader;
  protected String driver;
  protected String username;
  protected String password;
  protected String url;
  protected String databaseClass;
  protected String defaultSchemaName;
  protected String outputDefaultSchema;
  protected String outputDefaultCatalog;
  protected String liquibaseCatalogName;
  protected String liquibaseSchemaName;
  protected String databaseChangeLogTableName;
  protected String databaseChangeLogLockTableName;
  protected String defaultCatalogName;
  protected String changeLogFile;
  protected String classpath;
  protected String contexts;
  protected String labels;
  protected String driverPropertiesFile;
  protected String propertyProviderClass = null;
  protected Boolean promptForNonLocalDatabase = null;
  protected Boolean includeSystemClasspath;
  protected Boolean strict = Boolean.TRUE;
  protected String defaultsFile = "liquibase.properties";
  protected String diffTypes;
  protected String changeSetAuthor;
  protected String changeSetContext;
  protected String dataOutputDirectory;
  protected String referenceDriver;
  protected String referenceUrl;
  protected String referenceUsername;
  protected String referencePassword;
  protected String referenceDefaultCatalogName;
  protected String referenceDefaultSchemaName;
  protected String currentDateTimeFunction;
  protected String command;
  protected Set<String> commandParams = new LinkedHashSet<String>();
  protected String logLevel;
  protected String logFile;
  protected Map<String, Object> changeLogParameters = new HashMap<String, Object>();
  protected String outputFile;



/**
 * @param d
 * @throws DatabaseException
 * @throws LiquibaseException
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
public void toSQL(DatabaseChangeLog d,String url,String user,String password)
        throws DatabaseException, LiquibaseException, UnsupportedEncodingException, IOException {

    this.url=url;
    this.username=user;
    this.password=password;
    this.driver=""; //your driver
    this.outputFile=""; // The path in which the script have to be flushed.
    FileSystemResourceAccessor fsOpener = new FileSystemResourceAccessor();
    CommandLineResourceAccessor clOpener = new CommandLineResourceAccessor(this.getClass().getClassLoader());
    CompositeResourceAccessor fileOpener = new CompositeResourceAccessor(new ResourceAccessor[] { fsOpener, clOpener });

    Database database = CommandLineUtils.createDatabaseObject(fileOpener, this.url, this.username, this.password, this.driver, 
            this.defaultCatalogName, this.defaultSchemaName, Boolean.parseBoolean(this.outputDefaultCatalog),
            Boolean.parseBoolean(this.outputDefaultSchema), this.databaseClass, 
            this.driverPropertiesFile, this.propertyProviderClass, this.liquibaseCatalogName, 
            this.liquibaseSchemaName, this.databaseChangeLogTableName, this.databaseChangeLogLockTableName);


    Liquibase liquibase=new Liquibase(d, null, database);

    liquibase.update(new Contexts(this.contexts), new LabelExpression(this.labels), getOutputWriter());
}

   private Writer getOutputWriter()
    throws UnsupportedEncodingException, IOException
  {

    String charsetName = ((GlobalConfiguration)LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class)).getOutputEncoding();

    if (this.outputFile != null) {
      try {
        FileOutputStream fileOut = new FileOutputStream(this.outputFile, false);
        return new OutputStreamWriter(fileOut, charsetName);
      } catch (IOException e) {
        System.err.printf("Could not create output file %s\n", new Object[] { this.outputFile });
        throw e;
      }
    }
    return new OutputStreamWriter(System.out, charsetName);
  }


 }

1 Comment

you are not closing resources such a Writer, be careful about it.
1

You might have a look at: https://www.hascode.com/2014/07/java-ee-7-database-migrations-with-liquibase-and-wildfly/#Bootstrap_Loader

(I don't know, whether I am allowed to copy&paste the code example from this site to stackoverflow, so I don't do it.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.