Skip to content

Instantly share code, notes, and snippets.

@bamcgill
Last active October 3, 2022 15:49
Show Gist options
  • Save bamcgill/d2b1b8246288efc6044e27f2b768f6b0 to your computer and use it in GitHub Desktop.
Save bamcgill/d2b1b8246288efc6044e27f2b768f6b0 to your computer and use it in GitHub Desktop.
Example of how to call SQLcl liquibase functionality with java from SQLCL 22.3.0. Download SQLCL from OTN or YUM
package examples;
import java.io.BufferedOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import oracle.dbtools.raptor.liquibase.core.CommandGenerator;
import oracle.dbtools.raptor.liquibase.util.LbUtils;
import oracle.dbtools.raptor.newscriptrunner.ScriptRunnerContext;
import oracle.dbtools.raptor.newscriptrunner.ScriptUtils;
import oracle.dbtools.raptor.newscriptrunner.commands.SetDDLSettings;
import oracle.dbtools.raptor.newscriptrunner.commands.SetDDLSettings.Parameter;
import oracle.dbtools.raptor.newscriptrunner.parameterparser.CommandParserConstants;
import oracle.dbtools.raptor.newscriptrunner.parameterparser.parser.CommandParser;
import oracle.dbtools.raptor.scriptrunner.commands.liquibase.LBOptions;
import oracle.dbtools.raptor.scriptrunner.commands.liquibase.Messages;
/*
The following jars need to be on the classpath for this example to work
xmlparserv2_sans_jaxp_services.jar
guava-with-lf.jar
dbtools-apex.jar
dbtools-common.jar
dbtools-liquibase.jar
osdt_core.jar
osdt_cert.jar
oraclepki.jar
ojdbc11.jar
liquibase-core-4.15.0.jar
*/
public class example {
public static void main(final String[] args) {
// setup a context
final ScriptRunnerContext ctx = LbUtils.getContext();
try {
// create the connection
Class.forName("oracle.jdbc.driver.OracleDriver");
final String url = "jdbc:oracle:thin:@192.168.12.223:1521/xepdb1";
final Properties props = new Properties();
props.put("user", "skutz");
props.put("password", "oracle");
final Connection conn = DriverManager.getConnection(url, props);
// put the connection info onto the context
ctx.setCurrentConnection(conn);
ctx.setBaseConnection(conn);
ctx.putProperty(ScriptRunnerContext.CLI_CONN_URL, url);
ctx.putProperty(ScriptRunnerContext.CLI_CONN_PROPS, props);
// direct your output where you want
ctx.setOutputStreamWrapper(new BufferedOutputStream(System.out));
// change the log level here for more output from liquibase
final Level logLevel = Level.OFF;
final java.util.logging.Logger liquibaseLogger = java.util.logging.Logger.getLogger("liquibase");
final java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger("");
rootLogger.setLevel(Level.INFO);
liquibaseLogger.setLevel(logLevel);
for (final Handler handler : rootLogger.getHandlers()) {
if (handler instanceof ConsoleHandler) {
handler.setLevel(logLevel);
}
}
/// set ddl settting however you want them
SetDDLSettings.setDDLParameter(ctx, new Parameter(SetDDLSettings.TABLESPACE, "ON"), false);
SetDDLSettings.setDDLParameter(ctx, new Parameter(SetDDLSettings.SEGMENT_ATTRIBUTES, "ON"), false);
// note: passing the search path so I can have my code and xml files in different places
// set this to whatever command you want using the same syntax you would use inside SQLCL
final String command = "lb ups -changelog-file lob_table.xml -search-path /Volumes/sg/working";
// parse the command
CommandParser.loadParams(LBOptions.LB_PARMS, ScriptUtils.checkforContinuationChars(command), ctx, LBOptions.class);
// get the parsed command
final String _command = LbUtils.getParameter(CommandParserConstants.COMMAND);
// execute command
if (LBOptions.CALCULATE_CHECKSUM.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.CalculateCheckSumCommand());
} else if (LBOptions.CLEAR_CHECKSUMS.equalsIgnoreCase(_command)) {
CommandGenerator.ClearCheckSumsCommand();
} else if (LBOptions.VERSION.equalsIgnoreCase(_command)) {
ctx.write(LbUtils.getVersion());
} else if (LBOptions.HISTORY.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.HistoryCommand());
} else if (LBOptions.STATUS.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.StatusCommand());
} else if (LBOptions.VALIDATE.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.ValidateCommand());
} else if (LBOptions.CHANGELOG_SYNC.equalsIgnoreCase(_command)) {
CommandGenerator.ChangeLogSyncCommand();
} else if (LBOptions.CHANGELOG_SYNC_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.ChangeLogSyncSqlCommand());
} else if (LBOptions.CHANGELOG_SYNC_TO_TAG.equalsIgnoreCase(_command)) {
CommandGenerator.ChangeLogSyncCommand();
} else if (LBOptions.CHANGELOG_SYNC_TO_TAG_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.ChangeLogSyncSqlCommand());
} else if (LBOptions.DATA.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.DataCommand());
} else if (LBOptions.DIFF.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.DiffCommand());
} else if (LBOptions.DIFF_CHANGELOG.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.DiffChangelogCommand());
} else if (LBOptions.DB_DOC.equalsIgnoreCase(_command)) {
CommandGenerator.DbDocCommand();
} else if (LBOptions.FUTURE_ROLLBACK_COUNT_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.FutureRollbackCountSqlCommand());
} else if (LBOptions.FUTURE_ROLLBACK_FROM_TAG_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.FutureRollbackTagSqlCommand());
} else if (LBOptions.FUTURE_ROLLBACK_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.FutureRollbackSqlCommand());
} else if (LBOptions.GENERATE_CHANGELOG.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.GenerateChangelogCommand());
} else if (LBOptions.GENERATE_CONTROLFILE.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.GenerateControlFileCommand());
} else if (LBOptions.GENERATE_DB_OBJECT.equalsIgnoreCase(_command)) {
ctx.write(Messages.format(Messages.CHANGELOG_CREATED, CommandGenerator.GenerateDbObjectCommand(conn)));
} else if (LBOptions.GENERATE_ORDS_MODULE.equalsIgnoreCase(_command)) {
ctx.write(Messages.format(Messages.CHANGELOG_CREATED, CommandGenerator.GenerateOrdsModuleCommand(conn)));
} else if (LBOptions.GENERATE_ORDS_SCHEMA.equalsIgnoreCase(_command)) {
ctx.write(Messages.format(Messages.CHANGELOG_CREATED, CommandGenerator.GenerateOrdsSchemaCommand(conn)));
} else if (LBOptions.GENERATE_SCHEMA.equalsIgnoreCase(_command)) {
CommandGenerator.GenerateSchemaCommand(conn);
} else if (LBOptions.LIST_LOCKS.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.ListLocksCommand());
} else if (LBOptions.RELEASE_LOCKS.equalsIgnoreCase(_command)) {
CommandGenerator.ReleaseLocksCommand();
} else if (LBOptions.MARK_NEXT_CHANGESET_RAN.equalsIgnoreCase(_command)) {
CommandGenerator.MarkNextChangesetRanCommand();
} else if (LBOptions.MARK_NEXT_CHANGESET_RAN_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.MarkNextChangesetRanSqlCommand());
} else if (LBOptions.ROLLBACK.equalsIgnoreCase(_command)) {
CommandGenerator.RollbackCommand();
} else if (LBOptions.ROLLBACK_COUNT.equalsIgnoreCase(_command)) {
CommandGenerator.RollbackCountCommand();
} else if (LBOptions.ROLLBACK_COUNT_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.RollbackCountSqlCommand());
} else if (LBOptions.ROLLBACK_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.RollbackSqlCommand());
} else if (LBOptions.ROLLBACK_TO_DATE.equalsIgnoreCase(_command)) {
CommandGenerator.RollbackToDateCommand();
} else if (LBOptions.ROLLBACK_TO_DATE_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.RollbackToDateSqlCommand());
} else if (LBOptions.SNAPSHOT.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.SnapShotCommand());
} else if (LBOptions.SNAPSHOT_REFERENCE.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.SnapShotReferenceCommand());
} else if (LBOptions.TAG_EXISTS.equalsIgnoreCase(_command)) {
if (CommandGenerator.TagExistsCommand()) {
ctx.write(Messages.format(Messages.LB_TAGCHECK_SUCCESS, LbUtils.getParameter(LBOptions.TAG)));
} else {
ctx.write(Messages.format(Messages.LB_TAGCHECK_FAIL, LbUtils.getParameter(LBOptions.TAG)));
}
CommandGenerator.TagExistsCommand();
} else if (LBOptions.TAG.equalsIgnoreCase(_command)) {
CommandGenerator.TagCommand();
} else if (LBOptions.UNEXPECTED_CHANGESETS.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.UnexpectedChangessetsCommand());
} else if (LBOptions.UPDATE.equalsIgnoreCase(_command)) {
CommandGenerator.UpdateCommand();
} else if (LBOptions.UPDATE_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.UpdateSqlCommand());
} else if (LBOptions.UPDATE_COUNT.equalsIgnoreCase(_command)) {
CommandGenerator.UpdateCountCommand();
} else if (LBOptions.UPDATE_COUNT_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.UpdateCountSqlCommand());
} else if (LBOptions.UPDATE_TO_TAG.equalsIgnoreCase(_command)) {
CommandGenerator.UpdateToTagCommand();
} else if (LBOptions.UPDATE_TO_TAG_SQL.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.UpdateToTagSqlCommand());
} else if (LBOptions.UPDATE_TESTING_ROLLBACK.equalsIgnoreCase(_command)) {
ctx.write(CommandGenerator.UpdateTestingRollbackCommand());
} else if (LBOptions.DROP_ALL.equalsIgnoreCase(_command)) {
CommandGenerator.DropAllCommand();
} else {
ctx.write(Messages.getString(Messages.COMMAND_ERROR));
}
// force the buffer to write
ctx.getOutputStream().flush();
} catch (final Exception e) {
// handle any errors as you see fit
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment