Skip to content

Commit

Permalink
Merge pull request #2371 from liquibase/DAT-7447
Browse files Browse the repository at this point in the history
Reworked license not found message
  • Loading branch information
suryaaki2 committed Feb 17, 2022
2 parents 252c18e + 1f329e9 commit 3632cfa
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ExecuteShellCommandChange extends AbstractChange {
protected List<String> finalCommandArray;
private String executable;
private List<String> os;
private List<String> args = new ArrayList<String>();
private final List<String> args = new ArrayList<>();
private String timeout;
private static final Pattern TIMEOUT_PATTERN = Pattern.compile("^\\s*(\\d+)\\s*([sSmMhH]?)\\s*$");
private static final Long SECS_IN_MILLIS = 1000L;
Expand Down Expand Up @@ -190,8 +190,8 @@ protected void executeCommand(Database database) throws Exception {
int returnCode = 0;
try {
//output both stdout and stderr data from proc to stdout of this process
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), errorStream);
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), inputStream);
StreamGobbler errorGobbler = createErrorGobbler(p.getErrorStream(), errorStream);
StreamGobbler outputGobbler = createErrorGobbler(p.getInputStream(), inputStream);

errorGobbler.start();
outputGobbler.start();
Expand Down Expand Up @@ -225,6 +225,10 @@ protected void executeCommand(Database database) throws Exception {
processResult(returnCode, errorStreamOut, infoStreamOut, database);
}

protected StreamGobbler createErrorGobbler(InputStream processStream, OutputStream outputStream) {
return new StreamGobbler(processStream, outputStream, Thread.currentThread());
}

/**
* Max bytes to copy from output to {@link #processResult(int, String, String, Database)}. If null, process all output.
* @return
Expand All @@ -238,10 +242,8 @@ protected Integer getMaxStreamGobblerOutput() {
* <p>
* Creates a scheduled task to destroy the process in given timeout milliseconds.
* This killer task will be cancelled if the process returns before the timeout value.
*
* @param process
* @param process
* @param timeoutInMillis waits for specified timeoutInMillis before destroying the process.
* It will wait indefinitely if timeoutInMillis is 0.
*/
@java.lang.SuppressWarnings("squid:S2142")
private int waitForOrKill(final Process process, final long timeoutInMillis) throws TimeoutException {
Expand Down Expand Up @@ -273,6 +275,11 @@ public void run() {
}
} catch (InterruptedException ignore) {
// check again
if (timedOut.get()) {
timer.cancel();
String timeoutStr = timeout != null ? timeout : timeoutInMillis + " ms";
throw new TimeoutException("Process timed out (" + timeoutStr + ")");
}
}
}

