Search in sources :

Example 6 with ChangeSet

use of liquibase.changelog.ChangeSet in project stdlib by petergeneric.

the class LiquibaseCore method executeAction.

/**
 * Executes the Liquibase update.
 */
private static void executeAction(InitialContext jndi, GuiceApplicationValueContainer config, Map<String, String> parameters, LiquibaseAction action) throws NamingException, SQLException, LiquibaseException, IOException, ParserConfigurationException {
    // N.B. liquibase may create a databasechangeloglock / databasechangelog table if one does not already exist
    if (action.isWriteAction() && StringUtils.equalsIgnoreCase("true", config.getValue(HIBERNATE_IS_READONLY))) {
        log.info("Changing liquibase action from " + action + " to ASSERT_UPDATED because hibernate is set to read only mode");
        action = LiquibaseAction.ASSERT_UPDATED;
    }
    // Fail if hbm2ddl is enabled (Hibernate should not be involved in schema management)
    if (StringUtils.isNotEmpty(config.getValue(HIBERNATE_SCHEMA_MANAGEMENT)) && action != LiquibaseAction.GENERATE_CHANGELOG) {
        throw new RuntimeException("Liquibase is enabled but so is " + HIBERNATE_SCHEMA_MANAGEMENT + ". Only one of these schema management methods may be used at a time.");
    }
    final String dataSourceName = config.getDataSource();
    final String changeLogFile = config.getValue(GuiceProperties.LIQUIBASE_CHANGELOG);
    final String contexts = config.getValue(GuiceProperties.LIQUIBASE_CONTEXTS);
    final String labels = config.getValue(GuiceProperties.LIQUIBASE_LABELS);
    final String defaultSchema = config.getDefaultSchema();
    final String jdbcUrl = config.getValue(AvailableSettings.URL);
    final String jdbcUsername = config.getValue(AvailableSettings.USER);
    final String jdbcPassword = config.getValue(AvailableSettings.PASS);
    if (StringUtils.isEmpty(dataSourceName) && StringUtils.isEmpty(jdbcUrl))
        throw new RuntimeException("Cannot run Liquibase: no JNDI datasource or JDBC URL set");
    else if (changeLogFile == null)
        throw new RuntimeException("Cannot run Liquibase: " + GuiceProperties.LIQUIBASE_CHANGELOG + " is not set");
    int storedTransactionIsolation = Integer.MIN_VALUE;
    Connection connection = null;
    Database database = null;
    try {
        // Set up the resource accessor
        final ResourceAccessor resourceAccessor;
        {
            final CompositeResourceAccessor composite;
            {
                ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
                ResourceAccessor threadClFO = new ClassLoaderResourceAccessor(contextClassLoader);
                ResourceAccessor clFO = new ClassLoaderResourceAccessor();
                ResourceAccessor fsFO = new FileSystemResourceAccessor();
                composite = new CompositeResourceAccessor(clFO, fsFO, threadClFO);
            }
            // If loading a resource with an absolute path fails, re-try it as a path relative to /
            // This is for unit tests where /liquibase/changelog.xml needs to be accessed as liquibase/changelog.xml
            final ResourceAccessor fallback = new RetryAbsoluteAsRelativeResourceAccessor(composite);
            // Wrap the resource accessor in a filter that interprets ./ as the changeLogFile folder
            resourceAccessor = new RelativePathFilteringResourceAccessor(fallback, changeLogFile);
        }
        // Set up the database
        {
            if (StringUtils.isNotEmpty(dataSourceName)) {
                if (log.isDebugEnabled())
                    log.debug("Look up datasource for liquibase: " + dataSourceName);
                final DataSource dataSource = (DataSource) jndi.lookup(dataSourceName);
                connection = dataSource.getConnection();
            } else {
                if (log.isDebugEnabled())
                    log.debug("Create JDBC Connection directly: " + jdbcUrl);
                // N.B. do we need to call Class.forName on the JDBC Driver URL?
                // JDBC drivers should expose themselves using the service provider interface nowadays so this shouldn't be necessary
                connection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);
            }
            // Allow changing the transaction isolation away from the default for liquibase
            // This is a hack inserted for SQL Server where the SNAPSHOT isolation is being used
            // because in this mode it refuses to execute certain DDL statements because of metadata not being versioned
            {
                storedTransactionIsolation = connection.getTransactionIsolation();
                // In this case we change to READ UNCOMMITTED for the duration of the liquibase run
                if (storedTransactionIsolation == 4096) {
                    connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
                }
            }
            database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
            database.setDefaultSchemaName(defaultSchema);
        }
        Liquibase liquibase = new Liquibase(changeLogFile, resourceAccessor, database);
        for (Map.Entry<String, String> param : parameters.entrySet()) {
            liquibase.setChangeLogParameter(param.getKey(), param.getValue());
        }
        if (log.isDebugEnabled())
            log.debug("Execute liquibase action: " + action);
        switch(action) {
            case ASSERT_UPDATED:
                // Figure out which changesets need to be run
                List<ChangeSet> unrun = liquibase.listUnrunChangeSets(new Contexts(contexts), new LabelExpression(labels));
                if (log.isDebugEnabled())
                    log.debug("Pending changesets: " + unrun);
                // If any need to be run, fail
                if (unrun.size() > 0)
                    throw new LiquibaseChangesetsPending(unrun);
                else
                    return;
            case UPDATE:
                // Perform a schema update
                liquibase.update(new Contexts(contexts), new LabelExpression(labels));
                return;
            case MARK_UPDATED:
                // Mark all pending changesets as run
                liquibase.changeLogSync(new Contexts(contexts), new LabelExpression(labels));
                return;
            case GENERATE_CHANGELOG:
                CatalogAndSchema catalogueAndSchema = CatalogAndSchema.DEFAULT;
                DiffToChangeLog writer = new DiffToChangeLog(new DiffOutputControl(false, false, false, new CompareControl.SchemaComparison[0]));
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                PrintStream pw = new PrintStream(bos);
                liquibase.generateChangeLog(catalogueAndSchema, writer, pw);
                System.out.println("********** GENERATED CHANGELOG START **********");
                System.out.println(new String(bos.toByteArray()));
                System.out.println("********** GENERATED CHANGELOG END **********");
                break;
            default:
                throw new RuntimeException("Unknown liquibase action: " + action);
        }
    } finally {
        // N.B. we don't return to isolations < 0 (isolation at db defaults)
        if (connection != null && connection.getTransactionIsolation() != storedTransactionIsolation && storedTransactionIsolation >= 0) {
            connection.setTransactionIsolation(storedTransactionIsolation);
        }
        if (database != null)
            database.close();
        else if (connection != null)
            connection.close();
    }
}
Also used : CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) JdbcConnection(liquibase.database.jvm.JdbcConnection) Contexts(liquibase.Contexts) Database(liquibase.database.Database) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) ChangeSet(liquibase.changelog.ChangeSet) PrintStream(java.io.PrintStream) LiquibaseChangesetsPending(com.peterphi.std.guice.liquibase.exception.LiquibaseChangesetsPending) Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection) DiffOutputControl(liquibase.diff.output.DiffOutputControl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CatalogAndSchema(liquibase.CatalogAndSchema) DataSource(javax.sql.DataSource) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) Liquibase(liquibase.Liquibase) LabelExpression(liquibase.LabelExpression) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with ChangeSet

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

