Search in sources :

Example 1 with ChangeLogAlreadyRegisteredException

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

the class RegisterChangelogCommandStep method doRegisterChangelog.

public void doRegisterChangelog(String changelogFilepath, UUID hubProjectId, String hubProjectName, CommandResultsBuilder resultsBuilder, boolean skipPromptIfOneProject) throws LiquibaseException, CommandLineParsingException {
    try (PrintWriter output = new PrintWriter(resultsBuilder.getOutputStream())) {
        HubChangeLog hubChangeLog;
        final HubService service = Scope.getCurrentScope().getSingleton(HubServiceFactory.class).getService();
        // 
        // Check for existing changeLog file using the untouched changelog filepath
        // 
        DatabaseChangeLog databaseChangeLog = parseChangeLogFile(changelogFilepath);
        if (databaseChangeLog == null) {
            throw new CommandExecutionException("Cannot parse " + changelogFilepath);
        }
        final String changeLogId = databaseChangeLog.getChangeLogId();
        if (changeLogId != null) {
            hubChangeLog = service.getHubChangeLog(UUID.fromString(changeLogId));
            if (hubChangeLog != null) {
                throw new CommandExecutionException("Changelog '" + changelogFilepath + "' is already registered with changeLogId '" + changeLogId + "' to project '" + hubChangeLog.getProject().getName() + "' with project ID '" + hubChangeLog.getProject().getId().toString() + "'.\n" + "For more information visit https://docs.liquibase.com.", new ChangeLogAlreadyRegisteredException(hubChangeLog));
            } else {
                throw new CommandExecutionException("Changelog '" + changelogFilepath + "' is already registered with changeLogId '" + changeLogId + "'.\n" + "For more information visit https://docs.liquibase.com.", new ChangeLogAlreadyRegisteredException());
            }
        }
        // 
        // Retrieve the projects
        // 
        Project project;
        if (hubProjectId != null) {
            project = service.getProject(hubProjectId);
            if (project == null) {
                throw new CommandExecutionException("Project Id '" + hubProjectId + "' does not exist or you do not have access to it");
            }
        } else if (hubProjectName != null) {
            if (hubProjectName.length() > 255) {
                throw new CommandExecutionException("\nThe project name you gave is longer than 255 characters\n\n");
            }
            project = service.createProject(new Project().setName(hubProjectName));
            if (project == null) {
                throw new CommandExecutionException("Unable to create project '" + hubProjectName + "'.\n" + "Learn more at https://hub.liquibase.com.");
            }
            output.print("\nProject '" + project.getName() + "' created with project ID '" + project.getId() + "'.\n\n");
        } else {
            project = retrieveOrCreateProject(service, changelogFilepath, skipPromptIfOneProject);
            if (project == null) {
                throw new CommandExecutionException("Your changelog " + changelogFilepath + " 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.");
            }
        }
        // 
        // Go create the Hub Changelog
        // 
        String changelogFilename = Paths.get(databaseChangeLog.getFilePath()).getFileName().toString();
        HubChangeLog newChangeLog = new HubChangeLog();
        newChangeLog.setProject(project);
        newChangeLog.setFileName(changelogFilename);
        newChangeLog.setName(changelogFilename);
        hubChangeLog = service.createChangeLog(newChangeLog);
        // 
        // Update the changelog file
        // Add the registered changelog ID to the results so that
        // the caller can use it
        // 
        ChangelogRewriter.ChangeLogRewriterResult changeLogRewriterResult = ChangelogRewriter.addChangeLogId(changelogFilepath, hubChangeLog.getId().toString(), databaseChangeLog);
        if (changeLogRewriterResult.success) {
            Scope.getCurrentScope().getLog(RegisterChangelogCommandStep.class).info(changeLogRewriterResult.message);
            output.println("* Changelog file '" + changelogFilepath + "' with changelog ID '" + hubChangeLog.getId().toString() + "' has been " + "registered to Project " + project.getName());
            resultsBuilder.addResult("statusCode", 0);
            resultsBuilder.addResult(REGISTERED_CHANGELOG_ID.getName(), hubChangeLog.getId().toString());
        }
    }
}
Also used : Project(liquibase.hub.model.Project) ChangelogRewriter(liquibase.changelog.ChangelogRewriter) ChangeLogAlreadyRegisteredException(liquibase.exception.ChangeLogAlreadyRegisteredException) HubChangeLog(liquibase.hub.model.HubChangeLog) HubServiceFactory(liquibase.hub.HubServiceFactory) CommandExecutionException(liquibase.exception.CommandExecutionException) HubService(liquibase.hub.HubService) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) PrintWriter(java.io.PrintWriter)

Aggregations

PrintWriter (java.io.PrintWriter)1 ChangelogRewriter (liquibase.changelog.ChangelogRewriter)1 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)1 ChangeLogAlreadyRegisteredException (liquibase.exception.ChangeLogAlreadyRegisteredException)1 CommandExecutionException (liquibase.exception.CommandExecutionException)1 HubService (liquibase.hub.HubService)1 HubServiceFactory (liquibase.hub.HubServiceFactory)1 HubChangeLog (liquibase.hub.model.HubChangeLog)1 Project (liquibase.hub.model.Project)1