[ACCEPTED]-Spring-Boot execute data.sql in one profile only-spring-boot

Accepted answer
Score: 19

You were on the right track with your approach 8 1), but you should set datasource platform 7 via spring.datasource.platform, not spring.jpa.database-platform. The script execution functionality 6 is not JPA-specific.

You can also manually 5 specify which SQL script files get executed 4 by setting the spring.datasource.schema property. This is an excerpt 3 from org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration file in 1.0.2.RELEASE:

String schema = this.datasourceProperties.getProperty("schema");
if (schema == null) {
    schema = "classpath*:schema-"
            + this.datasourceProperties.getProperty("platform", "all")
            + ".sql,classpath*:schema.sql,classpath*:data.sql";
}

As you can see, the set of files specified 2 in the documentation is only used if you 1 don't specify your own list.

Score: 0

For future readers.

I solved this a slightly 6 different way ... using "java config".

The 5 most important part of the below is:

@Bean @Profile(SPRING_PROFILE_DEFAULT) public 4 DataSourceInitializer getDataSourceInitializer(final 3 DataSource dataSource) {

That will load the 2 .sql file NOT named "data.sql"....only 1 when that profile is active.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Optional;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
/* below @CS is not needed with setPackagesToScan */
//@ComponentScan(basePackageClasses = {SomeJpaEntityOne.class, SomeJpaEntityTwo.class})
public class PersistenceJpaConfig {

    public static final String SPRING_PROFILE_DEFAULT = "default";

    /* the below file-name is purposely not "data.sql" (or data-spring.sql) to avoid/bypass "auto-find" spring-data logic.  the file/resource is referred to later in a specific spring profile */
    @Value("classpath:developer.local.seed.data.dml.sql")
    private Resource seedDataLocalDeveloperResource;

    /**
     * @param env
     * @return
     */
    /* bean must be named entityManagerFactory to satisfy spring-jpa magic */
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(Environment env) {
        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(this.getDataSourceOne());

        final String entityManagerFactoryPackagesToScanCsv = "com.myentitiespackageone,com.myentitiespackagetwo";
        String[] packagesArray = entityManagerFactoryPackagesToScanCsv.split(",");
        em.setPackagesToScan(packagesArray);

        final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(getCustomHibernateProperties(env, configMapRetriever));

        return em;
    }

    @Bean
    public DataSource getDataSourceOne() {

        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("myDataSourceDriverClassName");
        dataSourceBuilder.url("mydataSourceUrl");
        dataSourceBuilder.username("mydataSourceUserName");
        dataSourceBuilder.password("myPassword");

        DataSource returnItem = dataSourceBuilder.build();
        return returnItem;
    }

    /**
     * @param env
     * @param secretRetriever
     * @param configMapRetriever
     * @return JPA PlatformTransactionManager
     */
    /* This bean must be named 'transactionManager' to satisfy jpa string-magic */
    @Bean(name = "transactionManager")
    public PlatformTransactionManager getAPlatformTransactionManager(Environment env) {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory(env).getObject());
        return transactionManager;
    }

    /**
     * @return JPA PersistenceExceptionTranslationPostProcessor
     */
    @Bean
    public PersistenceExceptionTranslationPostProcessor getAPersistenceExceptionTranslationPostProcessor() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    final Properties getCustomHibernateProperties(Environment env, IConfigMapRetriever configMapRetriever) {

        Properties hibernateProperties = new Properties();
        /* not shown */
        /* but stuff like

                "hibernate.dialect"
                "hibernate.hbm2ddl.auto"
                "hibernate.jdbc.batch_size"
                "hibernate.jdbc.fetch_size"
                "hibernate.order_inserts"
                "hibernate.order_updates"
                "hibernate.jdbc.batch_versioned_data"
                "hibernate.generate_statistics"
                "hibernate.show_sql"
                "hibernate.format_sql"

        */

        return hibernateProperties;
    }

    /**
     * @param dataSource
     * @return
     */
    @Bean
    @Profile(SPRING_PROFILE_DEFAULT)
    public DataSourceInitializer getDataSourceInitializer(final DataSource dataSource) {
        final DataSourceInitializer initializer = new DataSourceInitializer();
        initializer.setDataSource(dataSource);
        initializer.setDatabasePopulator(getDatabasePopulator());
        return initializer;
    }

    private DatabasePopulator getDatabasePopulator() {
        final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScript(seedDataLocalDeveloperResource);
        return populator;
    }
}

More Related questions