the class StandardHubService method sendOperationChanges.

@Override
public void sendOperationChanges(OperationChange operationChange) throws LiquibaseHubException {
    List<HubChange> hubChangeList = new ArrayList<>();
    for (ChangeSet changeSet : operationChange.getChangeSets()) {
        hubChangeList.add(new HubChange(changeSet));
    }
    http.doPost("/api/v1" + "/organizations/" + getOrganization().getId().toString() + "/projects/" + operationChange.getProject().getId().toString() + "/operations/" + operationChange.getOperation().getId().toString() + "/changes", hubChangeList, ArrayList.class);
}
Also used : RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet)

Example 8 with ChangeSet

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

the class ValidationFailedException method getMessage.

@Override
public String getMessage() {
    StringBuilder message = new StringBuilder();
    String separator = StreamUtil.getLineSeparator();
    message.append(coreBundle.getString("validation.failed")).append(separator);
    if (!invalidMD5Sums.isEmpty()) {
        message.append(INDENT_SPACES).append(String.format(coreBundle.getString("check.sum.changed"), invalidMD5Sums.size())).append(separator);
        for (int i = 0; i < invalidMD5Sums.size(); i++) {
            if (i > 25) {
                break;
            }
            message.append("          ").append(invalidMD5Sums.get(i));
            message.append(separator);
        }
    }
    if (!failedPreconditions.isEmpty()) {
        message.append(INDENT_SPACES).append(String.format(coreBundle.getString("preconditions.failed"), failedPreconditions.size())).append(separator);
        for (FailedPrecondition invalid : failedPreconditions) {
            message.append(INDENT_SPACES).append(invalid.toString());
            message.append(separator);
        }
    }
    if (!errorPreconditions.isEmpty()) {
        message.append(INDENT_SPACES).append(String.format(coreBundle.getString("preconditions.generated.error"), errorPreconditions.size())).append(separator);
        for (ErrorPrecondition invalid : errorPreconditions) {
            message.append(INDENT_SPACES).append(invalid.toString());
            message.append(separator);
        }
    }
    if (!duplicateChangeSets.isEmpty()) {
        message.append(INDENT_SPACES).append(String.format(coreBundle.getString("change.sets.duplicate.identifiers"), duplicateChangeSets.size())).append(separator);
        for (ChangeSet invalid : duplicateChangeSets) {
            message.append("          ").append(invalid.toString(false));
            message.append(separator);
        }
    }
    if (!setupExceptions.isEmpty()) {
        message.append(INDENT_SPACES).append(String.format(coreBundle.getString("changes.have.failures"), setupExceptions.size())).append(separator);
        for (SetupException invalid : setupExceptions) {
            message.append("          ").append(invalid.toString());
            message.append(separator);
        }
    }
    if (!changeValidationExceptions.isEmpty()) {
        message.append(INDENT_SPACES).append(String.format(coreBundle.getString("changes.have.validation.errors"), changeValidationExceptions.size())).append(separator);
        for (Throwable invalid : changeValidationExceptions) {
            Scope.getCurrentScope().getLog(getClass()).fine(coreBundle.getString("validation.exception"), invalid);
            message.append("          ").append(invalid.toString());
            message.append(separator);
        }
    }
    if (validationErrors.hasErrors()) {
        message.append(INDENT_SPACES).append(String.format(coreBundle.getString("changes.have.validation.failures"), validationErrors.getErrorMessages().size())).append(separator);
        for (String invalid : validationErrors.getErrorMessages()) {
            message.append("          ").append(invalid);
            message.append(separator);
        }
    }
    return message.toString();
}
Also used : ErrorPrecondition(liquibase.precondition.ErrorPrecondition) FailedPrecondition(liquibase.precondition.FailedPrecondition) ChangeSet(liquibase.changelog.ChangeSet)

