Search in sources :

Example 1 with HubServiceFactory

use of liquibase.hub.HubServiceFactory in project liquibase by liquibase.

the class MockHubService method reset.

public void reset() {
    randomUUID = UUID.randomUUID();
    this.returnProjects = new ArrayList<>(Collections.singletonList(new Project().setId(randomUUID).setName("Test project")));
    this.returnConnections = new ArrayList<>(Collections.singletonList(new Connection().setId(randomUUID).setJdbcUrl("jdbc://test").setProject(this.returnProjects.get(0))));
    this.returnChangeLogs = new ArrayList<>(Collections.singletonList(new HubChangeLog().setId(randomUUID).setName("Mock changelog").setFileName("com/example/test.xml").setProject(this.returnProjects.get(0))));
    HubChangeLog deletedChangeLog = new HubChangeLog().setId(deletedUUID).setName("Deleted changelog").setFileName("com/example/deleted.xml").setProject(this.returnProjects.get(0));
    deletedChangeLog.setStatus("deleted");
    this.returnChangeLogs.add(deletedChangeLog);
    HubChangeLog alreadyRegisteredChangeLog = new HubChangeLog().setId(alreadyRegisteredUUID).setName("Already registered changelog").setFileName("com/example/registered.xml").setProject(this.returnProjects.get(0));
    this.returnChangeLogs.add(alreadyRegisteredChangeLog);
    HubChangeLog notFoundChangeLog = new HubChangeLog().setId(notFoundChangeLogUUID).setName("Already registered changelog").setFileName("com/example/registered.xml").setProject(this.returnProjects.get(0));
    this.returnChangeLogs.add(notFoundChangeLog);
    this.sentObjects = new TreeMap<>();
    final HubServiceFactory hubServiceFactory = Scope.getCurrentScope().getSingleton(HubServiceFactory.class);
    hubServiceFactory.setOfflineReason("HubService is configured to be offline");
    online = true;
}
Also used : HubServiceFactory(liquibase.hub.HubServiceFactory)

Example 2 with HubServiceFactory

use of liquibase.hub.HubServiceFactory in project liquibase by liquibase.

the class DeactivateChangelogCommandStep method run.

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
    CommandScope commandScope = resultsBuilder.getCommandScope();
    try (PrintWriter output = new PrintWriter(resultsBuilder.getOutputStream())) {
        // 
        // 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 deactivateChangeLog 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);
        DatabaseChangeLog databaseChangeLog = parseChangeLogFile(changeLogFile);
        final String changeLogId = (databaseChangeLog != null ? databaseChangeLog.getChangeLogId() : null);
        if (changeLogId == null) {
            throw new CommandExecutionException("Changelog '" + changeLogFile + "' does not have a changelog ID and is not registered with Hub.\n" + "For more information visit https://docs.liquibase.com.");
        }
        final HubService service = Scope.getCurrentScope().getSingleton(HubServiceFactory.class).getService();
        HubChangeLog hubChangeLog = service.getHubChangeLog(UUID.fromString(changeLogId));
        if (hubChangeLog == null) {
            String message = "WARNING: Changelog '" + changeLogFile + "' has a changelog ID but was not found in Hub.\n" + "The changelog ID will be removed from the file, but Hub will not be updated.";
            Scope.getCurrentScope().getUI().sendMessage(message);
            Scope.getCurrentScope().getLog(DeactivateChangelogCommandStep.class).warning(message);
        } else {
            // 
            // Update Hub to deactivate the changelog
            // 
            hubChangeLog.setStatus("INACTIVE");
            hubChangeLog = service.deactivateChangeLog(hubChangeLog);
        }
        // 
        // Remove the changeLog Id and update the file
        // 
        ChangelogRewriter.ChangeLogRewriterResult rewriterResult = ChangelogRewriter.removeChangeLogId(changeLogFile, changeLogId, databaseChangeLog);
        String message = null;
        if (rewriterResult.success) {
            message = "The changelog '" + changeLogFile + "' was deactivated.\n" + "Note: If this is a shared changelog, please check it into Source Control.\n" + "Operation data sent to the now inactive changelogID will still be accepted at Hub.\n" + "For more information visit https://docs.liquibase.com.\n";
            Scope.getCurrentScope().getLog(DeactivateChangelogCommandStep.class).info(message);
            output.println(message);
            resultsBuilder.addResult("statusCode", 0);
            return;
        }
        throw new CommandExecutionException(rewriterResult.message);
    }
}
Also used : ChangelogRewriter(liquibase.changelog.ChangelogRewriter) HubChangeLog(liquibase.hub.model.HubChangeLog) HubServiceFactory(liquibase.hub.HubServiceFactory) CommandExecutionException(liquibase.exception.CommandExecutionException) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) HubService(liquibase.hub.HubService) PrintWriter(java.io.PrintWriter)

Example 3 with HubServiceFactory

use of liquibase.hub.HubServiceFactory 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 4 with HubServiceFactory

use of liquibase.hub.HubServiceFactory 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 5 with HubServiceFactory

use of liquibase.hub.HubServiceFactory 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

HubServiceFactory (liquibase.hub.HubServiceFactory)5 CommandExecutionException (liquibase.exception.CommandExecutionException)3 HubService (liquibase.hub.HubService)3 HubChangeLog (liquibase.hub.model.HubChangeLog)3 LiquibaseHubException (liquibase.hub.LiquibaseHubException)2 PrintWriter (java.io.PrintWriter)1 UUID (java.util.UUID)1 ChangelogRewriter (liquibase.changelog.ChangelogRewriter)1 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)1 Database (liquibase.database.Database)1 HubConfiguration (liquibase.hub.HubConfiguration)1 HubUpdater (liquibase.hub.HubUpdater)1 Connection (liquibase.hub.model.Connection)1 Project (liquibase.hub.model.Project)1 ResourceAccessor (liquibase.resource.ResourceAccessor)1