Skip to content

Commit

Permalink
FINERACT-984-6: PostgreSQL support
Browse files Browse the repository at this point in the history
  • Loading branch information
galovics authored and vidakovic committed Feb 22, 2022
1 parent 3472918 commit 8389106
Show file tree
Hide file tree
Showing 92 changed files with 3,505 additions and 655 deletions.
107 changes: 107 additions & 0 deletions docker-compose-postgresql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

# NB: Travis CI (dist: bionic) supports up to v3.7
# (unless we'd install a more recent version in .travis.yml)
version: '3.7'
services:
# Backend service
fineractpostgresql:
image: postgres:13.4
volumes:
- ./fineract-db/docker/postgresql:/docker-entrypoint-initdb.d/:Z,ro
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: skdcnwauicn2ucnaecasdsajdnizucawencascdca
FINERACT_DB_USER: postgres
FINERACT_DB_PASS: skdcnwauicn2ucnaecasdsajdnizucawencascdca
FINERACT_TENANTS_DB_NAME: fineract_tenants
FINERACT_TENANT_DEFAULT_DB_NAME: fineract_default
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
timeout: 10s
retries: 10
ports:
- "5432:5432"
fineract-server:
image: fineract:latest
volumes:
- ./fineract-provider/build/data:/data
healthcheck:
test: ["CMD", 'sh', '-c', 'echo -e "Checking for the availability of Fineract server deployment"; while ! nc -z "fineract-server" 8443; do sleep 1; printf "-"; done; echo -e " >> Fineract server has started";' ]
timeout: 10s
retries: 10
ports:
- 8443:8443
depends_on:
fineractpostgresql:
condition: service_healthy
environment:
# TODO: env vars prefixed with "fineract_tenants_*" will be removed with one of the next releases
#- fineract_tenants_driver=org.postgresql.Driver
#- fineract_tenants_url=jdbc:postgresql://fineractpostgresql:5432/fineract_tenants
#- fineract_tenants_uid=postgres
#- fineract_tenants_pwd=skdcnwauicn2ucnaecasdsajdnizucawencascdca
# NOTE: node aware scheduler
- FINERACT_NODE_ID=1
# NOTE: env vars prefixed "FINERACT_HIKARI_*" are used to configure the database connection pool
- FINERACT_HIKARI_DRIVER_SOURCE_CLASS_NAME=org.postgresql.Driver
- FINERACT_HIKARI_JDBC_URL=jdbc:postgresql://fineractpostgresql:5432/fineract_tenants
- FINERACT_HIKARI_USERNAME=postgres
- FINERACT_HIKARI_PASSWORD=skdcnwauicn2ucnaecasdsajdnizucawencascdca
# ... following variables are optional; "application.properties" contains reasonable defaults (same as here)
- FINERACT_HIKARI_MINIMUM_IDLE=3
- FINERACT_HIKARI_MAXIMUM_POOL_SIZE=10
- FINERACT_HIKARI_IDLE_TIMEOUT=60000
- FINERACT_HIKARI_CONNECTION_TIMEOUT=20000
- FINERACT_HIKARI_TEST_QUERY=SELECT 1
- FINERACT_HIKARI_AUTO_COMMIT=true
- FINERACT_HIKARI_DS_PROPERTIES_CACHE_PREP_STMTS=true
- FINERACT_HIKARI_DS_PROPERTIES_PREP_STMT_CACHE_SIZE=250
- FINERACT_HIKARI_DS_PROPERTIES_PREP_STMT_CACHE_SQL_LIMIT=2048
- FINERACT_HIKARI_DS_PROPERTIES_USE_SERVER_PREP_STMTS=true
- FINERACT_HIKARI_DS_PROPERTIES_USE_LOCAL_SESSION_STATE=true
- FINERACT_HIKARI_DS_PROPERTIES_REWRITE_BATCHED_STATEMENTS=true
- FINERACT_HIKARI_DS_PROPERTIES_CACHE_RESULT_SET_METADATA=true
- FINERACT_HIKARI_DS_PROPERTIES_CACHE_SERVER_CONFIGURATION=true
- FINERACT_HIKARI_DS_PROPERTIES_ELIDE_SET_AUTO_COMMITS=true
- FINERACT_HIKARI_DS_PROPERTIES_MAINTAIN_TIME_STATS=false
- FINERACT_HIKARI_DS_PROPERTIES_LOG_SLOW_QUERIES=true
- FINERACT_HIKARI_DS_PROPERTIES_DUMP_QUERIES_IN_EXCEPTION=true
# NOTE: env vars prefixed "FINERACT_DEFAULT_TENANTDB_*" are used to create the default tenant database
- FINERACT_DEFAULT_TENANTDB_HOSTNAME=fineractpostgresql
- FINERACT_DEFAULT_TENANTDB_PORT=5432
- FINERACT_DEFAULT_TENANTDB_UID=postgres
- FINERACT_DEFAULT_TENANTDB_PWD=skdcnwauicn2ucnaecasdsajdnizucawencascdca
- FINERACT_DEFAULT_TENANTDB_CONN_PARAMS=
- FINERACT_DEFAULT_TENANTDB_TIMEZONE=Asia/Kolkata
- FINERACT_DEFAULT_TENANTDB_IDENTIFIER=default
- FINERACT_DEFAULT_TENANTDB_NAME=fineract_default
- FINERACT_DEFAULT_TENANTDB_DESCRIPTION=Default Demo Tenant

