Skip to content
Permalink
Browse files
Fixing creation of database seshat and table tenants
  • Loading branch information
Izakey committed May 2, 2019
1 parent b83331b commit 56155da12c9e4426686bd8abc2b949d7c9449938
Showing 3 changed files with 47 additions and 28 deletions.
@@ -32,6 +32,7 @@
import java.util.Date;
import java.util.UUID;
import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.apache.fineract.cn.api.util.ApiConstants;
import org.apache.fineract.cn.cassandra.core.CassandraSessionProvider;
@@ -83,6 +84,7 @@ public void initialize() {

this.initializeCassandra();
this.initializeDatabase(PostgreSQLConstants.POSTGRESQL_DATABASE_NAME_DEFAULT);
this.createTableTenants();
//this.initializeDatabase("playground");
} catch (final Exception ex) {
throw new IllegalStateException("Could not initialize service!", ex);
@@ -200,37 +202,54 @@ private void initializeDatabase(String postgresDbName) throws Exception {

this.logger.info("Creating meta database {} ", postgresDbName);
try (
final Connection connection = DataSourceUtils.createProvisionerConnection(this.environment, postgresDbName);
final Connection connection = DataSourceUtils.createProvisionerConnection(this.environment, "postgres");
final Statement testStatement = connection.createStatement();
final Statement statement = connection.createStatement()
) {
) {
final ResultSet validityQuery = testStatement.executeQuery("SELECT 1");
if (validityQuery.next()){
this.logger.info("Connection to database postgres established");
final ResultSet findDB = statement.executeQuery("SELECT datname FROM pg_database WHERE datname = '" + postgresDbName + "'");
if (!findDB.next()) {
this.logger.info("Database {} does not exists, creating the database now.", postgresDbName);
this.logger.info("Database {} does not exists, creating the database {} now.", postgresDbName);
statement.execute("CREATE DATABASE " + postgresDbName);
} else {
this.logger.info("Sorry, Database {} does exists.", postgresDbName);
this.logger.info("Database {} already exists.", postgresDbName);
}

try (
final Connection metaConnection = DataSourceUtils.createProvisionerConnection(this.environment, postgresDbName);
final Statement metaStatement = metaConnection.createStatement()
) {
this.createTableTenants(metaStatement, postgresDbName);
} else {
this.logger.warn("Could not connect to database postgres");
throw new IllegalMonitorStateException("Could not connect to database postgres");
}
}
}

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)" +
")");
private void createTableTenants() throws SQLException {
final String databaseName = PostgreSQLConstants.POSTGRESQL_DATABASE_NAME_DEFAULT;

this.logger.info("Create tenants table in database {} if it does not exists", databaseName);
try (
final Connection provisionerConnection = DataSourceUtils.createProvisionerConnection(this.environment, databaseName);
final Statement testStatement = provisionerConnection.createStatement();
final Statement findSeshatStatement = provisionerConnection.createStatement()
) {
final ResultSet validityQuery = testStatement.executeQuery("SELECT 1");
if (validityQuery.next()) {
this.logger.info("Connection to database {} established", databaseName);
final ResultSet resultSet = findSeshatStatement.executeQuery("SELECT datname FROM pg_database where datname = '"+ databaseName +"'");
if (resultSet.next()) {
this.logger.info("Database {} exists !", databaseName);
this.logger.info("Creating table tenants now");
findSeshatStatement.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))");
} else {
this.logger.warn("Database {} does not exists !", databaseName);
}
} else {
this.logger.warn("Could not connect to database seshat");
throw new IllegalMonitorStateException("Could not connect to database seshat");
}
} catch (SQLException sqlex) {
this.logger.error(sqlex.getMessage(), sqlex);
throw new IllegalStateException("Could not create table tenants");
}
}
}
}
@@ -67,12 +67,13 @@ public static Connection createProvisionerConnection(final Environment environme
databaseConnectionInfo.setPort(environment.getProperty("postgresql.port"));
databaseConnectionInfo.setUser(environment.getProperty("postgresql.user"));
databaseConnectionInfo.setPassword(environment.getProperty("postgresql.password"));
final Connection connection = DataSourceUtils.create(databaseConnectionInfo);

try {
final Connection connection = DataSourceUtils.create(databaseConnectionInfo);
connection.setAutoCommit(true);
} catch (SQLException e) {
// do nothing
return connection;
} catch (SQLException error) {
throw new IllegalStateException(error.getMessage(), error);
}
return connection;
}
}
@@ -48,7 +48,6 @@ postgresql:
spring:
datasource:
driver-class-name: org.postgresql.Driver
platform: postgres
url: jdbc:postgresql://localhost:5432/seshat
username: postgres
password: postgres

0 comments on commit 56155da

Please sign in to comment.