Search in sources :

Example 6 with CommandScope

use of liquibase.command.CommandScope in project liquibase by liquibase.

the class Main method createLiquibaseCommand.

private CommandScope createLiquibaseCommand(Database database, Liquibase liquibase, String commandName, Map<String, Object> argsMap) throws LiquibaseException {
    argsMap.put("rollbackScript", rollbackScript);
    argsMap.put("changeLogFile", changeLogFile);
    argsMap.put("database", database);
    argsMap.put("liquibase", liquibase);
    if (!commandParams.contains("--help")) {
        argsMap.put("changeLog", liquibase.getDatabaseChangeLog());
    }
    ChangeLogParameters clp = new ChangeLogParameters(database);
    for (Map.Entry<String, Object> entry : changeLogParameters.entrySet()) {
        clp.set(entry.getKey(), entry.getValue());
    }
    argsMap.put("changeLogParameters", clp);
    if (this.commandParams.contains("--force") || this.commandParams.contains("--force=true")) {
        argsMap.put("force", Boolean.TRUE);
    }
    if (this.commandParams.contains("--help")) {
        argsMap.put("help", Boolean.TRUE);
    }
    if (liquibaseProLicenseKey != null) {
        argsMap.put("liquibaseProLicenseKey", liquibaseProLicenseKey);
    }
    String liquibaseHubApiKey = HubConfiguration.LIQUIBASE_HUB_API_KEY.getCurrentValue();
    if (liquibaseHubApiKey != null) {
        argsMap.put("liquibaseHubApiKey", liquibaseHubApiKey);
    }
    CommandScope liquibaseCommand = new CommandScope(commandName);
    for (Map.Entry<String, Object> entry : argsMap.entrySet()) {
        liquibaseCommand.addArgumentValue(entry.getKey(), entry.getValue());
    }
    return liquibaseCommand;
}
Also used : ChangeLogParameters(liquibase.changelog.ChangeLogParameters) CommandScope(liquibase.command.CommandScope)

Example 7 with CommandScope

use of liquibase.command.CommandScope in project liquibase by liquibase.

the class CommandLineUtils method doDiff.

public static void doDiff(Database referenceDatabase, Database targetDatabase, String snapshotTypes, CompareControl.SchemaComparison[] schemaComparisons, ObjectChangeFilter objectChangeFilter, PrintStream output) throws LiquibaseException {
    CommandScope diffCommand = createDiffCommand(referenceDatabase, targetDatabase, snapshotTypes, schemaComparisons, objectChangeFilter, output);
    Scope.getCurrentScope().getUI().sendMessage("");
    Scope.getCurrentScope().getUI().sendMessage(coreBundle.getString("diff.results"));
    try {
        diffCommand.execute();
    } catch (CommandExecutionException e) {
        throw new LiquibaseException(e);
    }
}
Also used : CommandExecutionException(liquibase.exception.CommandExecutionException) LiquibaseException(liquibase.exception.LiquibaseException) CommandScope(liquibase.command.CommandScope)

Example 8 with CommandScope

use of liquibase.command.CommandScope in project liquibase by liquibase.

the class HubUpdater method registerChangeLog.

// 
// Register the specified changelog
// 
private void registerChangeLog(UUID hubProjectId, DatabaseChangeLog changeLog, String changeLogFile) throws LiquibaseException {
    String apiKey = StringUtil.trimToNull(HubConfiguration.LIQUIBASE_HUB_API_KEY.getCurrentValue());
    if (apiKey == null) {
        throw new LiquibaseException("The command 'RegisterChangeLog' " + " failed because the Liquibase API Key has not been set. Learn more at https://hub.liquibase.com");
    }
    CommandScope registerChangeLogCommand = new CommandScope("registerChangeLog");
    registerChangeLogCommand.addArgumentValue(RegisterChangelogCommandStep.CHANGELOG_FILE_ARG, changeLogFile);
    try {
        if (hubProjectId != null) {
            try {
                registerChangeLogCommand.addArgumentValue(RegisterChangelogCommandStep.HUB_PROJECT_ID_ARG, hubProjectId);
            } catch (IllegalArgumentException e) {
                throw new LiquibaseException("The command 'RegisterChangeLog' " + " failed because parameter 'hubProjectId' has invalid value '" + hubProjectId + "'. Learn more at https://hub.liquibase.com");
            }
        }
    } catch (IllegalArgumentException e) {
        throw new LiquibaseException("Unexpected hubProjectId format: " + hubProjectId, e);
    }
    // 
    // Execute registerChangeLog and reset the changeLog ID
    // 
    CommandResults results = registerChangeLogCommand.execute();
    String registerChangeLogId = results.getResult(RegisterChangelogCommandStep.REGISTERED_CHANGELOG_ID);
    if (registerChangeLogId != null) {
        changeLog.setChangeLogId(registerChangeLogId);
    }
}
Also used : LiquibaseException(liquibase.exception.LiquibaseException) CommandScope(liquibase.command.CommandScope) CommandResults(liquibase.command.CommandResults)