Expand Down Expand Up @@ -368,22 +375,24 @@ protected void customLoadLogic(ParsedNode parsedNode, ResourceAccessor resourceA
}
}
}
private class StreamGobbler extends Thread {

public class StreamGobbler extends Thread {
private static final int THREAD_SLEEP_MILLIS = 100;
private final OutputStream outputStream;
private InputStream processStream;
boolean loggedTruncated = false;
long copiedSize = 0;
private final Thread parentThread;

private StreamGobbler(InputStream processStream, ByteArrayOutputStream outputStream) {
public StreamGobbler(InputStream processStream, OutputStream outputStream, Thread parentThread) {
this.processStream = processStream;
this.outputStream = outputStream;
this.parentThread = parentThread;
}

@Override
public void run() {
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(processStream);
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(processStream)) {
while (processStream != null) {
if (bufferedInputStream.available() > 0) {
copy(bufferedInputStream, outputStream);
Expand All @@ -396,7 +405,10 @@ public void run() {
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
Scope.getCurrentScope().getLog(ExecuteShellCommandChange.class).warning(ioe.getMessage());
if (parentThread != null) {
parentThread.interrupt();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package liquibase.changelog;

import liquibase.ContextExpression;
import liquibase.Labels;
import liquibase.RuntimeEnvironment;
import liquibase.Scope;
import liquibase.*;
import liquibase.changelog.filter.ChangeSetFilter;
import liquibase.changelog.filter.ChangeSetFilterResult;
import liquibase.changelog.visitor.ChangeSetVisitor;
import liquibase.changelog.visitor.SkippedChangeSetVisitor;
import liquibase.changelog.visitor.ValidatingVisitor;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.exception.LiquibaseException;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.exception.ValidationErrors;
Expand Down Expand Up @@ -94,9 +93,12 @@ public void run() throws Exception {
Scope.child(Scope.Attr.changeSet.name(), changeSet, () -> {
if (finalShouldVisit && !alreadySaw(changeSet)) {
//
// Go validate any change sets with an Executor
// Go validate any change sets with an Executor if
// we are using a ValidatingVisitor
//
validateChangeSetExecutor(changeSet, env);
if (visitor instanceof ValidatingVisitor) {
validateChangeSetExecutor(changeSet, env);
}

//
// Execute the visit call in its own scope with a new
Expand Down Expand Up @@ -137,7 +139,8 @@ private void validateChangeSetExecutor(ChangeSet changeSet, RuntimeEnvironment e
if (changeSet.getRunWith() == null) {
return;
}
String executorName = changeSet.getRunWith();
String executorName = ChangeSet.lookupExecutor(changeSet.getRunWith());

Executor executor;
try {
executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor(executorName, env.getTargetDatabase());
Expand Down
31 changes: 30 additions & 1 deletion liquibase-core/src/main/java/liquibase/changelog/ChangeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import liquibase.change.core.EmptyChange;
import liquibase.change.core.RawSQLChange;
import liquibase.changelog.visitor.ChangeExecListener;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.database.Database;
import liquibase.database.DatabaseList;
import liquibase.database.ObjectQuotingStrategy;
Expand Down Expand Up @@ -719,7 +720,8 @@ private Executor setupCustomExecutorIfNecessary(Database database) {
if (getRunWith() == null || originalExecutor instanceof LoggingExecutor) {
return originalExecutor;
}
Executor customExecutor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor(getRunWith(), database);
String executorName = ChangeSet.lookupExecutor(getRunWith());
Executor customExecutor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor(executorName, database);
Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor("jdbc", database, customExecutor);
List<Change> changes = getChanges();
for (Change change : changes) {
Expand All @@ -735,6 +737,33 @@ private Executor setupCustomExecutorIfNecessary(Database database) {
return originalExecutor;
}

/**
*
* Look for a configuration property that matches liquibase.<executor name>.executor
* and if found, return its value as the executor name
*
* @param executorName The value from the input changeset runWith attribute
* @return String The mapped value
*
*/
public static String lookupExecutor(String executorName) {
if (StringUtil.isEmpty(executorName)) {
return null;
}
String key = "liquibase." + executorName.toLowerCase() + ".executor";
String replacementExecutorName =
(String)Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class).getCurrentConfiguredValue(null, null, key).getValue();
if (replacementExecutorName != null) {
Scope.getCurrentScope().getLog(ChangeSet.class).info("Mapped '" + executorName + "' to executor '" + replacementExecutorName + "'");
return replacementExecutorName;
} else if (executorName.equalsIgnoreCase("native")) {
String message = "Unable to locate an executor for 'runWith=" + executorName + "'. You must specify a valid executor name.";
Scope.getCurrentScope().getLog(ChangeSet.class).warning(message);
Scope.getCurrentScope().getUI().sendErrorMessage("WARNING: " + message);
}
return executorName;
}

public void rollback(Database database) throws RollbackFailedException {
rollback(database, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public class ValidatingVisitor implements ChangeSetVisitor {
private Map<String, RanChangeSet> ranIndex;
private Database database;

//
// Added for test
//
public ValidatingVisitor() {
}

public ValidatingVisitor(List<RanChangeSet> ranChangeSets) {
ranIndex = new HashMap<>();
for(RanChangeSet changeSet:ranChangeSets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,18 @@ public ParsedNode removeChild(String namespace, String name) throws ParsedNodeEx
return this;
}

/**
*
* Match the namespace and name of this node. This is now done case-insensitively
*
* @param node
* @param namespace
* @param nodename
* @return boolean
*
*/
protected boolean nodeMatches(ParsedNode node, String namespace, String nodename) {
return namespaceMatches(node, namespace) && node.getName().equals(nodename);
return namespaceMatches(node, namespace) && node.getName().equalsIgnoreCase(nodename);
}

protected boolean namespaceMatches(ParsedNode node, String namespace) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#### _ _ _ _ _____
## | | (_) (_) | | __ \
## | | _ __ _ _ _ _| |__ __ _ ___ ___ | |__) | __ ___
## | | | |/ _` | | | | | '_ \ / _` / __|/ _ \ | ___/ '__/ _ \
## | |___| | (_| | |_| | | |_) | (_| \__ \ __/ | | | | | (_) |
## \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___| |_| |_| \___/
## | |
## |_|
##
## The liquibase.sqlcmd.conf file stores properties which are used during the
## execution of the Microsoft SQLCMD tool.
## Learn more: https://www.liquibase.org/documentation/config_properties.html
####
####
## Note about relative and absolute paths:
## The liquibase.sqlcmd.path must be a valid path to the SQLCMD executable.
## The liquibase.sqlcmd.timeout value can be one of:
## -1 - disable the timeout
## Any integer value > 0 (measured in seconds)
##
####

# The full path to the SQLCMD executable.
# Sample Linux path
# liquibase.sqlcmd.path=/opt/mssql-tools/bin/sqlcmd
# Sample Windows path
# liquibase.sqlcmd.path="C:\\Program Files\\Microsoft SQL Server\\Client SDK\\ODBC\\170\\Tools\\Binn\\SQLCMD.EXE"

# A valid timeout value for the execution of the SQLCMD tool
liquibase.sqlcmd.timeout=-1

# Flag to indicate whether or not to keep the temporary SQL file after execution of SQLCMD.
# True = keep False = delete (default)
liquibase.sqlcmd.keep.temp=true

# OPTIONAL Flag to designate the location to store temporary SQL file after execution of SQLCMD.
# Liquibase will attempt to use path exactly as entered, so please ensure it complies with your OS requirements.
# liquibase.sqlcmd.keep.temp.path=

# OPTIONAL Flag to designate the name of temporary SQL file after execution of SQLCMD.
# Liquibase will attempt to use the name exactly as entered, so please ensure it complies with your OS requirements.
# liquibase.sqlcmd.keep.temp.name=

# OPTIONAL Args to pass directly to SQLCMD.
# Learn about SQLCMD args at https://<link>
# Note: The delimiter for args is a space eg:" " and not "," or ";" separated.
# liquibase.sqlcmd.args=

# OPTIONAL Path to a log file for the SQLCMD output
# liquibase.sqlcmd.logFile=
#

# OPTIONAL Name of a custom executor to use instead of SQLCMD
# The Executor must be on the Liquibase classpath
# liquibase.sqlcmd.executor=
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
####

# The full path to the SQLPLUS executable.
# Sample linux path
# Sample Linux path
# liquibase.sqlplus.path=/apps/app/12.2.0.1.0/oracle/product/12.2.0.1.0/client_1/bin/sqlplus
# Sample windows path
# Sample Windows path
# liquibase.sqlplus.path=c:\\oracle\\product\\11.2.0\\client_1\\bin\\sqlplus.exe

# A valid timeout value for the execution of the SQLPLUS tool
Expand All @@ -45,3 +45,7 @@ liquibase.sqlplus.keep.temp=true
# Learn about SQLPLUS args at https://docs.oracle.com/cd/B10501_01/server.920/a90842/ch4.htm
# Note: The delimiter for args is a space eg:" " and not "," or ";" separated.
# liquibase.sqlplus.args=

# OPTIONAL Name of a custom executor to use instead of SQLPLUS
# The Executor must be on the Liquibase classpath
# liquibase.sqlplus.executor=
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#### _ _ _ _ _____
## | | (_) (_) | | __ \
## | | _ __ _ _ _ _| |__ __ _ ___ ___ | |__) | __ ___
## | | | |/ _` | | | | | '_ \ / _` / __|/ _ \ | ___/ '__/ _ \
## | |___| | (_| | |_| | | |_) | (_| \__ \ __/ | | | | | (_) |
## \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___| |_| |_| \___/
## | |
## |_|
##
## The liquibase.sqlcmd.conf file stores properties which are used during the
## execution of the Microsoft SQLCMD tool.
## Learn more: https://www.liquibase.org/documentation/config_properties.html
####
####
## Note about relative and absolute paths:
## The liquibase.sqlcmd.path must be a valid path to the SQLCMD executable.
## The liquibase.sqlcmd.timeout value can be one of:
## -1 - disable the timeout
## Any integer value > 0 (measured in seconds)
##
####

# The full path to the SQLCMD executable.
# Sample Linux path
# liquibase.sqlcmd.path=/opt/mssql-tools/bin/sqlcmd
# Sample Windows path
# liquibase.sqlcmd.path="C:\\Program Files\\Microsoft SQL Server\\Client SDK\\ODBC\\170\\Tools\\Binn\\SQLCMD.EXE"

# A valid timeout value for the execution of the SQLCMD tool
liquibase.sqlcmd.timeout=-1

# Flag to indicate whether or not to keep the temporary SQL file after execution of SQLCMD.
# True = keep False = delete (default)
liquibase.sqlcmd.keep.temp=true

# OPTIONAL Flag to designate the location to store temporary SQL file after execution of SQLCMD.
# Liquibase will attempt to use path exactly as entered, so please ensure it complies with your OS requirements.
# liquibase.sqlcmd.keep.temp.path=

# OPTIONAL Flag to designate the name of temporary SQL file after execution of SQLCMD.
# Liquibase will attempt to use the name exactly as entered, so please ensure it complies with your OS requirements.
# liquibase.sqlcmd.keep.temp.name=

# OPTIONAL Args to pass directly to SQLCMD.
# Learn about SQLCMD args at https://<link>
# Note: The delimiter for args is a space eg:" " and not "," or ";" separated.
# liquibase.sqlcmd.args=

# OPTIONAL Path to a log file for the SQLCMD output
# liquibase.sqlcmd.logFile=

# OPTIONAL Name of a custom executor to use instead of SQLCMD
# The Executor must be on the Liquibase classpath
# liquibase.sqlcmd.executor=
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
####

# The full path to the SQLPLUS executable.
# Sample linux path
# Sample Linux path
# liquibase.sqlplus.path=/apps/app/12.2.0.1.0/oracle/product/12.2.0.1.0/client_1/bin/sqlplus
# Sample windows path
# Sample Windows path
# liquibase.sqlplus.path=c:\\oracle\\product\\11.2.0\\client_1\\bin\\sqlplus.exe

# A valid timeout value for the execution of the SQLPLUS tool
Expand All @@ -45,3 +45,7 @@ liquibase.sqlplus.keep.temp=true
# Learn about SQLPLUS args at https://docs.oracle.com/cd/B10501_01/server.920/a90842/ch4.htm
# Note: The delimiter for args is a space eg:" " and not "," or ";" separated.
# liquibase.sqlplus.args=

# OPTIONAL Name of a custom executor to use instead of SQLPLUS
# The Executor must be on the Liquibase classpath
# liquibase.sqlplus.executor=
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ no.deployment.ids.found=No deployment IDs were located. No rollbacks were perfo
no.change.sets.found.for.deployment.id=No changesets were located matching deployment ID '%s'. No rollbacks were performed.
deployment.id.required=You must specify the deployment ID.
no.executor.pro.license.found=The Executor '%s' requires a Liquibase Pro license, available at https://www.liquibase.org/download or sales@liquibase.com. Options include the liquibase.pro.licenseKey in the defaults file, adding a flag in the CLI, and more. Learn more at https://docs.liquibase.com.
no.pro.license.found=The command '%s' requires a Liquibase Pro license, available at https://www.liquibase.org/download or sales@liquibase.com. Options include the liquibase.pro.licenseKey in the defaults file, adding a flag in the CLI, and more. Learn more at https://docs.liquibase.com.
no.parameter.pro.license.found=Using '%s' in the '%s' parameter requires a valid Liquibase Pro license.\nGet a free Pro license key at https://liquibase.com/protrial and add liquibase.pro.licenseKey=<yourKey> into your defaults file or\nuse --pro-license-key=<yourKey> before your command in the CLI.
no.pro.license.found=Using '%s' requires a valid Liquibase Pro license.\nGet a free Pro license key at https://liquibase.com/protrial and add liquibase.pro.licenseKey=<yourKey> into your defaults file or\nuse --pro-license-key=<yourKey> before your command in the CLI.
attempt.to.delete.the.file.failed.cannot.continue=Attempt to delete the file '%s' failed. Cannot continue. Sorry.
successfully.released.database.change.log.locks=Successfully released all database change log locks for '%s'
successfully.tagged=Successfully tagged '%s'
Expand Down

0 comments on commit 3632cfa

Please sign in to comment.