Skip to content
Permalink
Browse files
Merge pull request #6 from Izakey/FINCN-129
Migrate provisioner from MariaDB to PostgreSQL
  • Loading branch information
Izakey committed Sep 2, 2019
2 parents 336042b + a06a654 commit 4327ce032c97cc961ce15e39c22863654cefc230
Showing 15 changed files with 172 additions and 131 deletions.
@@ -1,5 +1,5 @@
Apache Fineract CN Provisioner
Copyright [2017-2018] The Apache Software Foundation
Copyright [2017-2019] The Apache Software Foundation

This product includes software developed at
The Apache Software Foundation (http://www.apache.org/).
@@ -51,7 +51,8 @@ dependencies {
[group: 'org.apache.fineract.cn', name: 'lang', version: versions.frameworklang],
[group: 'org.springframework.boot', name: 'spring-boot-starter-test'],
[group: 'org.springframework.restdocs', name: 'spring-restdocs-mockmvc'],
[group: 'junit', name: 'junit', version: '4.12']
[group: 'junit', name: 'junit', version: '4.12'],
[group: 'com.opentable.components', name: 'otj-pg-embedded', version: '0.7.1']
)
}

@@ -59,15 +59,14 @@ public Logger logger() {
}
}


private static TestEnvironment testEnvironment = new TestEnvironment(APP_NAME);
private static ProvisionerMariaDBInitializer mariaDBInitializer = new ProvisionerMariaDBInitializer();
private static ProvisionerPostgreSQLInitializer postgreSQLInitializer = new ProvisionerPostgreSQLInitializer();
private static ProvisionerCassandraInitializer cassandraInitializer = new ProvisionerCassandraInitializer();

@ClassRule
public static TestRule orderClassRules = RuleChain
.outerRule(testEnvironment)
.around(mariaDBInitializer)
.around(postgreSQLInitializer)
.around(cassandraInitializer);

@SuppressWarnings("SpringAutowiredFieldsWarningInspection")
@@ -88,4 +87,4 @@ public static void setup() throws Exception {
protected String getClientId() {
return CLIENT_ID;
}
}
}
@@ -18,27 +18,32 @@
*/
package org.apache.fineract.cn.provisioner;

import ch.vorburger.exec.ManagedProcessException;
import ch.vorburger.mariadb4j.DB;
import com.opentable.db.postgres.embedded.EmbeddedPostgres;
import org.junit.rules.ExternalResource;

import java.io.IOException;