Example 9 with CommandScope

use of liquibase.command.CommandScope in project liquibase by liquibase.

the class CommandRunner method call.

@Override
public CommandResults call() throws Exception {
    List<String> command = new ArrayList<>();
    command.add(spec.commandLine().getCommandName());
    CommandLine parentCommand = spec.commandLine().getParent();
    while (!parentCommand.getCommandName().equals("liquibase")) {
        command.add(0, parentCommand.getCommandName());
        parentCommand = parentCommand.getParent();
    }
    final String[] commandName = LiquibaseCommandLine.getCommandNames(spec.commandLine());
    for (int i = 0; i < commandName.length; i++) {
        commandName[i] = StringUtil.toCamelCase(commandName[i]);
    }
    final CommandScope commandScope = new CommandScope(commandName);
    final File outputFile = LiquibaseCommandLineConfiguration.OUTPUT_FILE.getCurrentValue();
    OutputStream outputStream = null;
    try {
        if (outputFile != null) {
            outputStream = new FileOutputStream(outputFile);
            commandScope.setOutput(outputStream);
        }
        return commandScope.execute();
    } catch (CommandValidationException cve) {
        Throwable cause = cve.getCause();
        if (cause instanceof MissingRequiredArgumentException) {
            // This is a list of the arguments which the init project command supports. The thinking here is that if the user
            // forgets to supply one of these arguments, we're going to remind them about the init project command, which
            // can help them figure out what they should be providing here.
            final Set<String> initProjectArguments = Stream.of(CommonArgumentNames.CHANGELOG_FILE, CommonArgumentNames.URL, CommonArgumentNames.USERNAME, CommonArgumentNames.PASSWORD).map(CommonArgumentNames::getArgumentName).collect(Collectors.toSet());
            throw new CommandValidationException(cve.getMessage() + (initProjectArguments.contains(((MissingRequiredArgumentException) cause).getArgumentName()) ? ". If you need to configure new liquibase project files and arguments, run the 'liquibase init project' command." : ""));
        } else {
            throw cve;
        }
    } finally {
        if (outputStream != null) {
            outputStream.flush();
            outputStream.close();
        }
    }
}
Also used : Set(java.util.Set) CommandValidationException(liquibase.exception.CommandValidationException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) CommandLine(picocli.CommandLine) MissingRequiredArgumentException(liquibase.exception.MissingRequiredArgumentException) FileOutputStream(java.io.FileOutputStream) CommandScope(liquibase.command.CommandScope) File(java.io.File) CommonArgumentNames(liquibase.command.CommonArgumentNames)

Example 10 with CommandScope

use of liquibase.command.CommandScope in project liquibase by liquibase.

the class DiffCommand method run.

@Override
public CommandResult run() throws Exception {
    final CommandScope commandScope = new CommandScope("diffInternal");
    commandScope.addArgumentValue(InternalDiffCommandStep.REFERENCE_DATABASE_ARG, this.referenceDatabase);
    commandScope.addArgumentValue(InternalDiffCommandStep.TARGET_DATABASE_ARG, this.targetDatabase);
    commandScope.addArgumentValue(InternalDiffCommandStep.SNAPSHOT_TYPES_ARG, this.snapshotTypes);
    commandScope.addArgumentValue(InternalDiffCommandStep.SNAPSHOT_LISTENER_ARG, this.snapshotListener);
    commandScope.addArgumentValue(InternalDiffCommandStep.REFERENCE_SNAPSHOT_CONTROL_ARG, this.referenceSnapshotControl);
    commandScope.addArgumentValue(InternalDiffCommandStep.TARGET_SNAPSHOT_CONTROL_ARG, this.targetSnapshotControl);
    commandScope.addArgumentValue(InternalDiffCommandStep.OBJECT_CHANGE_FILTER_ARG, this.objectChangeFilter);
    commandScope.addArgumentValue(InternalDiffCommandStep.COMPARE_CONTROL_ARG, this.compareControl);
    commandScope.setOutput(this.outputStream);
    commandScope.execute();
    return new CommandResult("OK");
}
Also used : CommandScope(liquibase.command.CommandScope) CommandResult(liquibase.command.CommandResult)

Aggregations

CommandScope (liquibase.command.CommandScope)19 LiquibaseException (liquibase.exception.LiquibaseException)6 Database (liquibase.database.Database)5 CommandResult (liquibase.command.CommandResult)4 CompareControl (liquibase.diff.compare.CompareControl)4 CommandExecutionException (liquibase.exception.CommandExecutionException)4 ChangeLogParameters (liquibase.changelog.ChangeLogParameters)2 CommandResults (liquibase.command.CommandResults)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 SQLException (java.sql.SQLException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 UUID (java.util.UUID)1 JarFile (java.util.jar.JarFile)1