private static void ensureDatabaseExists(Config dbConfig) throws Exception { if (testMetastoreDatabaseServer == null) { try (Mutex ignored = new Mutex()) { if (testMetastoreDatabaseServer == null) { testMetastoreDatabaseServer = new TestMetastoreDatabaseServer(dbConfig); } } } }
static void release(ITestMetastoreDatabase instance) throws IOException { synchronized (syncObject) { if (instances.remove(instance) && instances.size() == 0) { testMetastoreDatabaseServer.close(); testMetastoreDatabaseServer = null; } } }
@Override public void close() throws IOException { try { this.testMetastoreDatabaseServer.drop(database); } catch (URISyntaxException | SQLException ignored) { } finally { TestMetastoreDatabaseFactory.release(this); } }
public void drop(String database) throws SQLException, URISyntaxException { Optional<Connection> connectionOptional = Optional.absent(); try { connectionOptional = getConnector(getInformationSchemaJdbcUrl()); Connection connection = connectionOptional.get(); executeStatement(connection, String.format(DROP_DATABASE_TEMPLATE, database)); } finally { if (connectionOptional.isPresent()) { connectionOptional.get().close(); } } }
private void ensureDatabaseExists(String database) throws SQLException, URISyntaxException { Optional<Connection> connectionOptional = Optional.absent(); try { connectionOptional = getConnector(getInformationSchemaJdbcUrl()); Connection connection = connectionOptional.get(); executeStatements(connection, String.format(DROP_DATABASE_TEMPLATE, database), String.format(CREATE_DATABASE_TEMPLATE, database, config.getCharset().getCharset(), config.getCharset().getCollate()), String.format(ADD_USER_TEMPLATE, database, config.getUsername())); } finally { if (connectionOptional.isPresent()) { connectionOptional.get().close(); } } }
TestMetastoreDatabaseServer(Config dbConfig) throws Exception { Config realConfig = dbConfig.withFallback(getDefaultConfig()).getConfig(CONFIG_PREFIX); this.embeddedMysqlEnabled = realConfig.getBoolean(EMBEDDED_MYSQL_ENABLED_KEY); this.dbUserName = realConfig.getString(DBUSER_NAME_KEY); this.dbUserPassword = realConfig.getString(DBUSER_PASSWORD_KEY); this.dbHost = this.embeddedMysqlEnabled ? "localhost" : realConfig.getString(DBHOST_KEY); this.dbPort = this.embeddedMysqlEnabled ? chooseRandomPort() : realConfig.getInt(DBPORT_KEY); this.log.error("Starting with config: embeddedMysqlEnabled={} dbUserName={} dbHost={} dbPort={}", this.embeddedMysqlEnabled, this.dbUserName, this.dbHost, this.dbPort); config = MysqldConfig.aMysqldConfig(Version.v5_6_latest) .withPort(this.dbPort) .withUser(this.dbUserName, this.dbUserPassword) .build(); if (this.embeddedMysqlEnabled) { testingMySqlServer = EmbeddedMysql.anEmbeddedMysql(config).start(); } else { testingMySqlServer = null; } }
void prepareDatabase(String database, String version) throws Exception { // Drop/create the database this.ensureDatabaseExists(database); // Deploy the schema DatabaseJobHistoryStoreSchemaManager schemaManager = DatabaseJobHistoryStoreSchemaManager.builder() .setDataSource(getJdbcUrl(database).toString(), this.dbUserName, this.dbUserPassword) .setVersion(version) .build(); schemaManager.migrate(); }
private MySqlJdbcUrl getInformationSchemaJdbcUrl() throws URISyntaxException { return getBaseJdbcUrl() .setPath(INFORMATION_SCHEMA) .setUser(ROOT_USER); }
private void executeStatements(Connection connection, String... statements) throws SQLException { for (String statement : statements) { executeStatement(connection, statement); } }
MySqlJdbcUrl getJdbcUrl(String database) throws URISyntaxException { return getBaseJdbcUrl() .setPath(database) .setUser(this.dbUserName) .setPassword(this.dbUserPassword) .setParameter("useLegacyDatetimeCode", "false") .setParameter("rewriteBatchedStatements", "true"); }