liquibase.parser.ChangeLogParserFactory Java Examples

The following examples show how to use liquibase.parser.ChangeLogParserFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LiquibaseProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Collect the configured changeLog file for the default and all named datasources.
 * <p>
 * A {@link LinkedHashSet} is used to avoid duplications.
 */
private List<String> getChangeLogs(Collection<String> dataSourceNames, LiquibaseBuildTimeConfig liquibaseBuildConfig) {
    if (dataSourceNames.isEmpty()) {
        return Collections.emptyList();
    }

    ChangeLogParameters changeLogParameters = new ChangeLogParameters();
    ClassLoaderResourceAccessor classLoaderResourceAccessor = new ClassLoaderResourceAccessor(
            Thread.currentThread().getContextClassLoader());

    ChangeLogParserFactory changeLogParserFactory = ChangeLogParserFactory.getInstance();

    Set<String> resources = new LinkedHashSet<>();

    // default datasource
    if (DataSourceUtil.hasDefault(dataSourceNames)) {
        resources.addAll(findAllChangeLogs(liquibaseBuildConfig.defaultDataSource.changeLog, changeLogParserFactory,
                classLoaderResourceAccessor, changeLogParameters));
    }

    // named datasources
    Collection<String> namedDataSourceChangeLogs = dataSourceNames.stream()
            .filter(n -> !DataSourceUtil.isDefault(n))
            .map(liquibaseBuildConfig::getConfigForDataSourceName)
            .map(c -> c.changeLog)
            .collect(Collectors.toCollection(LinkedHashSet::new));

    for (String namedDataSourceChangeLog : namedDataSourceChangeLogs) {
        resources.addAll(
                findAllChangeLogs(namedDataSourceChangeLog, changeLogParserFactory, classLoaderResourceAccessor,
                        changeLogParameters));
    }

    LOGGER.debugf("Liquibase changeLogs: %s", resources);

    return new ArrayList<>(resources);
}
 
Example #2
Source File: LiquibaseProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Finds all resource files for the given change log file
 */
private Set<String> findAllChangeLogs(String file, ChangeLogParserFactory changeLogParserFactory,
        ClassLoaderResourceAccessor classLoaderResourceAccessor,
        ChangeLogParameters changeLogParameters) {
    try {
        ChangeLogParser parser = changeLogParserFactory.getParser(file, classLoaderResourceAccessor);
        DatabaseChangeLog changelog = parser.parse(file, changeLogParameters, classLoaderResourceAccessor);

        if (changelog != null) {
            Set<String> result = new LinkedHashSet<>();
            // get all changeSet files
            for (ChangeSet changeSet : changelog.getChangeSets()) {
                result.add(changeSet.getFilePath());

                // get all parents of the changeSet
                DatabaseChangeLog parent = changeSet.getChangeLog();
                while (parent != null) {
                    result.add(parent.getFilePath());
                    parent = parent.getParentChangeLog();
                }
            }
            result.add(changelog.getFilePath());
            return result;
        }
    } catch (LiquibaseException ex) {
        throw new IllegalStateException(ex);
    }
    return Collections.emptySet();
}
 
Example #3
Source File: ChangeLogParserTest.java    From liquibase-percona with Apache License 2.0 4 votes vote down vote up
private DatabaseChangeLog loadChangeLog(String filename) throws Exception {
    ChangeLogParserFactory parserFactory = ChangeLogParserFactory.getInstance();
    ChangeLogParser parser = parserFactory.getParser(filename, resourceAccessor);
    return parser.parse(filename, new ChangeLogParameters(), resourceAccessor);
}
 
Example #4
Source File: QuarkusLiquibaseConnectionProvider.java    From keycloak with Apache License 2.0 4 votes vote down vote up
protected void baseLiquibaseInitialization(KeycloakSession session) {
    resourceAccessor = new ClassLoaderResourceAccessor(getClass().getClassLoader());
    FastServiceLocator locator = (FastServiceLocator) ServiceLocator.getInstance();

    JpaConnectionProviderFactory jpaConnectionProvider = (JpaConnectionProviderFactory) session
            .getKeycloakSessionFactory().getProviderFactory(JpaConnectionProvider.class);

    // register our custom databases
    locator.register(new PostgresPlusDatabase());
    locator.register(new UpdatedMySqlDatabase());
    locator.register(new UpdatedMariaDBDatabase());
    
    // registers only the database we are using
    try (Connection connection = jpaConnectionProvider.getConnection()) {
        Database database = DatabaseFactory.getInstance()
                .findCorrectDatabaseImplementation(new JdbcConnection(connection));
        if (database.getDatabaseProductName().equals(MySQLDatabase.PRODUCT_NAME)) {
            // Adding CustomVarcharType for MySQL 8 and newer
            DataTypeFactory.getInstance().register(MySQL8VarcharType.class);
        } else if (database.getDatabaseProductName().equals(MariaDBDatabase.PRODUCT_NAME)) {
            // Adding CustomVarcharType for MySQL 8 and newer
            DataTypeFactory.getInstance().register(MySQL8VarcharType.class);
        }

        DatabaseFactory.getInstance().clearRegistry();
        locator.register(database);
    } catch (Exception cause) {
        throw new RuntimeException("Failed to configure Liquibase database", cause);
    }

    // disables XML validation
    for (ChangeLogParser parser : ChangeLogParserFactory.getInstance().getParsers()) {
        if (parser instanceof XMLChangeLogSAXParser) {
            Method getSaxParserFactory = null;
            try {
                getSaxParserFactory = XMLChangeLogSAXParser.class.getDeclaredMethod("getSaxParserFactory");
                getSaxParserFactory.setAccessible(true);
                SAXParserFactory saxParserFactory = (SAXParserFactory) getSaxParserFactory.invoke(parser);
                saxParserFactory.setValidating(false);
                saxParserFactory.setSchema(null);
            } catch (Exception e) {
                logger.warnf("Failed to disable liquibase XML validations");
            } finally {
                if (getSaxParserFactory != null) {
                    getSaxParserFactory.setAccessible(false);
                }
            }
        }
    }
}