简体   繁体   中英

Spring-Boot execute data.sql in one profile only

I have this application where using a profile "default" it connects to a PostgreSQL database and do migrations using Flyway.

I want to create another profile called "devEmbeddedCreate" where I need to use an embedded database server (h2), create the database using spring.jpa.hibernate.ddl-auto=create-drop in the application.properties file and execute a differently called "data.sql" script to initialize some tables.

If I leave the script with "data.sql" file name, it gets executed every time the application starts. That's something I don't want to happen, I need it to be executed only in a certain profile.

Things I've tried:

  1. The documentation mentions there can be a schema-${platform}.sql file and you can define the platform using spring.datasource.platform in the config. The problem it doesn't work with a data-${platform}.sql file. ( here )

  2. Created a EmbeddedDatabaseBuilder . The problem is when I use it, it doesn't automatically create the database and only apply the specified script. Couldn't find a way to create the database automatically as spring.jpa.hibernate.ddl-auto=create-drop does. ( here and here )

  3. Looking for a way to transform an XML config to Java-based configuration found a way to create the database and all. After a lot of tweaking and changing to work in memory, it looked promising but haven't been able to find out why the database get closed (and erased all its structure) while starting up ( here )

There must be a simpler way to just say "hey spring... run on strartup this data-devEmbeddedCreate.sql script when my profile is devEmbeddedCreate , right?

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

You can also manually specify which SQL script files get executed by setting the spring.datasource.schema property. This is an excerpt 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 in the documentation is only used if you don't specify your own list.

For future readers.

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

The most important part of the below is:

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

That will load the .sql file NOT named "data.sql"....only 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM