Search in sources :

Example 6 with CommandExecutionException

use of liquibase.exception.CommandExecutionException in project liquibase by liquibase.

the class AbstractCliWrapperCommandStep method run.

@Override
public final void run(CommandResultsBuilder resultsBuilder) throws Exception {
    preRunCheck(resultsBuilder);
    final OutputStream outputStream = resultsBuilder.getOutputStream();
    PrintStream printStream = null;
    if (outputStream != null) {
        printStream = new PrintStream(outputStream);
        Main.setOutputStream(printStream);
    }
    CommandScope commandScope = resultsBuilder.getCommandScope();
    String[] args = collectArguments(commandScope);
    int statusCode = Main.run(args);
    if (statusCode != 0) {
        throw new CommandExecutionException("Unexpected error running liquibase");
    }
    resultsBuilder.addResult("statusCode", statusCode);
    if (printStream != null) {
        printStream.close();
    }
}
Also used : PrintStream(java.io.PrintStream) OutputStream(java.io.OutputStream) CommandExecutionException(liquibase.exception.CommandExecutionException)

Example 7 with CommandExecutionException

use of liquibase.exception.CommandExecutionException in project liquibase by liquibase.

the class InternalSyncHubCommandStep method run.

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
    CommandScope commandScope = resultsBuilder.getCommandScope();
    final HubServiceFactory hubServiceFactory = Scope.getCurrentScope().getSingleton(HubServiceFactory.class);
    if (!hubServiceFactory.isOnline()) {
        if (commandScope.getArgumentValue(FAIL_IF_OFFLINE_ARG)) {
            throw new CommandExecutionException("The command syncHub requires access to Liquibase Hub: " + hubServiceFactory.getOfflineReason() + ".  Learn more at https://hub.liquibase.com");
        } else {
            Scope.getCurrentScope().getUI().sendMessage("Sync skipped, offline");
            return;
        }
    }
    // 
    // Check for both connection and project specified
    // unless we have said to favor the connectionID
    // 
    final Boolean favorConnectionId = commandScope.getArgumentValue(CONTINUE_IF_CONNECTION_AND_PROJECT_ID_BOTH_SET_ARG);
    final UUID hubConnectionId = commandScope.getArgumentValue(HUB_CONNECTION_ID_ARG);
    if (!favorConnectionId && hubConnectionId != null && commandScope.getArgumentValue(HUB_PROJECT_ID_ARG) != null) {
        String message = "The syncHub command requires only one valid hubConnectionId or hubProjectId or unique URL. Please remove extra values.";
        Scope.getCurrentScope().getLog(getClass()).severe(message);
        throw new CommandExecutionException(message);
    }
    HubChangeLog hubChangeLog = null;
    final HubService hubService = Scope.getCurrentScope().getSingleton(HubServiceFactory.class).getService();
    Connection connectionToSync;
    if (hubConnectionId == null) {
        Project project = null;
        if (StringUtil.isNotEmpty(commandScope.getArgumentValue(CHANGELOG_FILE_ARG))) {
            final ResourceAccessor resourceAccessor = Scope.getCurrentScope().getResourceAccessor();
            final String changelogFile = commandScope.getArgumentValue(CHANGELOG_FILE_ARG);
            final DatabaseChangeLog databaseChangeLog = ChangeLogParserFactory.getInstance().getParser(changelogFile, resourceAccessor).parse(changelogFile, new ChangeLogParameters(), resourceAccessor);
            final String changeLogId = databaseChangeLog.getChangeLogId();
            if (changeLogId == null) {
                Scope.getCurrentScope().getLog(getClass()).info("Changelog " + changelogFile + " has not been registered with Liquibase Hub. Cannot use it to help determine project.");
            } else {
                hubChangeLog = hubService.getHubChangeLog(UUID.fromString(changeLogId), "*");
                if (hubChangeLog == null) {
                    throw new CommandExecutionException("Changelog " + changelogFile + " has an unrecognized changeLogId.");
                }
                if (hubChangeLog.isDeleted()) {
                    // 
                    // Complain and stop the operation
                    // 
                    String message = "\n" + "The operation did not complete and will not be reported to Hub because the\n" + "" + "registered changelog has been deleted by someone in your organization.\n" + "Learn more at http://hub.liquibase.com";
                    throw new LiquibaseHubException(message);
                }
                project = hubChangeLog.getProject();
            }
        } else if (commandScope.getArgumentValue(HUB_PROJECT_ID_ARG) != null) {
            project = hubService.getProject(commandScope.getArgumentValue(HUB_PROJECT_ID_ARG));
            if (project == null) {
                throw new CommandExecutionException("Project Id '" + commandScope.getArgumentValue(HUB_PROJECT_ID_ARG) + "' does not exist or you do not have access to it");
            }
        } else {
            Scope.getCurrentScope().getLog(getClass()).info("No project, connection, or changeLogFile specified. Searching for jdbcUrl across the entire organization.");
        }
        final String url = commandScope.getArgumentValue(URL_ARG);
        final Connection searchConnection = new Connection().setJdbcUrl(url).setProject(project);
        final List<Connection> connections = hubService.getConnections(searchConnection);
        if (connections.size() == 0) {
            if (project == null) {
                throw new CommandExecutionException("The url " + url + " does not match any defined connections. To auto-create a connection, please specify a 'changeLogFile=<changeLogFileName>' in liquibase.properties or the command line which contains a registered changeLogId.");
            }
            Connection inputConnection = new Connection();
            inputConnection.setJdbcUrl(url);
            inputConnection.setProject(project);
            connectionToSync = hubService.createConnection(inputConnection);
        } else if (connections.size() == 1) {
            connectionToSync = connections.get(0);
        } else {
            throw new CommandExecutionException("The url " + url + " is used by more than one connection. Please specify 'hubConnectionId=<hubConnectionId>' or 'changeLogFile=<changeLogFileName>' in liquibase.properties or the command line.");
        }
    } else {
        final List<Connection> connections = hubService.getConnections(new Connection().setId(hubConnectionId));
        if (connections.size() == 0) {
            throw new CommandExecutionException("Hub connection Id " + hubConnectionId + " was either not found, or you do not have access");
        } else {
            connectionToSync = connections.get(0);
        }
    }
    final Database database = commandScope.getArgumentValue(DATABASE_ARG);
    Scope.child(Scope.Attr.database, database, () -> {
        final ChangeLogHistoryService historyService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
        final List<RanChangeSet> ranChangeSets = historyService.getRanChangeSets();
        hubService.setRanChangeSets(connectionToSync, ranChangeSets);
    });
    // 
    if (hubChangeLog != null && hubChangeLog.isInactive()) {
        String message = "\n" + "The command completed and reported to Hub, but the changelog has been deactivated by someone in your organization.\n" + "To synchronize your changelog, checkout the latest from source control or run \"deactivatechangelog\".\n" + "After that, commands run against this changelog will not be reported to Hub until \"registerchangelog\" is run again.\n" + "Learn more at http://hub.liquibase.com";
        Scope.getCurrentScope().getLog(HubUpdater.class).warning(message);
        Scope.getCurrentScope().getUI().sendMessage("WARNING: " + message);
    }
}
Also used : ResourceAccessor(liquibase.resource.ResourceAccessor) Connection(liquibase.hub.model.Connection) HubServiceFactory(liquibase.hub.HubServiceFactory) Project(liquibase.hub.model.Project) LiquibaseHubException(liquibase.hub.LiquibaseHubException) HubUpdater(liquibase.hub.HubUpdater) HubChangeLog(liquibase.hub.model.HubChangeLog) Database(liquibase.database.Database) CommandExecutionException(liquibase.exception.CommandExecutionException) UUID(java.util.UUID) HubService(liquibase.hub.HubService)

