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);
}
}
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;
}
Aggregations