# Frontend service
community-app:
image: openmf/community-app:latest
container_name: mifos-ui
restart: always
ports:
- 9090:80
depends_on:
fineract-server:
condition: service_healthy
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ services:
# NOTE: node aware scheduler
- FINERACT_NODE_ID=1
# NOTE: env vars prefixed "FINERACT_HIKARI_*" are used to configure the database connection pool
- FINERACT_HIKARI_DRIVER_CLASS_NAME=org.mariadb.jdbc.Driver
- FINERACT_HIKARI_DRIVER_SOURCE_CLASS_NAME=org.mariadb.jdbc.Driver
- FINERACT_HIKARI_JDBC_URL=jdbc:mariadb://fineractmysql:3306/fineract_tenants
- FINERACT_HIKARI_USERNAME=root
- FINERACT_HIKARI_PASSWORD=skdcnwauicn2ucnaecasdsajdnizucawencascdca
Expand Down
29 changes: 29 additions & 0 deletions fineract-db/docker/postgresql/01-init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

set -e
export PGPASSWORD=$POSTGRES_PASSWORD;
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER $FINERACT_DB_USER WITH PASSWORD '$FINERACT_DB_PASS';
CREATE DATABASE $FINERACT_TENANTS_DB_NAME;
CREATE DATABASE $FINERACT_TENANT_DEFAULT_DB_NAME;
GRANT ALL PRIVILEGES ON DATABASE $FINERACT_TENANTS_DB_NAME TO $FINERACT_DB_USER;
GRANT ALL PRIVILEGES ON DATABASE $FINERACT_TENANT_DEFAULT_DB_NAME TO $FINERACT_DB_USER;
EOSQL
3 changes: 3 additions & 0 deletions fineract-provider/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ configurations {
}
dependencies {
driver 'org.mariadb.jdbc:mariadb-java-client:2.7.4'
driver 'org.postgresql:postgresql:42.1.4'
}

