|
| 1 | +package ubic.gemma.core.apps; |
| 2 | + |
| 3 | +import com.zaxxer.hikari.HikariDataSource; |
| 4 | +import org.apache.commons.cli.CommandLine; |
| 5 | +import org.apache.commons.cli.Options; |
| 6 | +import org.apache.commons.cli.ParseException; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.context.annotation.Profile; |
| 10 | +import org.springframework.jdbc.datasource.init.CompositeDatabasePopulator; |
| 11 | +import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; |
| 12 | +import ubic.gemma.core.util.AbstractCLI; |
| 13 | +import ubic.gemma.persistence.hibernate.LocalSessionFactoryBean; |
| 14 | +import ubic.gemma.persistence.initialization.CreateDatabasePopulator; |
| 15 | +import ubic.gemma.persistence.initialization.DatabaseSchemaPopulator; |
| 16 | +import ubic.gemma.persistence.initialization.InitialDataPopulator; |
| 17 | + |
| 18 | +import javax.annotation.Nullable; |
| 19 | +import javax.sql.DataSource; |
| 20 | + |
| 21 | +import static ubic.gemma.persistence.initialization.BootstrappedDataSourceFactory.createBootstrappedDataSource; |
| 22 | + |
| 23 | +/** |
| 24 | + * This is exclusively available for the test database. |
| 25 | + */ |
| 26 | +@Profile("testdb") |
| 27 | +public class InitializeDatabaseCli extends AbstractCLI { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private DataSource dataSource; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private LocalSessionFactoryBean factory; |
| 34 | + |
| 35 | + @Value("${gemma.testdb.name}") |
| 36 | + private String databaseName; |
| 37 | + |
| 38 | + @Nullable |
| 39 | + @Override |
| 40 | + public String getCommandName() { |
| 41 | + return "initializeDatabase"; |
| 42 | + } |
| 43 | + |
| 44 | + @Nullable |
| 45 | + @Override |
| 46 | + public String getShortDesc() { |
| 47 | + return ""; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public CommandGroup getCommandGroup() { |
| 52 | + return CommandGroup.MISC; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void buildOptions( Options options ) { |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void processOptions( CommandLine commandLine ) throws ParseException { |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected void doWork() throws Exception { |
| 65 | + String jdbcUrl; |
| 66 | + if ( dataSource instanceof HikariDataSource ) { |
| 67 | + jdbcUrl = ( ( HikariDataSource ) dataSource ).getJdbcUrl(); |
| 68 | + } else { |
| 69 | + jdbcUrl = dataSource.toString(); |
| 70 | + } |
| 71 | + promptConfirmationOrAbort( "The following data source will be initialized: " + jdbcUrl ); |
| 72 | + try ( HikariDataSource bootstrappedDataSource = createBootstrappedDataSource( dataSource ) ) { |
| 73 | + CreateDatabasePopulator cdb = new CreateDatabasePopulator( databaseName ); |
| 74 | + cdb.setDropIfExists( true ); |
| 75 | + DatabasePopulatorUtils.execute( cdb, bootstrappedDataSource ); |
| 76 | + } |
| 77 | + CompositeDatabasePopulator cdp = new CompositeDatabasePopulator(); |
| 78 | + cdp.addPopulators( |
| 79 | + new DatabaseSchemaPopulator( factory, "mysql" ), |
| 80 | + new InitialDataPopulator( false ) ); |
| 81 | + DatabasePopulatorUtils.execute( cdp, dataSource ); |
| 82 | + } |
| 83 | +} |
0 commit comments