问题
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?
回答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);
}
}
来源:https://stackoverflow.com/questions/55499807/how-to-use-liquibase-programatically-in-java