如何在java中以编程方式使用Liquibase?

问题描述 投票:1回答:1

我是Liquibase的新手,并已成功为给定的更改日志生成ddl脚本。

我使用变化集作为xml并使用maven目标liquibase:updateSQl生成ddl脚本。我还使用liquibase.properies来指定urldriverdialect

这对我来说很好,我使用了liquibase version 3.5.5

我试图使用java代码同样添加liquibase 3.5.5作为我的maven依赖项。但我无法使用java代码实现相同的功能。有人可以把光放在我的路上吗?

我怎么能通过java做updateSQL

spring jpa liquibase changelog
1个回答
0
投票

我在liquibase Main课上做了一些研究,我想出了一个解决方案。对于下面的代码inputdatabaseChangeLogoutputddl脚本刷新。

 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);
  }


 }
© www.soinside.com 2019 - 2024. All rights reserved.