8
8
import java .nio .file .attribute .BasicFileAttributes ;
9
9
import java .sql .Connection ;
10
10
import java .sql .DriverManager ;
11
- import java .sql .PreparedStatement ;
12
11
import java .sql .ResultSet ;
13
12
import java .sql .SQLException ;
14
13
import java .sql .Statement ;
@@ -40,75 +39,25 @@ public class DatabaseBackupHelper implements DatabaseBackupInterface {
40
39
public static final String BACKUP_PREFIX = "backup_" ;
41
40
public static final String SQL_SUFFIX = ".sql" ;
42
41
42
+ @ Value ("${dbType:postgresql}" )
43
+ private String dbType ;
44
+
43
45
@ Value ("${spring.datasource.url}" )
44
46
private String url ;
45
47
46
48
@ Value ("${spring.datasource.username}" )
47
- private String databaseUsername ;
49
+ private String username ;
48
50
49
51
@ Value ("${spring.datasource.password}" )
50
- private String databasePassword ;
51
-
52
- @ Value ("${spring.datasource.stirling.url}" )
53
- private String stirlingUrl ;
54
-
55
- @ Value ("${spring.datasource.stirling.username}" )
56
- private String stirlingDatabaseUsername ;
57
-
58
- @ Value ("${spring.datasource.stirling.password}" )
59
- private String stirlingDatabasePassword ;
52
+ private String password ;
60
53
61
54
private final Path BACKUP_PATH = Paths .get ("configs/db/backup/" );
62
55
63
- // fixMe: should check if backups exist without returning the whole list
64
- @ Override
65
- public void initDatabase () {
66
- log .info ("Creating database stirling-pdf-DB" );
67
-
68
- String initDBAndRoleScript =
69
- """
70
- CREATE DATABASE "stirling-pdf-DB";
71
- CREATE USER %s WITH ENCRYPTED PASSWORD '%s';
72
- ALTER DATABASE "stirling-pdf-DB" OWNER TO %s;
73
- GRANT ALL PRIVILEGES ON DATABASE "stirling-pdf-DB" TO %s;
74
- """
75
- .formatted (
76
- stirlingDatabaseUsername ,
77
- stirlingDatabasePassword ,
78
- stirlingDatabaseUsername ,
79
- stirlingDatabaseUsername );
80
- try (Connection conn =
81
- DriverManager .getConnection (url , databaseUsername , databasePassword );
82
- PreparedStatement initStmt = conn .prepareStatement (initDBAndRoleScript )) {
83
- initStmt .execute ();
84
-
85
- String setRoleScript = "SET ROLE " + stirlingDatabaseUsername + ";" ;
86
-
87
- try (Connection stirlingDBConn =
88
- DriverManager .getConnection (
89
- stirlingUrl ,
90
- stirlingDatabaseUsername ,
91
- stirlingDatabasePassword );
92
- PreparedStatement stmt = conn .prepareStatement (setRoleScript )) {
93
- stmt .execute ();
94
-
95
- log .info ("Database stirling-pdf-DB created" );
96
- log .info ("User admin created" );
97
-
98
- ensureBackupDirectoryExists ();
99
- } catch (SQLException e ) {
100
- log .error ("Failed to set admin to stirling-pdf-DB: {}" , e .getMessage (), e );
101
- }
102
- } catch (SQLException e ) {
103
- log .error ("Failed to create stirling-pdf-DB: {}" , e .getMessage (), e );
104
- }
105
- }
106
-
107
56
@ Override
108
57
public boolean hasBackup () {
109
58
// Check if there is at least one backup
110
59
try (Stream <Path > entries = Files .list (BACKUP_PATH )) {
111
- return entries .findFirst ().isEmpty ();
60
+ return entries .findFirst ().isPresent ();
112
61
} catch (IOException e ) {
113
62
log .error ("Error reading backup directory: {}" , e .getMessage (), e );
114
63
throw new RuntimeException (e );
@@ -147,6 +96,7 @@ public List<FileInfo> getBackupList() {
147
96
} catch (IOException e ) {
148
97
log .error ("Error reading backup directory: {}" , e .getMessage (), e );
149
98
}
99
+
150
100
return backupFiles ;
151
101
}
152
102
@@ -156,7 +106,6 @@ public boolean importDatabaseFromUI(String fileName) throws IOException {
156
106
importDatabaseFromUI (getBackupFilePath (fileName ));
157
107
return true ;
158
108
} catch (IOException e ) {
159
- // fixme: do we want to show the filename here?
160
109
log .error (
161
110
"Error importing database from file: {}, message: {}" ,
162
111
fileName ,
@@ -205,9 +154,7 @@ public void exportDatabase() {
205
154
Path insertOutputFilePath =
206
155
this .getBackupFilePath (BACKUP_PREFIX + dateNow .format (myFormatObj ) + SQL_SUFFIX );
207
156
208
- try (Connection conn =
209
- DriverManager .getConnection (
210
- stirlingUrl , stirlingDatabaseUsername , stirlingDatabasePassword )) {
157
+ try (Connection conn = DriverManager .getConnection (url , username , password )) {
211
158
ScriptUtils .executeSqlScript (
212
159
conn , new EncodedResource (new PathResource (insertOutputFilePath )));
213
160
@@ -236,8 +183,7 @@ private static void deleteOldestBackup(List<FileInfo> filteredBackupList) {
236
183
// Retrieves the H2 database version.
237
184
public String getH2Version () {
238
185
String version = "Unknown" ;
239
- try (Connection conn =
240
- DriverManager .getConnection (stirlingUrl , stirlingDatabaseUsername , stirlingDatabasePassword )) {
186
+ try (Connection conn = DriverManager .getConnection (url , username , password )) {
241
187
try (Statement stmt = conn .createStatement ();
242
188
ResultSet rs = stmt .executeQuery ("SELECT H2VERSION() AS version" )) {
243
189
if (rs .next ()) {
@@ -277,8 +223,7 @@ public Path getBackupFilePath(String fileName) {
277
223
}
278
224
279
225
private void executeDatabaseScript (Path scriptPath ) {
280
- try (Connection conn =
281
- DriverManager .getConnection (stirlingUrl , stirlingDatabaseUsername , stirlingDatabasePassword )) {
226
+ try (Connection conn = DriverManager .getConnection (url , username , password )) {
282
227
ScriptUtils .executeSqlScript (conn , new EncodedResource (new PathResource (scriptPath )));
283
228
284
229
log .info ("Database import completed: {}" , scriptPath );
0 commit comments