URLClassLoader loader = GroovyObject.class.classLoader
Expand Down Expand Up @@ -237,6 +238,7 @@ bootRun {

dependencies {
implementation 'org.mariadb.jdbc:mariadb-java-client:2.7.4'
implementation 'org.postgresql:postgresql:42.1.4'
}

classpath += files("build/generated/swagger-ui")
Expand Down Expand Up @@ -316,6 +318,7 @@ jib {

dependencies {
implementation 'org.mariadb.jdbc:mariadb-java-client:2.7.4'
implementation 'org.postgresql:postgresql:42.1.4'
}

extraDirectories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.fineract.infrastructure.core.service.PaginationHelper;
import org.apache.fineract.infrastructure.core.service.RoutingDataSource;
import org.apache.fineract.infrastructure.core.service.SearchParameters;
import org.apache.fineract.infrastructure.core.service.database.DatabaseSpecificSQLGenerator;
import org.apache.fineract.infrastructure.security.utils.ColumnValidator;
import org.apache.fineract.organisation.monetary.data.CurrencyData;
import org.apache.fineract.organisation.office.data.OfficeData;
Expand Down Expand Up @@ -77,18 +78,22 @@ public class JournalEntryReadPlatformServiceImpl implements JournalEntryReadPlat
private final ColumnValidator columnValidator;
private final FinancialActivityAccountRepositoryWrapper financialActivityAccountRepositoryWrapper;

private final PaginationHelper<JournalEntryData> paginationHelper = new PaginationHelper<>();
private final PaginationHelper paginationHelper;
private final DatabaseSpecificSQLGenerator sqlGenerator;

@Autowired
public JournalEntryReadPlatformServiceImpl(final RoutingDataSource dataSource,
final GLAccountReadPlatformService glAccountReadPlatformService, final ColumnValidator columnValidator,
final OfficeReadPlatformService officeReadPlatformService,
final FinancialActivityAccountRepositoryWrapper financialActivityAccountRepositoryWrapper) {
final FinancialActivityAccountRepositoryWrapper financialActivityAccountRepositoryWrapper,
DatabaseSpecificSQLGenerator sqlGenerator, PaginationHelper paginationHelper) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.glAccountReadPlatformService = glAccountReadPlatformService;
this.officeReadPlatformService = officeReadPlatformService;
this.financialActivityAccountRepositoryWrapper = financialActivityAccountRepositoryWrapper;
this.columnValidator = columnValidator;
this.paginationHelper = paginationHelper;
this.sqlGenerator = sqlGenerator;
}

private static final class GLJournalEntryMapper implements RowMapper<JournalEntryData> {
Expand Down Expand Up @@ -250,7 +255,7 @@ public Page<JournalEntryData> retrieveAll(final SearchParameters searchParameter

GLJournalEntryMapper rm = new GLJournalEntryMapper(associationParametersData);
final StringBuilder sqlBuilder = new StringBuilder(200);
sqlBuilder.append("select SQL_CALC_FOUND_ROWS ");
sqlBuilder.append("select " + sqlGenerator.calcFoundRows() + " ");
sqlBuilder.append(rm.schema());

final Object[] objectArray = new Object[15];
Expand Down Expand Up @@ -304,28 +309,19 @@ public Page<JournalEntryData> retrieveAll(final SearchParameters searchParameter
String fromDateString = null;
String toDateString = null;
if (fromDate != null && toDate != null) {
sqlBuilder.append(whereClose + " journalEntry.entry_date between ? and ? ");

whereClose = " and ";

fromDateString = df.format(fromDate);
toDateString = df.format(toDate);
objectArray[arrayPos] = fromDateString;
arrayPos = arrayPos + 1;
objectArray[arrayPos] = toDateString;
arrayPos = arrayPos + 1;
sqlBuilder.append(whereClose + " journalEntry.entry_date between '" + fromDateString + "' and '" + toDateString + "' ");

whereClose = " and ";
} else if (fromDate != null) {
sqlBuilder.append(whereClose + " journalEntry.entry_date >= ? ");
fromDateString = df.format(fromDate);
objectArray[arrayPos] = fromDateString;
arrayPos = arrayPos + 1;
sqlBuilder.append(whereClose + " journalEntry.entry_date >= '" + fromDateString + "' ");
whereClose = " and ";

} else if (toDate != null) {
sqlBuilder.append(whereClose + " journalEntry.entry_date <= ? ");
toDateString = df.format(toDate);
objectArray[arrayPos] = toDateString;
arrayPos = arrayPos + 1;
sqlBuilder.append(whereClose + " journalEntry.entry_date <= '" + toDateString + "' ");

whereClose = " and ";
}
Expand Down Expand Up @@ -368,15 +364,16 @@ public Page<JournalEntryData> retrieveAll(final SearchParameters searchParameter
}

if (searchParameters.isLimited()) {
sqlBuilder.append(" limit ").append(searchParameters.getLimit());
sqlBuilder.append(" ");
if (searchParameters.isOffset()) {
sqlBuilder.append(" offset ").append(searchParameters.getOffset());
sqlBuilder.append(sqlGenerator.limit(searchParameters.getLimit(), searchParameters.getOffset()));
} else {
sqlBuilder.append(sqlGenerator.limit(searchParameters.getLimit()));
}
}

final Object[] finalObjectArray = Arrays.copyOf(objectArray, arrayPos);
final String sqlCountRows = "SELECT FOUND_ROWS()";
return this.paginationHelper.fetchPage(this.jdbcTemplate, sqlCountRows, sqlBuilder.toString(), finalObjectArray, rm);
return this.paginationHelper.fetchPage(this.jdbcTemplate, sqlBuilder.toString(), finalObjectArray, rm);
}

@Override
Expand Down Expand Up @@ -530,9 +527,8 @@ public Page<JournalEntryData> retrieveJournalEntriesByEntityId(String transactio
final GLJournalEntryMapper rm = new GLJournalEntryMapper(associationParametersData);
final String sql = "select " + rm.schema()
+ " where journalEntry.transaction_id = ? and journalEntry.entity_id = ? and journalEntry.entity_type_enum = ?";
final String sqlCountRows = "SELECT FOUND_ROWS()";
Object[] data = { transactionId, entityId, entityType };
return this.paginationHelper.fetchPage(this.jdbcTemplate, sqlCountRows, sql, data, rm);
return this.paginationHelper.fetchPage(this.jdbcTemplate, sql, data, rm);
} catch (final EmptyResultDataAccessException e) {
throw new JournalEntriesNotFoundException(entityId, e);
}
Expand Down
Loading

0 comments on commit 8389106

Please sign in to comment.