Example 9 with ChangeSet

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

the class FormattedSqlChangeLogParser method parse.

@Override
public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
    DatabaseChangeLog changeLog = new DatabaseChangeLog();
    changeLog.setChangeLogParameters(changeLogParameters);
    changeLog.setPhysicalFilePath(physicalChangeLogLocation);
    try (BufferedReader reader = new BufferedReader(StreamUtil.readStreamWithReader(openChangeLogFile(physicalChangeLogLocation, resourceAccessor), null))) {
        StringBuilder currentSql = new StringBuilder();
        StringBuilder currentRollbackSql = new StringBuilder();
        ChangeSet changeSet = null;
        RawSQLChange change = null;
        Pattern changeLogPattern = Pattern.compile("\\-\\-\\s*liquibase formatted.*", Pattern.CASE_INSENSITIVE);
        Pattern propertyPattern = Pattern.compile("\\s*\\-\\-[\\s]*property\\s+(.*:.*)\\s+(.*:.*).*", Pattern.CASE_INSENSITIVE);
        Pattern changeSetPattern = Pattern.compile("\\s*\\-\\-[\\s]*changeset\\s+(\"[^\"]+\"|[^:]+):\\s*(\"[^\"]+\"|\\S+).*", Pattern.CASE_INSENSITIVE);
        Pattern rollbackPattern = Pattern.compile("\\s*\\-\\-[\\s]*rollback (.*)", Pattern.CASE_INSENSITIVE);
        Pattern preconditionsPattern = Pattern.compile("\\s*\\-\\-[\\s]*preconditions(.*)", Pattern.CASE_INSENSITIVE);
        Pattern preconditionPattern = Pattern.compile("\\s*\\-\\-[\\s]*precondition\\-([a-zA-Z0-9-]+) (.*)", Pattern.CASE_INSENSITIVE);
        Pattern stripCommentsPattern = Pattern.compile(".*stripComments:(\\w+).*", Pattern.CASE_INSENSITIVE);
        Pattern splitStatementsPattern = Pattern.compile(".*splitStatements:(\\w+).*", Pattern.CASE_INSENSITIVE);
        Pattern rollbackSplitStatementsPattern = Pattern.compile(".*rollbackSplitStatements:(\\w+).*", Pattern.CASE_INSENSITIVE);
        Pattern endDelimiterPattern = Pattern.compile(".*endDelimiter:(\\S*).*", Pattern.CASE_INSENSITIVE);
        Pattern rollbackEndDelimiterPattern = Pattern.compile(".*rollbackEndDelimiter:(\\S*).*", Pattern.CASE_INSENSITIVE);
        Pattern commentPattern = Pattern.compile("\\-\\-[\\s]*comment:? (.*)", Pattern.CASE_INSENSITIVE);
        Pattern validCheckSumPattern = Pattern.compile("\\-\\-[\\s]*validCheckSum:? (.*)", Pattern.CASE_INSENSITIVE);
        Pattern ignoreLinesPattern = Pattern.compile("\\-\\-[\\s]*ignoreLines:(\\w+)", Pattern.CASE_INSENSITIVE);
        Pattern runWithPattern = Pattern.compile(".*runWith:([\\w\\$\\{\\}]+).*", Pattern.CASE_INSENSITIVE);
        Pattern runOnChangePattern = Pattern.compile(".*runOnChange:(\\w+).*", Pattern.CASE_INSENSITIVE);
        Pattern runAlwaysPattern = Pattern.compile(".*runAlways:(\\w+).*", Pattern.CASE_INSENSITIVE);
        Pattern contextPattern = Pattern.compile(".*context:(\".*\"|\\S*).*", Pattern.CASE_INSENSITIVE);
        Pattern logicalFilePathPattern = Pattern.compile(".*logicalFilePath:(\\S*).*", Pattern.CASE_INSENSITIVE);
        Pattern changeLogIdPattern = Pattern.compile(".*changeLogId:(\\S*).*", Pattern.CASE_INSENSITIVE);
        Pattern labelsPattern = Pattern.compile(".*labels:(\\S*).*", Pattern.CASE_INSENSITIVE);
        Pattern runInTransactionPattern = Pattern.compile(".*runInTransaction:(\\w+).*", Pattern.CASE_INSENSITIVE);
        Pattern dbmsPattern = Pattern.compile(".*dbms:([^,][\\w!,]+).*", Pattern.CASE_INSENSITIVE);
        Pattern failOnErrorPattern = Pattern.compile(".*failOnError:(\\w+).*", Pattern.CASE_INSENSITIVE);
        Pattern onFailPattern = Pattern.compile(".*onFail:(\\w+).*", Pattern.CASE_INSENSITIVE);
        Pattern onErrorPattern = Pattern.compile(".*onError:(\\w+).*", Pattern.CASE_INSENSITIVE);
        Pattern onUpdateSqlPattern = Pattern.compile(".*onUpdateSQL:(\\w+).*", Pattern.CASE_INSENSITIVE);
        Matcher rollbackSplitStatementsPatternMatcher = null;
        boolean rollbackSplitStatements = true;
        String rollbackEndDelimiter = null;
        String line;
        while ((line = reader.readLine()) != null) {
            Matcher propertyPatternMatcher = propertyPattern.matcher(line);
            if (propertyPatternMatcher.matches()) {
                handleProperty(changeLogParameters, changeLog, propertyPatternMatcher);
                continue;
            }
            Matcher changeLogPatterMatcher = changeLogPattern.matcher(line);
            if (changeLogPatterMatcher.matches()) {
                Matcher logicalFilePathMatcher = logicalFilePathPattern.matcher(line);
                changeLog.setLogicalFilePath(parseString(logicalFilePathMatcher));
                Matcher changeLogIdMatcher = changeLogIdPattern.matcher(line);
                changeLog.setChangeLogId(parseString(changeLogIdMatcher));
            }
            Matcher ignoreLinesMatcher = ignoreLinesPattern.matcher(line);
            if (ignoreLinesMatcher.matches()) {
                if ("start".equals(ignoreLinesMatcher.group(1))) {
                    while ((line = reader.readLine()) != null) {
                        ignoreLinesMatcher = ignoreLinesPattern.matcher(line);
                        if (ignoreLinesMatcher.matches()) {
                            if ("end".equals(ignoreLinesMatcher.group(1))) {
                                break;
                            }
                        }
                    }
                    continue;
                } else {
                    try {
                        long ignoreCount = Long.parseLong(ignoreLinesMatcher.group(1));
                        while (ignoreCount > 0 && (line = reader.readLine()) != null) {
                            ignoreCount--;
                        }
                        continue;
                    } catch (NumberFormatException | NullPointerException nfe) {
                        throw new ChangeLogParseException("Unknown ignoreLines syntax");
                    }
                }
            }
            Matcher changeSetPatternMatcher = changeSetPattern.matcher(line);
            if (changeSetPatternMatcher.matches()) {
                String finalCurrentSql = changeLogParameters.expandExpressions(StringUtil.trimToNull(currentSql.toString()), changeLog);
                if (changeSet != null) {
                    if (finalCurrentSql == null) {
                        throw new ChangeLogParseException("No SQL for changeset " + changeSet.toString(false));
                    }
                    change.setSql(finalCurrentSql);
                    if (StringUtil.trimToNull(currentRollbackSql.toString()) != null) {
                        if (currentRollbackSql.toString().trim().toLowerCase().matches("^not required.*")) {
                            changeSet.addRollbackChange(new EmptyChange());
                        } else {
                            RawSQLChange rollbackChange = new RawSQLChange();
                            rollbackChange.setSql(changeLogParameters.expandExpressions(currentRollbackSql.toString(), changeLog));
                            if (rollbackSplitStatementsPatternMatcher.matches()) {
                                rollbackChange.setSplitStatements(rollbackSplitStatements);
                            }
                            if (rollbackEndDelimiter != null) {
                                rollbackChange.setEndDelimiter(rollbackEndDelimiter);
                            }
                            changeSet.addRollbackChange(rollbackChange);
                        }
                    }
                }
                Matcher stripCommentsPatternMatcher = stripCommentsPattern.matcher(line);
                Matcher splitStatementsPatternMatcher = splitStatementsPattern.matcher(line);
                Matcher runWithMatcher = runWithPattern.matcher(line);
                rollbackSplitStatementsPatternMatcher = rollbackSplitStatementsPattern.matcher(line);
                Matcher endDelimiterPatternMatcher = endDelimiterPattern.matcher(line);
                Matcher rollbackEndDelimiterPatternMatcher = rollbackEndDelimiterPattern.matcher(line);
                Matcher logicalFilePathMatcher = logicalFilePathPattern.matcher(line);
                Matcher runOnChangePatternMatcher = runOnChangePattern.matcher(line);
                Matcher runAlwaysPatternMatcher = runAlwaysPattern.matcher(line);
                Matcher contextPatternMatcher = contextPattern.matcher(line);
                Matcher labelsPatternMatcher = labelsPattern.matcher(line);
                Matcher runInTransactionPatternMatcher = runInTransactionPattern.matcher(line);
                Matcher dbmsPatternMatcher = dbmsPattern.matcher(line);
                Matcher failOnErrorPatternMatcher = failOnErrorPattern.matcher(line);
                boolean stripComments = parseBoolean(stripCommentsPatternMatcher, changeSet, true);
                boolean splitStatements = parseBoolean(splitStatementsPatternMatcher, changeSet, true);
                rollbackSplitStatements = parseBoolean(rollbackSplitStatementsPatternMatcher, changeSet, true);
                boolean runOnChange = parseBoolean(runOnChangePatternMatcher, changeSet, false);
                boolean runAlways = parseBoolean(runAlwaysPatternMatcher, changeSet, false);
                boolean runInTransaction = parseBoolean(runInTransactionPatternMatcher, changeSet, true);
                boolean failOnError = parseBoolean(failOnErrorPatternMatcher, changeSet, true);
                String runWith = parseString(runWithMatcher);
                if (runWith != null) {
                    runWith = changeLogParameters.expandExpressions(runWith, changeLog);
                }
                String endDelimiter = parseString(endDelimiterPatternMatcher);
                rollbackEndDelimiter = parseString(rollbackEndDelimiterPatternMatcher);
                String context = StringUtil.trimToNull(// remove surrounding quotes if they're in there
                StringUtil.trimToEmpty(parseString(contextPatternMatcher)).replaceFirst("^\"", "").replaceFirst("\"$", ""));
                if (context != null) {
                    context = changeLogParameters.expandExpressions(context, changeLog);
                }
                String labels = parseString(labelsPatternMatcher);
                if (labels != null) {
                    labels = changeLogParameters.expandExpressions(labels, changeLog);
                }
                String logicalFilePath = parseString(logicalFilePathMatcher);
                if ((logicalFilePath == null) || "".equals(logicalFilePath)) {
                    logicalFilePath = changeLog.getLogicalFilePath();
                }
                if (logicalFilePath != null) {
                    logicalFilePath = changeLogParameters.expandExpressions(logicalFilePath, changeLog);
                }
                String dbms = parseString(dbmsPatternMatcher);
                if (dbms != null) {
                    dbms = changeLogParameters.expandExpressions(dbms, changeLog);
                }
                String changeSetId = changeLogParameters.expandExpressions(StringUtil.stripEnclosingQuotes(changeSetPatternMatcher.group(2)), changeLog);
                String changeSetAuthor = changeLogParameters.expandExpressions(StringUtil.stripEnclosingQuotes(changeSetPatternMatcher.group(1)), changeLog);
                changeSet = new ChangeSet(changeSetId, changeSetAuthor, runAlways, runOnChange, logicalFilePath, context, dbms, runWith, runInTransaction, changeLog.getObjectQuotingStrategy(), changeLog);
                changeSet.setLabels(new Labels(labels));
                changeSet.setFailOnError(failOnError);
                changeLog.addChangeSet(changeSet);
                change = new RawSQLChange();
                change.setSql(finalCurrentSql);
                if (splitStatementsPatternMatcher.matches()) {
                    change.setSplitStatements(splitStatements);
                }
                change.setStripComments(stripComments);
                change.setEndDelimiter(endDelimiter);
                changeSet.addChange(change);
                currentSql.setLength(0);
                currentRollbackSql.setLength(0);
            } else {
                if (changeSet != null) {
                    Matcher commentMatcher = commentPattern.matcher(line);
                    Matcher rollbackMatcher = rollbackPattern.matcher(line);
                    Matcher preconditionsMatcher = preconditionsPattern.matcher(line);
                    Matcher preconditionMatcher = preconditionPattern.matcher(line);
                    Matcher validCheckSumMatcher = validCheckSumPattern.matcher(line);
                    if (commentMatcher.matches()) {
                        if (commentMatcher.groupCount() == 1) {
                            changeSet.setComments(commentMatcher.group(1));
                        }
                    } else if (validCheckSumMatcher.matches()) {
                        if (validCheckSumMatcher.groupCount() == 1) {
                            changeSet.addValidCheckSum(validCheckSumMatcher.group(1));
                        }
                    } else if (rollbackMatcher.matches()) {
                        if (rollbackMatcher.groupCount() == 1) {
                            currentRollbackSql.append(rollbackMatcher.group(1)).append(System.lineSeparator());
                        }
                    } else if (preconditionsMatcher.matches()) {
                        if (preconditionsMatcher.groupCount() == 1) {
                            String body = preconditionsMatcher.group(1);
                            Matcher onFailMatcher = onFailPattern.matcher(body);
                            Matcher onErrorMatcher = onErrorPattern.matcher(body);
                            Matcher onUpdateSqlMatcher = onUpdateSqlPattern.matcher(body);
                            PreconditionContainer pc = new PreconditionContainer();
                            pc.setOnFail(StringUtil.trimToNull(parseString(onFailMatcher)));
                            pc.setOnError(StringUtil.trimToNull(parseString(onErrorMatcher)));
                            pc.setOnSqlOutput(StringUtil.trimToNull(parseString(onUpdateSqlMatcher)));
                            changeSet.setPreconditions(pc);
                        }
                    } else if (preconditionMatcher.matches()) {
                        if (changeSet.getPreconditions() == null) {
                            // create the defaults
                            changeSet.setPreconditions(new PreconditionContainer());
                        }
                        if (preconditionMatcher.groupCount() == 2) {
                            String name = StringUtil.trimToNull(preconditionMatcher.group(1));
                            if (name != null) {
                                String body = preconditionMatcher.group(2).trim();
                                if ("sql-check".equals(name)) {
                                    changeSet.getPreconditions().addNestedPrecondition(parseSqlCheckCondition(changeLogParameters.expandExpressions(StringUtil.trimToNull(body), changeSet.getChangeLog())));
                                } else {
                                    throw new ChangeLogParseException("The '" + name + "' precondition type is not supported.");
                                }
                            }
                        }
                    } else {
                        currentSql.append(line).append(System.lineSeparator());
                    }
                }
            }
        }
        if (changeSet != null) {
            change.setSql(changeLogParameters.expandExpressions(StringUtil.trimToNull(currentSql.toString()), changeSet.getChangeLog()));
            if ((change.getEndDelimiter() == null) && StringUtil.trimToEmpty(change.getSql()).endsWith("\n/")) {
                change.setEndDelimiter("\n/$");
            }
            if (StringUtil.trimToNull(currentRollbackSql.toString()) != null) {
                if (currentRollbackSql.toString().trim().toLowerCase().matches("^not required.*")) {
                    changeSet.addRollbackChange(new EmptyChange());
                } else {
                    RawSQLChange rollbackChange = new RawSQLChange();
                    rollbackChange.setSql(changeLogParameters.expandExpressions(currentRollbackSql.toString(), changeSet.getChangeLog()));
                    if (rollbackSplitStatementsPatternMatcher.matches()) {
                        rollbackChange.setSplitStatements(rollbackSplitStatements);
                    }
                    if (rollbackEndDelimiter != null) {
                        rollbackChange.setEndDelimiter(rollbackEndDelimiter);
                    }
                    changeSet.addRollbackChange(rollbackChange);
                }
            }
        }
    } catch (IOException e) {
        throw new ChangeLogParseException(e);
    }
    return changeLog;
}
Also used : RawSQLChange(liquibase.change.core.RawSQLChange) Pattern(java.util.regex.Pattern) PreconditionContainer(liquibase.precondition.core.PreconditionContainer) Matcher(java.util.regex.Matcher) Labels(liquibase.Labels) IOException(java.io.IOException) EmptyChange(liquibase.change.core.EmptyChange) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) ChangeLogParseException(liquibase.exception.ChangeLogParseException) BufferedReader(java.io.BufferedReader) ChangeSet(liquibase.changelog.ChangeSet)

