|
| 1 | +package com.otchi.infrastructure.config.database; |
| 2 | + |
| 3 | +import com.zaxxer.hikari.HikariConfig; |
| 4 | +import com.zaxxer.hikari.HikariDataSource; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.springframework.beans.factory.annotation.Value; |
| 8 | +import org.springframework.context.ApplicationContextException; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import org.springframework.context.annotation.Profile; |
| 12 | +import org.springframework.core.io.ClassPathResource; |
| 13 | +import org.springframework.core.io.Resource; |
| 14 | +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; |
| 15 | + |
| 16 | +import javax.sql.DataSource; |
| 17 | + |
| 18 | +import static com.otchi.infrastructure.config.Constants.SPRING_PROFILE_PRODUCTION; |
| 19 | + |
| 20 | +@Configuration |
| 21 | +@Profile(SPRING_PROFILE_PRODUCTION) |
| 22 | +public class HerokuDatabaseConfig { |
| 23 | + |
| 24 | + private final Logger log = LoggerFactory.getLogger(HerokuDatabaseConfig.class); |
| 25 | + |
| 26 | + @Value("${spring.datasource.cachePrepStmts:true}") |
| 27 | + private boolean cachePrepStmts; |
| 28 | + |
| 29 | + @Value("${spring.datasource.prepStmtCacheSize:250}") |
| 30 | + private int prepStmtCacheSize; |
| 31 | + |
| 32 | + @Value("${spring.datasource.prepStmtCacheSqlLimit:2048}") |
| 33 | + private int prepStmtCacheSqlLimit; |
| 34 | + |
| 35 | + @Value("${spring.datasource.dataSourceClassName}") |
| 36 | + private String dataSourceClassName; |
| 37 | + |
| 38 | + @Bean |
| 39 | + public DataSource dataSource() { |
| 40 | + log.debug("Configuring Heroku Datasource"); |
| 41 | + String herokuUrl = System.getenv("JDBC_DATABASE_URL"); |
| 42 | + if (herokuUrl != null) { |
| 43 | + HikariConfig config = new HikariConfig(); |
| 44 | + |
| 45 | + //MySQL optimizations, see https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration |
| 46 | + if ("com.mysql.jdbc.jdbc2.optional.MysqlDataSource".equals(dataSourceClassName)) { |
| 47 | + config.addDataSourceProperty("cachePrepStmts", cachePrepStmts); |
| 48 | + config.addDataSourceProperty("prepStmtCacheSize", prepStmtCacheSize); |
| 49 | + config.addDataSourceProperty("prepStmtCacheSqlLimit", prepStmtCacheSqlLimit); |
| 50 | + } |
| 51 | + |
| 52 | + config.setDataSourceClassName(dataSourceClassName); |
| 53 | + config.addDataSourceProperty("url", herokuUrl); |
| 54 | + HikariDataSource hikariDataSource = new HikariDataSource(config); |
| 55 | + executeScript(hikariDataSource, "database/schema.sql"); |
| 56 | + executeScript(hikariDataSource, "database/social-users-connections.sql"); |
| 57 | + executeScript(hikariDataSource, "database/data.sql"); |
| 58 | + return hikariDataSource; |
| 59 | + } else { |
| 60 | + throw new ApplicationContextException("Heroku database URL is not configured, you must set $JDBC_DATABASE_URL"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void executeScript(DataSource dataSource, String script) { |
| 65 | + Resource resource = new ClassPathResource(script); |
| 66 | + ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(resource); |
| 67 | + databasePopulator.execute(dataSource); |
| 68 | + } |
| 69 | +} |
0 commit comments