zython
zython

Reputation: 1288

How to inject searchPath parameter with Liquibase CDI

I'm trying to inject the searchPath parameter when using Liquibase CDI, but none of my approaches below seem to be working. Due to circumstances out of my control this is the only way I can modify my liquibase setup.

    // LiquibaseProducer.java
    
    package de.XXXX.XXXX.liquibase;

    import java.sql.SQLException;
    import java.util.HashMap;
    import java.util.Map;

    import javax.annotation.Resource;
    import javax.enterprise.context.Dependent;
    import javax.enterprise.inject.Produces;
    import javax.sql.DataSource;

    import liquibase.integration.cdi.CDILiquibaseConfig;
    import liquibase.integration.cdi.annotations.LiquibaseType;
    import liquibase.resource.ClassLoaderResourceAccessor;
    import liquibase.resource.ResourceAccessor;

    /**
     * CDI Producer to configure the CDI Liquibase integration.
     */
    @Dependent
    public class LiquibaseProducer {

        /** Used liquibase author. */
        public static final String LIQUIBASE_AUTHOR = "liquibase-auto";

        /** Datasource. */
        @Resource(mappedName = "XXXXXXXXX", name = "XXXXXX", type = DataSource.class)
        private DataSource dataSource;

        /** Creating the liquibase configuration. */
        @SuppressWarnings("static-method")
        @Produces
        @LiquibaseType
        public CDILiquibaseConfig createConfig() {
            CDILiquibaseConfig tConfig = new CDILiquibaseConfig();
            
            Map<String, String> tempMap = new HashMap<>();
            tempMap.put("searchPath", "./testpath");
            tempMap.put("search-path", "./testpaaaath/");
            tempMap.put("search-path", "LIQUIBASE_SEARCH_PATH=./testingpath");
            tempMap.put("liquibase.searchPath", "./testpathxyyz");
            tConfig.setParameters(tempMap);
            tConfig.setChangeLog("db/changelog/init.xml");
            return tConfig;
        }

        /** Creating the Datasource. */
        @Produces
        @LiquibaseType
        public DataSource createDataSource() throws SQLException {
            return dataSource;
        }

        /** Creating the ResourceAccessor. */
        @Produces
        @LiquibaseType
        public ResourceAccessor create() {
            return new ClassLoaderResourceAccessor(getClass().getClassLoader());
        }

    }

Upvotes: 0

Views: 57

Answers (1)

Lautert
Lautert

Reputation: 520

Those parameter are used as changelog parameters, not liquibase parameters: that's why they are not working. You would need to set this in the scope, but I don't see a way to do it in CDI.

Maybe you could set the ResourceAcessor and tame it to your needs. I guess CDI uses the DirectoryResourceAcessor but you could create one that better suits your needs, like SearchPath used in Maven plugin.

Upvotes: 1

Related Questions