Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DarioGii committed Jan 6, 2025
1 parent aadbaa2 commit c5213ba
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
stirling-pdf:
container_name: Stirling-PDF-Security-Fat-Postgres
image: stirlingtools/stirling-pdf:latest-fat
image: stirlingtools/stirling-pdf:latest-fat-postgres
deploy:
resources:
limits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class DatabaseConfig {

public DatabaseConfig(ApplicationProperties applicationProperties, boolean runningEE) {
this.applicationProperties = applicationProperties;
this.runningEE = true; // fixMe: change back
this.runningEE = runningEE;
}

/**
Expand Down Expand Up @@ -59,6 +59,10 @@ public DataSource dataSource() throws UnsupportedProviderException {
log.info("Using custom database configuration");

if (!datasource.getCustomDatabaseUrl().isBlank()) {
if (datasource.getCustomDatabaseUrl().contains("postgresql")) {
dataSourceBuilder.driverClassName(POSTGRES_DRIVER);
}

dataSourceBuilder.url(datasource.getCustomDatabaseUrl());
} else {
dataSourceBuilder.driverClassName(getDriverClassName(datasource.getType()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Expand All @@ -20,11 +21,8 @@

import javax.sql.DataSource;

import org.springframework.core.io.PathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.CannotReadScriptException;
import org.springframework.jdbc.datasource.init.ScriptException;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
Expand All @@ -50,13 +48,14 @@ public DatabaseService(ApplicationProperties applicationProperties, DataSource d
}

/**
* Checks if there is at least one backup
* Checks if there is at least one backup. First checks if the directory exists, then checks if
* there are backup scripts within the directory
*
* @return true if there are backup scripts, false if there are not
*/
@Override
public boolean hasBackup() {
Path filePath = Paths.get(BACKUP_DIR + "*");
Path filePath = Paths.get(BACKUP_DIR);

if (Files.exists(filePath)) {
return !getBackupList().isEmpty();
Expand Down Expand Up @@ -116,9 +115,11 @@ public void importDatabase() {
if (!hasBackup()) throw new BackupNotFoundException("No backup scripts were found.");

List<FileInfo> backupList = this.getBackupList();

backupList.sort(Comparator.comparing(FileInfo::getModificationDate).reversed());
executeDatabaseScript(Paths.get(backupList.get(0).getFilePath()));

Path latestExport = Paths.get(backupList.get(0).getFilePath());

executeDatabaseScript(latestExport);
}

/** Imports a database backup from the specified file. */
Expand Down Expand Up @@ -149,7 +150,6 @@ public boolean importDatabaseFromUI(Path tempTemplatePath) throws IOException {
return true;
}

/** Filter and delete old backups if there are more than 5 */
@Override
public void exportDatabase() {
List<FileInfo> filteredBackupList =
Expand All @@ -166,16 +166,21 @@ public void exportDatabase() {
Path insertOutputFilePath =
this.getBackupFilePath(BACKUP_PREFIX + dateNow.format(myFormatObj) + SQL_SUFFIX);

try (Connection conn = dataSource.getConnection()) {
ScriptUtils.executeSqlScript(
conn, new EncodedResource(new PathResource(insertOutputFilePath)));
if (isH2Database()) {
String query = "SCRIPT SIMPLE COLUMNS DROP to ?;";

log.info("Database export completed: {}", insertOutputFilePath);
} catch (SQLException e) {
log.error("Error during database export: {}", e.getMessage(), e);
} catch (CannotReadScriptException e) {
log.error("Error during database export: File {} not found", insertOutputFilePath);
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, insertOutputFilePath.toString());
stmt.execute();
} catch (SQLException e) {
log.error("Error during database export: {}", e.getMessage(), e);
} catch (CannotReadScriptException e) {
log.error("Error during database export: File {} not found", insertOutputFilePath);
}
}

log.info("Database export completed: {}", insertOutputFilePath);
}

private static void deleteOldestBackup(List<FileInfo> filteredBackupList) {
Expand Down Expand Up @@ -259,17 +264,20 @@ public Path getBackupFilePath(String fileName) {

private void executeDatabaseScript(Path scriptPath) {
if (isH2Database()) {
try (Connection conn = dataSource.getConnection()) {
ScriptUtils.executeSqlScript(
conn, new EncodedResource(new PathResource(scriptPath)));
String query = "RUNSCRIPT from ?;";

log.info("Database import completed: {}", scriptPath);
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(query)) {
stmt.setString(1, scriptPath.toString());
stmt.execute();
} catch (SQLException e) {
log.error("Error during database import: {}", e.getMessage(), e);
} catch (ScriptException e) {
log.error("Error: File {} not found", scriptPath.toString(), e);
}
}

log.info("Database import completed: {}", scriptPath);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;
import lombok.Data;

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spring.thymeleaf.encoding=UTF-8
spring.web.resources.mime-mappings.webmanifest=application/manifest+json
spring.mvc.async.request-timeout=${SYSTEM_CONNECTIONTIMEOUTMILLISECONDS:1200000}

spring.datasource.url=jdbc:h2:file:./configs/stirling-pdf-DB-2.3.232;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.url=jdbc:h2:file:./configs/stirling-pdf-DB-2.3.232;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=PostgreSQL
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/settings.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ system:
tessdataDir: /usr/share/tessdata # path to the directory containing the Tessdata files. This setting is relevant for Windows systems. For Windows users, this path should be adjusted to point to the appropriate directory where the Tessdata files are stored.
enableAnalytics: undefined # set to 'true' to enable analytics, set to 'false' to disable analytics; for enterprise users, this is set to true
datasource:
enableCustomDatabase: true # set this property to 'true' if you would like to use your own custom database configuration
enableCustomDatabase: false # set this property to 'true' if you would like to use your own custom database configuration
customDatabaseUrl: jdbc:postgresql://localhost:5432/postgres # set the url for your own custom database connection. If provided, the type, hostName, port and name are not necessary and will not be used
username: postgres # set the database username
password: postgres # set the database password
type: postgresql # the type of the database to set (e.g. 'h2', 'postgresql')
hostName: localhost # the host name to use for the database url. Set to 'localhost' when running the app locally. Set to match the name of the container name of your database container when running the app on a server (Docker configuration)
port: 5432 # set the port number of the database. Ensure this matches the port the database is listening to
name: postgres # set the name of your database. Should match the name of the database you create
username: postgres # set the database username
password: postgres # set the database password

ui:
appName: '' # application's visible name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package stirling.software.SPDF.config.security.database;

import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import stirling.software.SPDF.model.ApplicationProperties;
import stirling.software.SPDF.model.provider.UnsupportedProviderException;

import javax.sql.DataSource;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -32,7 +29,6 @@ void setUp() {
}

@Test
@Disabled
void testDataSource_whenRunningEEIsFalse() throws UnsupportedProviderException {
databaseConfig = new DatabaseConfig(applicationProperties, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import stirling.software.SPDF.model.ApplicationProperties;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class DatabaseServiceTest {

public static final String TEST_FILE = "test";
public static final String TEST_FILE = "test.txt";
private final String BACKUP_PATH = "configs/db/backup/";

@Mock
Expand All @@ -33,33 +30,22 @@ class DatabaseServiceTest {
@InjectMocks
private DatabaseService databaseService;

@BeforeEach
void setUp() throws IOException {
Files.deleteIfExists(Paths.get(BACKUP_PATH + TEST_FILE));
}

@Test
void testHasNoBackups() {
ApplicationProperties.System system = mock(ApplicationProperties.System.class);
ApplicationProperties.Datasource datasource = mock(ApplicationProperties.Datasource.class);

when(applicationProperties.getSystem()).thenReturn(system);
when(system.getDatasource()).thenReturn(datasource);
when(datasource.isEnableCustomDatabase()).thenReturn(false);

assertFalse(databaseService.hasBackup());
}

@Test
@Disabled
void testHasBackups() throws IOException {
Path backupDir = Paths.get(BACKUP_PATH);
Files.createDirectories(backupDir);
Path testFile = Paths.get(BACKUP_PATH + TEST_FILE);
ApplicationProperties.System system = mock(ApplicationProperties.System.class);
ApplicationProperties.Datasource datasource = mock(ApplicationProperties.Datasource.class);

Files.createFile(testFile);
Files.createTempFile(backupDir, TEST_FILE, null);

when(applicationProperties.getSystem()).thenReturn(system);
when(system.getDatasource()).thenReturn(datasource);
when(datasource.isEnableCustomDatabase()).thenReturn(false);

assertTrue(databaseService.hasBackup());

Files.deleteIfExists(testFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import stirling.software.SPDF.SPDFApplication;
Expand All @@ -13,7 +12,6 @@
import static java.nio.file.Files.exists;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Disabled
@SpringBootTest
public class SPDFApplicationIntegrationTest {

Expand Down

0 comments on commit c5213ba

Please sign in to comment.