Example 8 with CommandExecutionException

use of liquibase.exception.CommandExecutionException in project liquibase by liquibase.

the class RegisterChangelogCommandStep method run.

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
    CommandScope commandScope = resultsBuilder.getCommandScope();
    // 
    // Access the HubService
    // Stop if we do no have a key
    // 
    final HubServiceFactory hubServiceFactory = Scope.getCurrentScope().getSingleton(HubServiceFactory.class);
    if (!hubServiceFactory.isOnline()) {
        throw new CommandExecutionException("The command registerChangeLog requires communication with Liquibase Hub, \nwhich is prevented by liquibase.hub.mode='off'. \nPlease set to 'all' or 'meta' and try again.  \nLearn more at https://hub.liquibase.com");
    }
    // 
    // Check for existing changeLog file
    // 
    String changeLogFile = commandScope.getArgumentValue(CHANGELOG_FILE_ARG);
    UUID hubProjectId = commandScope.getArgumentValue(HUB_PROJECT_ID_ARG);
    String hubProjectName = commandScope.getArgumentValue(HUB_PROJECT_NAME_ARG);
    doRegisterChangelog(changeLogFile, hubProjectId, hubProjectName, resultsBuilder, false);
}
Also used : HubServiceFactory(liquibase.hub.HubServiceFactory) CommandExecutionException(liquibase.exception.CommandExecutionException)