Example 10 with ChangeSet

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

the class LicenseServiceUtils method checkForValidLicense.

public static ValidationErrors checkForValidLicense(String licenseType, Change change) {
    LicenseService licenseService = Scope.getCurrentScope().getSingleton(LicenseServiceFactory.class).getLicenseService();
    if (licenseService == null) {
        return new ValidationErrors();
    }
    if (licenseService.licenseIsValid(licenseType)) {
        String message = String.format("Found valid license with subject '%s' for '%s'", licenseType, change.getDescription());
        Scope.getCurrentScope().getLog(LicenseService.class).fine(message);
        return new ValidationErrors();
    }
    ChangeSet changeSet = change.getChangeSet();
    String changeType = Scope.getCurrentScope().getSingleton(ChangeFactory.class).getChangeMetaData(change).getName();
    ValidationErrors validationErrors = new ValidationErrors();
    String message = "Change Set ID: " + changeSet.getId() + " Change Set Author: " + changeSet.getAuthor() + "\n";
    message += "Change Type 'pro:" + changeType + "' is not allowed without a valid Liquibase Pro License.\n";
    message += "To purchase or renew a Liquibase Pro license key, please contact sales@liquibase.com or\n" + "go to https://www.liquibase.org/download";
    validationErrors.addError(message);
    return validationErrors;
}
Also used : ValidationErrors(liquibase.exception.ValidationErrors) ChangeSet(liquibase.changelog.ChangeSet)

Aggregations

ChangeSet (liquibase.changelog.ChangeSet)72 Test (org.junit.Test)41 Contexts (liquibase.Contexts)12 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)11 RanChangeSet (liquibase.changelog.RanChangeSet)10 Liquibase (liquibase.Liquibase)9 Change (liquibase.change.Change)9 Database (liquibase.database.Database)9 LiquibaseException (liquibase.exception.LiquibaseException)8 ArrayList (java.util.ArrayList)7 DiffOutputControl (liquibase.diff.output.DiffOutputControl)7 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)7 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)6 DiffResult (liquibase.diff.DiffResult)6 CompareControl (liquibase.diff.compare.CompareControl)6 IOException (java.io.IOException)5 Sql (liquibase.sql.Sql)5 MarkChangeSetRanStatement (liquibase.statement.core.MarkChangeSetRanStatement)5 Date (java.util.Date)4 Labels (liquibase.Labels)4