Search in sources :

Example 6 with DatabaseChangeLog

use of liquibase.changelog.DatabaseChangeLog 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)

Example 7 with DatabaseChangeLog

use of liquibase.changelog.DatabaseChangeLog in project liquibase by liquibase.

the class MarkChangeSetRanGeneratorTest method generateSqlSecondRunUpdatesLabelsContextsComments.

/**
 * Ensure that upon running an update on a changeset that has been run before, we still update the labels,
 * contexts and comments columns in the DBCL table.
 */
@Test
public void generateSqlSecondRunUpdatesLabelsContextsComments() {
    String changeSetContextExpression = "changeSetContext1 AND changeSetContext2";
    DatabaseChangeLog rootChangeLog = new DatabaseChangeLog();
    rootChangeLog.setContexts(new ContextExpression("rootContext1 OR (rootContext2) AND (rootContext3)"));
    DatabaseChangeLog childChangeLog = new DatabaseChangeLog();
    childChangeLog.setContexts(new ContextExpression("childChangeLogContext1, childChangeLogContext2 AND childChangeLogContext3"));
    childChangeLog.setIncludeContexts(new ContextExpression("includeContext1, includeContext2 AND includeContext3"));
    childChangeLog.setParentChangeLog(rootChangeLog);
    ChangeSet changeSet = new ChangeSet("1", "a", false, false, "c", changeSetContextExpression, null, childChangeLog);
    changeSet.setComments("comment12345");
    changeSet.setLabels(new Labels("newlabel123"));
    Sql[] sqls = new MarkChangeSetRanGenerator().generateSql(new MarkChangeSetRanStatement(changeSet, ChangeSet.ExecType.RERAN), new MockDatabase(), new MockSqlGeneratorChain());
    String sql = sqls[0].toSql();
    assertTrue(sql.contains("COMMENTS = 'comment12345'"));
    assertTrue(sql.contains("CONTEXTS = '(childChangeLogContext1, childChangeLogContext2 AND childChangeLogContext3) AND (includeContext1, includeContext2 AND includeContext3) AND (rootContext1 OR (rootContext2) AND (rootContext3)) AND (changeSetContext1 AND changeSetContext2)'"));
    assertTrue(sql.contains("LABELS = 'newlabel123'"));
}
Also used : ContextExpression(liquibase.ContextExpression) MarkChangeSetRanStatement(liquibase.statement.core.MarkChangeSetRanStatement) MockDatabase(liquibase.database.core.MockDatabase) Labels(liquibase.Labels) ChangeSet(liquibase.changelog.ChangeSet) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) Sql(liquibase.sql.Sql) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 8 with DatabaseChangeLog

use of liquibase.changelog.DatabaseChangeLog in project liquibase by liquibase.

the class MarkChangeSetRanGeneratorTest method generateSqlWithComplexContext.

@Test
public void generateSqlWithComplexContext() {
    String changeSetContextExpression = "changeSetContext1 AND changeSetContext2";
    DatabaseChangeLog rootChangeLog = new DatabaseChangeLog();
    rootChangeLog.setContexts(new ContextExpression("rootContext1 OR (rootContext2) AND (rootContext3)"));
    DatabaseChangeLog childChangeLog = new DatabaseChangeLog();
    childChangeLog.setContexts(new ContextExpression("childChangeLogContext1, childChangeLogContext2 AND childChangeLogContext3"));
    childChangeLog.setIncludeContexts(new ContextExpression("includeContext1, includeContext2 AND includeContext3"));
    childChangeLog.setParentChangeLog(rootChangeLog);
    ChangeSet changeSet = new ChangeSet("1", "a", false, false, "c", changeSetContextExpression, null, childChangeLog);
    Sql[] sqls = new MarkChangeSetRanGenerator().generateSql(new MarkChangeSetRanStatement(changeSet, ChangeSet.ExecType.EXECUTED), new MockDatabase(), new MockSqlGeneratorChain());
    assertTrue(sqls[0].toSql(), sqls[0].toSql().contains("(childChangeLogContext1, childChangeLogContext2 AND childChangeLogContext3) AND " + "(includeContext1, includeContext2 AND includeContext3) AND " + "(rootContext1 OR (rootContext2) AND (rootContext3)) AND " + "(changeSetContext1 AND changeSetContext2)"));
}
Also used : ContextExpression(liquibase.ContextExpression) MarkChangeSetRanStatement(liquibase.statement.core.MarkChangeSetRanStatement) MockDatabase(liquibase.database.core.MockDatabase) ChangeSet(liquibase.changelog.ChangeSet) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) Sql(liquibase.sql.Sql) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 9 with DatabaseChangeLog

use of liquibase.changelog.DatabaseChangeLog in project liquibase by liquibase.

the class ConvertCommandStep method run.

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
    CommandScope commandScope = resultsBuilder.getCommandScope();
    String src = commandScope.getArgumentValue(SRC_ARG);
    String out = commandScope.getArgumentValue(OUT_ARG);
    String classpath = commandScope.getArgumentValue(CLASSPATH_ARG);
    List<ResourceAccessor> openers = new ArrayList<>();
    openers.add(new FileSystemResourceAccessor());
    openers.add(new ClassLoaderResourceAccessor());
    if (classpath != null) {
        openers.add(new FileSystemResourceAccessor(new File(classpath)));
    }
    ResourceAccessor resourceAccessor = new CompositeResourceAccessor(openers);
    ChangeLogParser sourceParser = ChangeLogParserFactory.getInstance().getParser(src, resourceAccessor);
    ChangeLogSerializer outSerializer = ChangeLogSerializerFactory.getInstance().getSerializer(out);
    DatabaseChangeLog changeLog = sourceParser.parse(src, new ChangeLogParameters(), resourceAccessor);
    File outFile = new File(out);
    if (!outFile.exists()) {
        outFile.getParentFile().mkdirs();
    }
    try (FileOutputStream outputStream = new FileOutputStream(outFile)) {
        outSerializer.write(changeLog.getChangeSets(), outputStream);
    }
    Scope.getCurrentScope().getUI().sendMessage("Converted successfully");
}
Also used : CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ArrayList(java.util.ArrayList) ChangeLogSerializer(liquibase.serializer.ChangeLogSerializer) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) ChangeLogParser(liquibase.parser.ChangeLogParser) FileOutputStream(java.io.FileOutputStream) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) File(java.io.File)

Example 10 with DatabaseChangeLog

use of liquibase.changelog.DatabaseChangeLog in project liquibase by liquibase.

the class SqlChangeLogParser method parse.

@Override
public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
    DatabaseChangeLog changeLog = new DatabaseChangeLog();
    changeLog.setPhysicalFilePath(physicalChangeLogLocation);
    RawSQLChange change = new RawSQLChange();
    try {
        InputStream sqlStream = resourceAccessor.openStream(null, physicalChangeLogLocation);
        if (sqlStream != null) {
            String sql = StreamUtil.readStreamAsString(sqlStream);
            change.setSql(sql);
        } else {
            throw new ChangeLogParseException(FileUtil.getFileNotFoundMessage(physicalChangeLogLocation));
        }
    } catch (IOException e) {
        throw new ChangeLogParseException(e);
    }
    change.setSplitStatements(false);
    change.setStripComments(false);
    ChangeSet changeSet = new ChangeSet("raw", "includeAll", false, false, physicalChangeLogLocation, null, null, true, ObjectQuotingStrategy.LEGACY, changeLog);
    changeSet.addChange(change);
    changeLog.addChangeSet(changeSet);
    return changeLog;
}
Also used : RawSQLChange(liquibase.change.core.RawSQLChange) InputStream(java.io.InputStream) ChangeLogParseException(liquibase.exception.ChangeLogParseException) IOException(java.io.IOException) ChangeSet(liquibase.changelog.ChangeSet) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog)

Aggregations

DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)23 ChangeSet (liquibase.changelog.ChangeSet)11 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)6 RanChangeSet (liquibase.changelog.RanChangeSet)6 LiquibaseException (liquibase.exception.LiquibaseException)6 IOException (java.io.IOException)5 CreateTableChange (liquibase.change.core.CreateTableChange)5 ChangeLogParameters (liquibase.changelog.ChangeLogParameters)5 Database (liquibase.database.Database)5 MockDatabase (liquibase.database.core.MockDatabase)5 ChangeLogParseException (liquibase.exception.ChangeLogParseException)4 ContextExpression (liquibase.ContextExpression)3 Labels (liquibase.Labels)3 Liquibase (liquibase.Liquibase)3 ColumnConfig (liquibase.change.ColumnConfig)3 CommandExecutionException (liquibase.exception.CommandExecutionException)3 CompositeResourceAccessor (liquibase.resource.CompositeResourceAccessor)3 FileSystemResourceAccessor (liquibase.resource.FileSystemResourceAccessor)3 ResourceAccessor (liquibase.resource.ResourceAccessor)3