Search in sources :

Example 6 with HubChangeLog

use of liquibase.hub.model.HubChangeLog 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 7 with HubChangeLog

use of liquibase.hub.model.HubChangeLog in project liquibase by liquibase.

the class DropAllCommand method getHubChangeLog.

// 
// Return a HubChangeLog object if available
// If not available then return null
// If the HubChangeLog has been deleted then throw
// a LiquibaseHubException
// 
private HubChangeLog getHubChangeLog(DatabaseChangeLog changeLog) throws LiquibaseHubException {
    String apiKey = StringUtil.trimToNull(HubConfiguration.LIQUIBASE_HUB_API_KEY.getCurrentValue());
    HubConfiguration.HubMode hubMode = HubConfiguration.LIQUIBASE_HUB_MODE.getCurrentValue();
    String changeLogId = changeLog.getChangeLogId();
    final HubServiceFactory hubServiceFactory = Scope.getCurrentScope().getSingleton(HubServiceFactory.class);
    if (apiKey == null || hubMode == HubConfiguration.HubMode.OFF || !hubServiceFactory.isOnline()) {
        return null;
    }
    final HubService service = Scope.getCurrentScope().getSingleton(HubServiceFactory.class).getService();
    HubChangeLog hubChangeLog = (changeLogId != null ? service.getHubChangeLog(UUID.fromString(changeLogId), "*") : null);
    if (hubChangeLog == null) {
        return null;
    }
    // 
    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);
    }
    return hubChangeLog;
}
Also used : LiquibaseHubException(liquibase.hub.LiquibaseHubException) HubConfiguration(liquibase.hub.HubConfiguration) HubChangeLog(liquibase.hub.model.HubChangeLog) HubServiceFactory(liquibase.hub.HubServiceFactory) HubService(liquibase.hub.HubService)

Aggregations

HubChangeLog (liquibase.hub.model.HubChangeLog)7 HubService (liquibase.hub.HubService)6 HubServiceFactory (liquibase.hub.HubServiceFactory)6 LiquibaseHubException (liquibase.hub.LiquibaseHubException)4 CommandExecutionException (liquibase.exception.CommandExecutionException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 Change (liquibase.change.Change)2 ChangelogRewriter (liquibase.changelog.ChangelogRewriter)2 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)2 LiquibaseException (liquibase.exception.LiquibaseException)2 Connection (liquibase.hub.model.Connection)2 OperationChangeEvent (liquibase.hub.model.OperationChangeEvent)2 Project (liquibase.hub.model.Project)2 ChangeLogSerializer (liquibase.serializer.ChangeLogSerializer)2 Sql (liquibase.sql.Sql)2 UUID (java.util.UUID)1 Database (liquibase.database.Database)1 DatabaseConnection (liquibase.database.DatabaseConnection)1