Example 9 with CommandExecutionException

use of liquibase.exception.CommandExecutionException in project liquibase by liquibase.

the class RegisterChangelogCommandStep method retrieveOrCreateProject.

private Project retrieveOrCreateProject(HubService service, String changeLogFile, boolean skipPromptIfOneProject) throws CommandLineParsingException, LiquibaseException, LiquibaseHubException {
    final UIService ui = Scope.getCurrentScope().getUI();
    Project project = null;
    List<Project> projects = getProjectsFromHub();
    if (skipPromptIfOneProject && projects.size() == 1) {
        return projects.get(0);
    }
    boolean done = false;
    String input = null;
    while (!done) {
        input = readProjectFromConsole(projects, changeLogFile);
        try {
            if (input.equalsIgnoreCase("C")) {
                String projectName = readProjectNameFromConsole();
                if (StringUtil.isEmpty(projectName)) {
                    ui.sendMessage("\nNo project created\n");
                    continue;
                } else if (projectName.length() > 255) {
                    ui.sendMessage("\nThe project name you entered is longer than 255 characters\n");
                    continue;
                }
                project = service.createProject(new Project().setName(projectName));
                if (project == null) {
                    throw new CommandExecutionException("Unable to create project '" + projectName + "'.\n\n");
                }
                ui.sendMessage("\nProject '" + project.getName() + "' created with project ID '" + project.getId() + "'.\n");
                projects = getProjectsFromHub();
                done = true;
                continue;
            } else if (input.equalsIgnoreCase("N")) {
                throw new CommandExecutionException("Your changelog " + changeLogFile + " was not registered to any Liquibase Hub project. You can still run Liquibase commands, but no data will be saved in your Liquibase Hub account for monitoring or reports.  Learn more at https://hub.liquibase.com.");
            }
            int projectIdx = Integer.parseInt(input);
            if (projectIdx > 0 && projectIdx <= projects.size()) {
                project = projects.get(projectIdx - 1);
                if (project != null) {
                    done = true;
                }
            } else {
                ui.sendMessage("\nInvalid project '" + projectIdx + "' selected\n");
            }
        } catch (NumberFormatException nfe) {
            ui.sendMessage("\nInvalid selection '" + input + "'\n");
        }
    }
    return project;
}
Also used : Project(liquibase.hub.model.Project) UIService(liquibase.ui.UIService) CommandExecutionException(liquibase.exception.CommandExecutionException)

Example 10 with CommandExecutionException

use of liquibase.exception.CommandExecutionException in project liquibase by liquibase.

the class InternalDropAllCommandStep method validateConnectionAndProjectIdsDependingOnApiKey.

private void validateConnectionAndProjectIdsDependingOnApiKey(CommandScope commandScope) throws CommandExecutionException {
    UUID connectionId = commandScope.getArgumentValue(HUB_CONNECTION_ID_ARG);
    UUID projectId = commandScope.getArgumentValue(HUB_PROJECT_ID_ARG);
    String apiKey = StringUtil.trimToNull(HubConfiguration.LIQUIBASE_HUB_API_KEY.getCurrentValue());
    if (apiKey == null) {
        if (connectionId == null && projectId == null) {
            return;
        }
        throw new CommandExecutionException("No valid Hub API Key detected. Please add liquibase.hub.apikey to \n" + "defaults file or pass --hub-api-key=<yourkey> on the command line.");
    }
}
Also used : CommandExecutionException(liquibase.exception.CommandExecutionException) UUID(java.util.UUID)

Aggregations

CommandExecutionException (liquibase.exception.CommandExecutionException)13 LiquibaseException (liquibase.exception.LiquibaseException)5 CommandScope (liquibase.command.CommandScope)4 HubServiceFactory (liquibase.hub.HubServiceFactory)4 UUID (java.util.UUID)3 HubService (liquibase.hub.HubService)3 HubChangeLog (liquibase.hub.model.HubChangeLog)3 Project (liquibase.hub.model.Project)3 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 ChangelogRewriter (liquibase.changelog.ChangelogRewriter)2 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)2 CompareControl (liquibase.diff.compare.CompareControl)2 LockException (liquibase.exception.LockException)2 File (java.io.File)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 RandomAccessFile (java.io.RandomAccessFile)1 SQLException (java.sql.SQLException)1 CatalogAndSchema (liquibase.CatalogAndSchema)1