|
7 | 7 | import java.nio.file.Paths;
|
8 | 8 | import java.nio.file.attribute.BasicFileAttributes;
|
9 | 9 | import java.sql.Connection;
|
10 |
| -import java.sql.DriverManager; |
11 | 10 | import java.sql.ResultSet;
|
12 | 11 | import java.sql.SQLException;
|
13 | 12 | import java.sql.Statement;
|
|
20 | 19 | import java.util.stream.Collectors;
|
21 | 20 | import java.util.stream.Stream;
|
22 | 21 |
|
23 |
| -import org.springframework.beans.factory.annotation.Value; |
24 |
| -import org.springframework.context.annotation.Configuration; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
25 | 23 | import org.springframework.core.io.PathResource;
|
26 | 24 | import org.springframework.core.io.support.EncodedResource;
|
27 | 25 | import org.springframework.jdbc.datasource.init.ScriptException;
|
28 | 26 | import org.springframework.jdbc.datasource.init.ScriptUtils;
|
| 27 | +import org.springframework.stereotype.Service; |
29 | 28 |
|
30 | 29 | import lombok.extern.slf4j.Slf4j;
|
31 | 30 | import stirling.software.SPDF.config.interfaces.DatabaseBackupInterface;
|
32 | 31 | import stirling.software.SPDF.model.exception.BackupNotFoundException;
|
33 | 32 | import stirling.software.SPDF.utils.FileInfo;
|
34 | 33 |
|
35 | 34 | @Slf4j
|
36 |
| -@Configuration |
37 |
| -public class DatabaseBackupHelper implements DatabaseBackupInterface { |
| 35 | +@Service |
| 36 | +public class DatabaseBackupService implements DatabaseBackupInterface { |
38 | 37 |
|
39 | 38 | public static final String BACKUP_PREFIX = "backup_";
|
40 | 39 | public static final String SQL_SUFFIX = ".sql";
|
| 40 | + private static final Path BACKUP_PATH = Paths.get("configs/db/backup/"); |
41 | 41 |
|
42 |
| - @Value("${dbType:postgresql}") |
43 |
| - private String dbType; |
| 42 | + @Autowired private DatabaseConfig databaseConfig; |
44 | 43 |
|
45 |
| - @Value("${spring.datasource.url}") |
46 |
| - private String url; |
47 |
| - |
48 |
| - @Value("${spring.datasource.username}") |
49 |
| - private String username; |
50 |
| - |
51 |
| - @Value("${spring.datasource.password}") |
52 |
| - private String password; |
| 44 | + @Override |
| 45 | + public void setAdminUser() { |
| 46 | + String adminScript = |
| 47 | + """ |
| 48 | + DO |
| 49 | + $do$ |
| 50 | + BEGIN |
| 51 | + IF EXISTS ( |
| 52 | + SELECT FROM pg_catalog.pg_roles |
| 53 | + WHERE rolname = 'admin') THEN |
| 54 | +
|
| 55 | + RAISE NOTICE 'Role "admin" already exists. Skipping.'; |
| 56 | + ELSE |
| 57 | + CREATE USER admin WITH ENCRYPTED PASSWORD 'stirling'; |
| 58 | + END IF; |
| 59 | + END |
| 60 | + $do$; |
| 61 | +
|
| 62 | + CREATE SCHEMA IF NOT EXISTS stirling_pdf AUTHORIZATION admin; |
| 63 | + GRANT ALL PRIVILEGES ON DATABASE postgres TO admin; |
| 64 | + ALTER DATABASE postgres SET search_path TO stirling_pdf; |
| 65 | + ALTER USER admin SET search_path TO stirling_pdf; |
| 66 | + """ |
| 67 | + .trim(); |
| 68 | + |
| 69 | + try (Connection connection = databaseConfig.connection(); |
| 70 | + Statement statement = connection.createStatement()) { |
| 71 | + statement.execute(adminScript); |
| 72 | + } catch (SQLException e) { |
| 73 | + log.error("Error: Failed to create admin user for database", e); |
| 74 | + } |
53 | 75 |
|
54 |
| - private final Path BACKUP_PATH = Paths.get("configs/db/backup/"); |
| 76 | + log.info("Created admin user for database"); |
| 77 | + } |
55 | 78 |
|
56 | 79 | @Override
|
57 | 80 | public boolean hasBackup() {
|
@@ -154,7 +177,7 @@ public void exportDatabase() {
|
154 | 177 | Path insertOutputFilePath =
|
155 | 178 | this.getBackupFilePath(BACKUP_PREFIX + dateNow.format(myFormatObj) + SQL_SUFFIX);
|
156 | 179 |
|
157 |
| - try (Connection conn = DriverManager.getConnection(url, username, password)) { |
| 180 | + try (Connection conn = databaseConfig.connection()) { |
158 | 181 | ScriptUtils.executeSqlScript(
|
159 | 182 | conn, new EncodedResource(new PathResource(insertOutputFilePath)));
|
160 | 183 |
|
@@ -183,7 +206,7 @@ private static void deleteOldestBackup(List<FileInfo> filteredBackupList) {
|
183 | 206 | // Retrieves the H2 database version.
|
184 | 207 | public String getH2Version() {
|
185 | 208 | String version = "Unknown";
|
186 |
| - try (Connection conn = DriverManager.getConnection(url, username, password)) { |
| 209 | + try (Connection conn = databaseConfig.connection()) { |
187 | 210 | try (Statement stmt = conn.createStatement();
|
188 | 211 | ResultSet rs = stmt.executeQuery("SELECT H2VERSION() AS version")) {
|
189 | 212 | if (rs.next()) {
|
@@ -223,7 +246,7 @@ public Path getBackupFilePath(String fileName) {
|
223 | 246 | }
|
224 | 247 |
|
225 | 248 | private void executeDatabaseScript(Path scriptPath) {
|
226 |
| - try (Connection conn = DriverManager.getConnection(url, username, password)) { |
| 249 | + try (Connection conn = databaseConfig.connection()) { |
227 | 250 | ScriptUtils.executeSqlScript(conn, new EncodedResource(new PathResource(scriptPath)));
|
228 | 251 |
|
229 | 252 | log.info("Database import completed: {}", scriptPath);
|
|
0 commit comments