Skip to content
Permalink
Browse files
Ensure that seshat db and tenants table gets created
  • Loading branch information
Izakey committed Mar 29, 2019
1 parent f34a8ab commit b83331b723f175003ccf069764414e4ab371662c
Showing 5 changed files with 62 additions and 81 deletions.
@@ -59,7 +59,6 @@ public Logger logger() {
}
}


private static TestEnvironment testEnvironment = new TestEnvironment(APP_NAME);
private static ProvisionerPostgreSQLInitializer postgreSQLInitializer = new ProvisionerPostgreSQLInitializer();
private static ProvisionerCassandraInitializer cassandraInitializer = new ProvisionerCassandraInitializer();
@@ -27,6 +27,7 @@
import java.nio.ByteBuffer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.UUID;
@@ -58,7 +59,6 @@ public class ProvisionerInitializer {
private final HashGenerator hashGenerator;
private final String initialClientId;
private String metaKeySpaceName;
private String postgresDbName;

@Autowired
public ProvisionerInitializer(final Environment environment, @Qualifier(ProvisionerConstants.LOGGER_NAME) final Logger logger,
@@ -80,12 +80,10 @@ public void initialize() {
metaKeySpaceName = this.environment.getProperty(
CassandraConnectorConstants.KEYSPACE_PROP,
CassandraConnectorConstants.KEYSPACE_PROP_DEFAULT);
postgresDbName = this.environment.getProperty(
PostgreSQLConstants.POSTGRESQL_DATABASE_NAME_PROP,
PostgreSQLConstants.POSTGRESQL_DATABASE_NAME_DEFAULT);

this.initializeCassandra();
this.initializeDatabase();
this.initializeDatabase(PostgreSQLConstants.POSTGRESQL_DATABASE_NAME_DEFAULT);
//this.initializeDatabase("playground");
} catch (final Exception ex) {
throw new IllegalStateException("Could not initialize service!", ex);
}
@@ -198,9 +196,11 @@ private void initializeCassandra() throws Exception {
}
}

private void initializeDatabase() throws Exception {
private void initializeDatabase(String postgresDbName) throws Exception {

this.logger.info("Creating meta database {} ", postgresDbName);
try (
final Connection connection = DataSourceUtils.createProvisionerConnection(this.environment);
final Connection connection = DataSourceUtils.createProvisionerConnection(this.environment, postgresDbName);
final Statement statement = connection.createStatement()
) {
final ResultSet findDB = statement.executeQuery("SELECT datname FROM pg_database WHERE datname = '" + postgresDbName + "'");
@@ -212,21 +212,25 @@ private void initializeDatabase() throws Exception {
}

try (
final Connection metaConnection = DataSourceUtils.createProvisionerConnection(this.environment);
final Statement metaStatement = metaConnection.createStatement()
final Connection metaConnection = DataSourceUtils.createProvisionerConnection(this.environment, postgresDbName);
final Statement metaStatement = metaConnection.createStatement()
) {
this.logger.info("Create tenants table if it not exists");
metaStatement.execute("CREATE TABLE IF NOT EXISTS tenants (" +
" identifier VARCHAR(32) NOT NULL," +
" driver_class VARCHAR(255) NOT NULL," +
" database_name VARCHAR(32) NOT NULL," +
" host VARCHAR(512) NOT NULL," +
" port VARCHAR(5) NOT NULL," +
" a_user VARCHAR(32) NOT NULL," +
" pwd VARCHAR(32) NOT NULL," +
" PRIMARY KEY (identifier)" +
")");
this.createTableTenants(metaStatement, postgresDbName);
}
}
}

private void createTableTenants(final Statement statement, final String databaseName) throws SQLException {
this.logger.info("Create tenants table if it does not exists");
statement.execute("CREATE TABLE IF NOT EXISTS tenants (" +
" identifier VARCHAR(32) NOT NULL," +
" driver_class VARCHAR(255) NOT NULL," +
" database_name VARCHAR(32) NOT NULL," +
" host VARCHAR(512) NOT NULL," +
" port VARCHAR(5) NOT NULL," +
" a_user VARCHAR(32) NOT NULL," +
" pwd VARCHAR(32) NOT NULL," +
" PRIMARY KEY (identifier)" +
")");
}
}
@@ -18,6 +18,7 @@
*/
package org.apache.fineract.cn.provisioner.internal.repository;

import org.apache.fineract.cn.postgresql.util.PostgreSQLConstants;
import org.apache.fineract.cn.provisioner.api.v1.domain.DatabaseConnectionInfo;

import java.sql.Connection;
@@ -83,28 +84,13 @@ List<TenantDAO> collect() throws SQLException {
private static final int INDEX_PASSWORD = 7;

private static final String TABLE_NAME = "tenants";
private static final String META_KEYSPACE = "seshat"; //TODO: read MariaDB name from the configuration.
private static final String FETCH_ALL_STMT = " SELECT * FROM " +
META_KEYSPACE +
"." +
TenantDAO.TABLE_NAME;
private static final String FIND_ONE_STMT = " SELECT * FROM " +
META_KEYSPACE +
"." +
TenantDAO.TABLE_NAME +
" WHERE identifier = ?";
private static final String INSERT_STMT = " INSERT INTO " +
META_KEYSPACE +
"." +
TenantDAO.TABLE_NAME +
private static final String FETCH_ALL_STMT = " SELECT * FROM " + TenantDAO.TABLE_NAME;
private static final String FIND_ONE_STMT = " SELECT * FROM " + TenantDAO.TABLE_NAME + " WHERE identifier = ?";
private static final String INSERT_STMT = " INSERT INTO " + TenantDAO.TABLE_NAME +
" (identifier, driver_class, database_name, host, port, a_user, pwd) " +
" values " +
" (?, ?, ?, ?, ?, ?, ?) ";
private static final String DELETE_STMT = " DELETE FROM " +
META_KEYSPACE +
"." +
TenantDAO.TABLE_NAME +
" WHERE identifier = ? ";
private static final String DELETE_STMT = " DELETE FROM " + TenantDAO.TABLE_NAME + " WHERE identifier = ? ";

private String identifier;
private String driverClass;
@@ -18,6 +18,7 @@
*/
package org.apache.fineract.cn.provisioner.internal.service;

import org.apache.fineract.cn.postgresql.util.PostgreSQLConstants;
import org.apache.fineract.cn.provisioner.api.v1.domain.CassandraConnectionInfo;
import org.apache.fineract.cn.provisioner.api.v1.domain.DatabaseConnectionInfo;
import org.apache.fineract.cn.provisioner.api.v1.domain.Tenant;
@@ -30,6 +31,7 @@
import org.apache.fineract.cn.provisioner.internal.util.DataSourceUtils;
import org.apache.fineract.cn.provisioner.internal.util.DataStoreOption;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
@@ -49,7 +51,7 @@
@SuppressWarnings({"SqlNoDataSourceInspection", "SqlDialectInspection"})
@Component
public class TenantService {
private static final String META_KEYSPACE = "seshat"; //TODO: read MariaDB name from the configuration.
private static final String META_KEYSPACE = PostgreSQLConstants.POSTGRESQL_DATABASE_NAME_DEFAULT;

private final Logger logger;
private final Environment environment;
@@ -165,7 +167,7 @@ private void fetchAllDatabase(final ArrayList<Tenant> tenants) {
final DataStoreOption dataStoreOption = provisionerProperties.getDataStoreOption();
if (dataStoreOption.isEnabled(DataStoreOption.RDBMS)) {
if (tenants.size() > 0) {
try (final Connection connection = DataSourceUtils.createProvisionerConnection(this.environment)) {
try (final Connection connection = DataSourceUtils.createProvisionerConnection(this.environment, META_KEYSPACE)) {
for (final Tenant tenant : tenants) {
final Optional<TenantDAO> optionalTenantDAO = TenantDAO.find(connection, tenant.getIdentifier());
optionalTenantDAO.ifPresent(tenantDAO -> tenant.setDatabaseConnectionInfo(tenantDAO.map()));
@@ -175,7 +177,7 @@ private void fetchAllDatabase(final ArrayList<Tenant> tenants) {
throw new IllegalStateException("Could not load org.apache.fineract.cn.provisioner.tenant data!");
}
} else {
try (final Connection connection = DataSourceUtils.createProvisionerConnection(this.environment)) {
try (final Connection connection = DataSourceUtils.createProvisionerConnection(this.environment, META_KEYSPACE)) {
final List<TenantDAO> tenantDAOs = TenantDAO.fetchAll(connection);
for (final TenantDAO tenantDAO : tenantDAOs) {
final Tenant tenant = new Tenant();
@@ -210,7 +212,7 @@ private Optional<Tenant> findCassandra(final String identifier) {
private Tenant findInDatabase(final @Nonnull Tenant tenant, final @Nonnull String identifier) {
final DataStoreOption dataStoreOption = provisionerProperties.getDataStoreOption();
if (dataStoreOption.isEnabled(DataStoreOption.RDBMS)) {
try (final Connection connection = DataSourceUtils.createProvisionerConnection(this.environment)) {
try (final Connection connection = DataSourceUtils.createProvisionerConnection(this.environment, META_KEYSPACE)) {
final Optional<TenantDAO> optionalTenantDAO = TenantDAO.find(connection, identifier);
if (optionalTenantDAO.isPresent()) {
tenant.setDatabaseConnectionInfo(optionalTenantDAO.get().map());
@@ -229,45 +231,36 @@ private void initializeDatabase(final Tenant tenant) {
if (dataStoreOption.isEnabled(DataStoreOption.RDBMS)) {

try (
final Connection provisionerConnection = DataSourceUtils.createProvisionerConnection(this.environment);
final Connection provisionerConnection = DataSourceUtils.createProvisionerConnection(this.environment, META_KEYSPACE);
final Statement statement = provisionerConnection.createStatement()
) {
final java.sql.ResultSet resultSet = statement.executeQuery(
" SELECT * FROM " + META_KEYSPACE + ".tenants WHERE identifier = '" + tenant.getIdentifier() + "' ");
final ResultSet resultSet = statement.executeQuery(
" SELECT * FROM tenants WHERE identifier = '" + tenant.getIdentifier() + "' ");
if (resultSet.next()) {
this.logger.warn("Tenant {} already exists !", tenant.getIdentifier());
throw ServiceException.conflict("Tenant {0} already exists!", tenant.getIdentifier());
}
} catch (SQLException sqlex) {
this.logger.error(sqlex.getMessage(), sqlex);
throw new IllegalStateException("Could not insert org.apache.fineract.cn.provisioner.tenant info!", sqlex);
}
final DatabaseConnectionInfo databaseConnectionInfo = tenant.getDatabaseConnectionInfo();
try (
final Connection connection = DataSourceUtils.create(databaseConnectionInfo);
final Statement statement = connection.createStatement()
) {
statement.execute("CREATE DATABASE IF NOT EXISTS " + databaseConnectionInfo.getDatabaseName());
statement.close();
} catch (final SQLException sqlex) {
this.logger.error(sqlex.getMessage(), sqlex);
throw new IllegalStateException("Could not create database!", sqlex);
}
else {
final DatabaseConnectionInfo databaseConnectionInfo = tenant.getDatabaseConnectionInfo();
this.logger.info("Create database for tenant {}", tenant.getIdentifier());
statement.execute("CREATE DATABASE " + databaseConnectionInfo.getDatabaseName().toLowerCase());

try (final Connection provisionerConnection = DataSourceUtils.createProvisionerConnection(this.environment)) {
final TenantDAO tenantDAO = new TenantDAO();
tenantDAO.setIdentifier(tenant.getIdentifier());
tenantDAO.setDriverClass(databaseConnectionInfo.getDriverClass());
tenantDAO.setDatabaseName(databaseConnectionInfo.getDatabaseName());
tenantDAO.setHost(databaseConnectionInfo.getHost());
tenantDAO.setPort(databaseConnectionInfo.getPort());
tenantDAO.setUser(databaseConnectionInfo.getUser());
tenantDAO.setPassword(databaseConnectionInfo.getPassword());
tenantDAO.insert(provisionerConnection);
provisionerConnection.commit();
final TenantDAO tenantDAO = new TenantDAO();
tenantDAO.setIdentifier(tenant.getIdentifier());
tenantDAO.setDriverClass(databaseConnectionInfo.getDriverClass());
tenantDAO.setDatabaseName(databaseConnectionInfo.getDatabaseName());
tenantDAO.setHost(databaseConnectionInfo.getHost());
tenantDAO.setPort(databaseConnectionInfo.getPort());
tenantDAO.setUser(databaseConnectionInfo.getUser());
tenantDAO.setPassword(databaseConnectionInfo.getPassword());
tenantDAO.insert(provisionerConnection);
}
} catch (SQLException sqlex) {
this.logger.error(sqlex.getMessage(), sqlex);
throw new IllegalStateException("Could not insert org.apache.fineract.cn.provisioner.tenant info!", sqlex);
throw new IllegalStateException("Could not provision database for tenant {}" + tenant.getIdentifier(), sqlex);
}
} else {
this.logger.warn("Datastore option not chosen, Tenant in PostgreSQL RDBMS not created");
}
}

@@ -286,8 +279,7 @@ private void deleteFromCassandra(final @Nonnull String identifier) {
private void deleteDatabase(final String identifier) {
final DataStoreOption dataStoreOption = provisionerProperties.getDataStoreOption();
if (dataStoreOption.isEnabled(DataStoreOption.RDBMS)) {

try (final Connection provisionerConnection = DataSourceUtils.createProvisionerConnection(this.environment)) {
try (final Connection provisionerConnection = DataSourceUtils.createProvisionerConnection(this.environment, META_KEYSPACE)) {
final Optional<TenantDAO> optionalTenantDAO = TenantDAO.find(provisionerConnection, identifier);
if (optionalTenantDAO.isPresent()) {
final DatabaseConnectionInfo databaseConnectionInfo = optionalTenantDAO.get().map();
@@ -296,15 +288,15 @@ private void deleteDatabase(final String identifier) {
final Statement dropStatement = connection.createStatement()
) {
dropStatement.execute("DROP DATABASE " + databaseConnectionInfo.getDatabaseName());
connection.commit();
}
TenantDAO.delete(provisionerConnection, identifier);
provisionerConnection.commit();
}
} catch (final SQLException sqlex) {
this.logger.error(sqlex.getMessage(), sqlex);
throw new IllegalStateException("Could not delete database!");
throw new IllegalStateException("Could not delete database {}!" + identifier);
}
} else {
this.logger.warn("Datastore option not chosen, Tenant in PostgreSQL RDBMS not created");
}
}

@@ -59,10 +59,10 @@ public static Connection create(final DatabaseConnectionInfo databaseConnectionI
}
}

public static Connection createProvisionerConnection(final Environment environment) {
public static Connection createProvisionerConnection(final Environment environment, String databaseName) {
final DatabaseConnectionInfo databaseConnectionInfo = new DatabaseConnectionInfo();
databaseConnectionInfo.setDriverClass(environment.getProperty("postgresql.driverClass"));
databaseConnectionInfo.setDatabaseName(environment.getProperty("postgresql.database"));
databaseConnectionInfo.setDatabaseName(environment.getProperty(databaseName));
databaseConnectionInfo.setHost(environment.getProperty("postgresql.host"));
databaseConnectionInfo.setPort(environment.getProperty("postgresql.port"));
databaseConnectionInfo.setUser(environment.getProperty("postgresql.user"));

0 comments on commit b83331b

Please sign in to comment.