/**
* @author Myrle Krantz
*/
public class ProvisionerMariaDBInitializer extends ExternalResource {
private static DB EMBEDDED_MARIA_DB;
public class ProvisionerPostgreSQLInitializer extends ExternalResource {
private static EmbeddedPostgres EMBEDDED_POSTGRESQL_DB;
@Override
protected void before() throws ManagedProcessException {
EMBEDDED_MARIA_DB = DB.newEmbeddedDB(3306);
EMBEDDED_MARIA_DB.start();
protected void before() throws IOException {
try {
EMBEDDED_POSTGRESQL_DB = EmbeddedPostgres.builder().setPort(5432).start();
}
catch (IOException ioex) {
System.out.println(ioex);
}
}

@Override
protected void after() {
try {
EMBEDDED_MARIA_DB.stop();
} catch (ManagedProcessException e) {
throw new RuntimeException(e);
EMBEDDED_POSTGRESQL_DB.close();
} catch (IOException io) {
System.out.println(io);
}
}
}
@@ -25,7 +25,7 @@

import com.google.gson.Gson;
import org.apache.fineract.cn.provisioner.ProvisionerCassandraInitializer;
import org.apache.fineract.cn.provisioner.ProvisionerMariaDBInitializer;
import org.apache.fineract.cn.provisioner.ProvisionerPostgreSQLInitializer;
import org.apache.fineract.cn.provisioner.api.v1.client.Provisioner;
import org.apache.fineract.cn.provisioner.api.v1.domain.Application;
import org.apache.fineract.cn.provisioner.api.v1.domain.AssignedApplication;
@@ -124,15 +124,15 @@ public TokenProvider tokenProviderSpy(final @Qualifier("tokenProvider") TokenPro


private static TestEnvironment testEnvironment = new TestEnvironment(APP_NAME);
private static ProvisionerMariaDBInitializer mariaDBInitializer = new ProvisionerMariaDBInitializer();
private static ProvisionerPostgreSQLInitializer postgreSQLInitializer = new ProvisionerPostgreSQLInitializer();
private static ProvisionerCassandraInitializer cassandraInitializer = new ProvisionerCassandraInitializer();
private static SystemSecurityEnvironment systemSecurityEnvironment
= new SystemSecurityEnvironment(testEnvironment.getSystemKeyTimestamp(), testEnvironment.getSystemPublicKey(), testEnvironment.getSystemPrivateKey());

@ClassRule
public static TestRule orderClassRules = RuleChain
.outerRule(testEnvironment)
.around(mariaDBInitializer)
.around(postgreSQLInitializer)
.around(cassandraInitializer);

@SuppressWarnings("SpringAutowiredFieldsWarningInspection")
@@ -26,4 +26,11 @@ system:

activemq:
brokerUrl: vm://localhost?broker.persistent=false
concurrency: 3-10
concurrency: 3-10

spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/seshat
username: postgres
password: postgres
@@ -63,7 +63,6 @@ dependencies {
[group: 'org.apache.fineract.cn', name: 'lang', version: versions.frameworklang],
[group: 'org.apache.fineract.cn', name: 'async', version: versions.frameworkasync],
[group: 'org.apache.fineract.cn', name: 'cassandra', version: versions.frameworkcassandra],
[group: 'org.apache.fineract.cn', name: 'mariadb', version: versions.frameworkmariadb],
[group: 'org.hibernate', name: 'hibernate-validator', version: versions.validator],
[group: 'org.apache.fineract.cn', name: 'crypto', version: versions.frameworkcrypto],
)
@@ -29,7 +29,7 @@
import org.apache.fineract.cn.crypto.config.EnableCrypto;
import org.apache.fineract.cn.lang.config.EnableApplicationName;
import org.apache.fineract.cn.lang.config.EnableServiceException;
import org.apache.fineract.cn.mariadb.config.EnableMariaDB;
import org.apache.fineract.cn.postgresql.config.EnablePostgreSQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -55,7 +55,7 @@
@EnableCrypto
@EnableAsync
@EnableAnubis(provideSignatureRestController = false)
@EnableMariaDB
@EnablePostgreSQL
@EnableCassandra
@EnableServiceException
@EnableApplicationName
@@ -26,18 +26,20 @@
import org.apache.fineract.cn.provisioner.internal.util.DataSourceUtils;
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;
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;
import org.apache.fineract.cn.cassandra.util.CassandraConnectorConstants;
import org.apache.fineract.cn.crypto.HashGenerator;
import org.apache.fineract.cn.crypto.SaltGenerator;
import org.apache.fineract.cn.mariadb.util.MariaDBConstants;
import org.apache.fineract.cn.postgresql.util.PostgreSQLConstants;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -58,7 +60,6 @@ public class ProvisionerInitializer {
private final HashGenerator hashGenerator;
private final String initialClientId;
private String metaKeySpaceName;
private String mariaDBName;

@Autowired
public ProvisionerInitializer(final Environment environment, @Qualifier(ProvisionerConstants.LOGGER_NAME) final Logger logger,
@@ -80,12 +81,11 @@ public void initialize() {
metaKeySpaceName = this.environment.getProperty(
CassandraConnectorConstants.KEYSPACE_PROP,
CassandraConnectorConstants.KEYSPACE_PROP_DEFAULT);
mariaDBName = this.environment.getProperty(
MariaDBConstants.MARIADB_DATABASE_NAME_PROP,
MariaDBConstants.MARIADB_DATABASE_NAME_DEFAULT);

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

private void initializeDatabase() throws Exception {
final Connection connection = DataSourceUtils.createProvisionerConnection(this.environment);
try (final Statement statement = connection.createStatement()) {
// create meta data database
statement.execute("CREATE DATABASE IF NOT EXISTS " + mariaDBName);
statement.execute("USE " + mariaDBName);
// create tenants table
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)" +
")");
connection.commit();
} catch (final SQLException sqlex) {
throw new IllegalStateException(sqlex.getMessage(), sqlex);
} finally {
connection.close();
private void initializeDatabase(String postgresDbName) throws Exception {

this.logger.info("Creating meta database {} ", postgresDbName);
try (
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);
statement.execute("CREATE DATABASE " + postgresDbName);
} else {
this.logger.info("Database {} already exists.", postgresDbName);
}
} else {
this.logger.warn("Could not connect to database postgres");
throw new IllegalMonitorStateException("Could not connect to database postgres");
}
}
}

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");
}
}
}
}
@@ -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;

0 comments on commit 4327ce0

Please sign in to comment.