com.avaje.ebean.config.ServerConfig Java Examples

The following examples show how to use com.avaje.ebean.config.ServerConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DbDiffCommand.java    From dropwizard-experiment with MIT License 7 votes vote down vote up
@Override
protected void run(Bootstrap<T> bootstrap, Namespace namespace, T configuration) throws Exception {
    // The existing database with migrations managed by Liquibase.
    DataSourceFactory outdatedDb = configuration.getDatabaseConfig();

    try (CloseableLiquibase outdatedLiquibase = createLiquibase(outdatedDb)) {
        // A temporary database that starts out empty and then gets the autogenerated Ebean table definitions applied.
        DataSourceFactory freshDb = EbeanConfigUtils.clone(outdatedDb);
        String url = outdatedDb.getUrl();
        freshDb.setUrl(url.substring(0, url.lastIndexOf("/")) + "/migrationdiff");

        // Creating the Ebean server makes it apply its table definitions to the database immediately.
        ServerConfig serverConfig = EbeanConfigUtils.createServerConfig(freshDb);
        serverConfig.setDdlGenerate(true);
        serverConfig.setDdlRun(true);
        EbeanServer ebeanServer = EbeanServerFactory.create(serverConfig);

        try (CloseableLiquibase freshLiquibase = createLiquibase(freshDb)) {
            // Create and print the differences between the two databases, i.e. a migration that should be applied to update to the newest Ebean definitions.
            DiffResult diff = outdatedLiquibase.diff(freshLiquibase.getDatabase(), outdatedLiquibase.getDatabase(), CompareControl.STANDARD);
            DiffToChangeLog diffToChangeLog = new DiffToChangeLog(diff, new DiffOutputControl(false, false, true));
            diffToChangeLog.print(System.out);
        }
    }
}
 
Example #2
Source File: CraftServer.java    From Thermos with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void configureDbConfig(ServerConfig config) {
    Validate.notNull(config, "Config cannot be null");

    DataSourceConfig ds = new DataSourceConfig();
    ds.setDriver(configuration.getString("database.driver"));
    ds.setUrl(configuration.getString("database.url"));
    ds.setUsername(configuration.getString("database.username"));
    ds.setPassword(configuration.getString("database.password"));
    ds.setIsolationLevel(TransactionIsolation.getLevel(configuration.getString("database.isolation")));

    if (ds.getDriver().contains("sqlite")) {
        config.setDatabasePlatform(new SQLitePlatform());
        config.getDatabasePlatform().getDbDdlSyntax().setIdentity("");
    }

    config.setDataSourceConfig(ds);
}
 
Example #3
Source File: InMemoryEbeanServer.java    From dropwizard-experiment with MIT License 6 votes vote down vote up
public InMemoryEbeanServer() {
    // Create in-memory database configuration.
    DataSourceConfig dbConfig = new DataSourceConfig();
    dbConfig.setUsername("sa");
    dbConfig.setPassword("");
    dbConfig.setUrl("jdbc:h2:mem:tests2;DB_CLOSE_DELAY=-1");
    dbConfig.setDriver("org.h2.Driver");

    ServerConfig config = new ServerConfig();
    config.setName("h2");
    config.setDataSourceConfig(dbConfig);
    config.setDefaultServer(true);

    for (Class<?> entity : EbeanEntities.getEntities()) {
        config.addClass(entity);
    }

    config.setDdlGenerate(true);
    config.setDdlRun(true);

    server = EbeanServerFactory.create(config);
    ddl = ((SpiEbeanServer) server).getDdlGenerator();
}
 
Example #4
Source File: EbeanBundle.java    From dropwizard-experiment with MIT License 6 votes vote down vote up
@Override
public void run(HasDatabaseConfiguration configuration, Environment environment) throws Exception {
    ExtendedDataSourceFactory dbConfig = configuration.getDatabaseConfig();
    log.info("Connecting to database on '{}' with username '{}'.", dbConfig.getUrl(), dbConfig.getUser());

    if (dbConfig.isMigrationsEnabled()) {
        try {
            applyMigrations(dbConfig, environment.metrics());
        } catch (Exception e) {
            log.error("Error applying database migrations! {}", e.getMessage());
            throw e;
        }
    } else {
        log.info("Database migrations disabled.");
    }

    ServerConfig serverConfig = EbeanConfigUtils.createServerConfig(dbConfig);
    ebeanServer = EbeanServerFactory.create(serverConfig);
    Preconditions.checkNotNull(ebeanServer);

    environment.healthChecks().register("ebean-" + ebeanServer.getName(), new EbeanHealthCheck(ebeanServer));
}
 
Example #5
Source File: DbTestsApplication.java    From java-persistence-frameworks-comparison with MIT License 5 votes vote down vote up
@SuppressWarnings("SpringJavaAutowiringInspection")
@Bean
public ServerConfig ebeanServerConfig(DataSource dataSource) {
    ServerConfig config = new ServerConfig();
    config.setName("ebeanServer");
    config.setDefaultServer(true);
    config.setDataSource(dataSource);
    config.addPackage("com.clevergang.dbtests.repository.impl.ebean.entities");
    config.setExternalTransactionManager(new SpringAwareJdbcTransactionManager());
    config.setAutoCommitMode(false);
    config.setExpressionNativeIlike(true);

    return config;
}
 
Example #6
Source File: EbeanConfigUtils.java    From dropwizard-experiment with MIT License 5 votes vote down vote up
public static ServerConfig createServerConfig(DataSourceFactory dbConfig) {
    ServerConfig config = new ServerConfig();
    config.setName("main");
    config.setDataSourceConfig(createDataSourceConfig(dbConfig));
    config.setDefaultServer(true);

    EbeanEntities.getEntities().forEach(config::addClass);

    return config;
}
 
Example #7
Source File: DbTestsApplication.java    From java-persistence-frameworks-comparison with MIT License 4 votes vote down vote up
@Bean
public EbeanServer ebeanServer(ServerConfig serverConfig) {
    return EbeanServerFactory.create(serverConfig);
}