Skip to content

Commit 1f15399

Browse files
committed
feat: make DB location configurable
Note that this currently requires the path to the file to exist before the application is started
1 parent ca37dfd commit 1f15399

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

server/src/main/java/net/laprun/sustainability/power/persistence/SQLiteFilePersister.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,17 @@
2525
@Singleton
2626
public class SQLiteFilePersister {
2727
private final AtomicBoolean executing = new AtomicBoolean(false);
28-
@ConfigProperty(name = "quarkus.datasource.jdbc.url")
29-
String jdbcUrl;
28+
@ConfigProperty(name = "power-server.db.backup.location")
29+
String dbFilelocation;
3030
@Inject
3131
DataSource dataSource;
3232
private Path dbFile;
3333
private Path backupDBFile;
3434

3535
@PostConstruct
3636
void init() {
37-
int prefixLength = "jdbc:sqlite:".length();
38-
int queryParamsIdx = jdbcUrl.indexOf('?');
39-
int length = (queryParamsIdx != -1) ? queryParamsIdx : jdbcUrl.length();
40-
var dbFileName = jdbcUrl.substring(prefixLength, length);
41-
dbFile = Paths.get(dbFileName);
42-
backupDBFile = dbFile.toAbsolutePath().getParent().resolve(dbFile.getFileName() + "_backup");
37+
dbFile = Paths.get(dbFilelocation);
38+
backupDBFile = dbFile.toAbsolutePath().getParent().resolve(dbFile.getFileName() + ".backup");
4339
}
4440

4541
// Periodical backup
@@ -56,24 +52,25 @@ public void onShutdown(@Observes ShutdownEvent event) {
5652
void backup() {
5753
if (executing.compareAndSet(false, true)) {
5854
try {
59-
Log.trace("Starting DB backup for file: " + dbFile);
55+
56+
Log.trace("Persisting database to: " + dbFile);
6057
try (var conn = dataSource.getConnection();
6158
var stmt = conn.createStatement()) {
6259
// Execute the backup
6360
stmt.executeUpdate("backup to " + backupDBFile);
6461
// Atomically substitute the DB file with its backup
6562
Files.move(backupDBFile, dbFile, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
6663
} catch (SQLException e) {
67-
throw new RuntimeException("Failed to backup the database", e);
64+
throw new RuntimeException("Failed to persist the database", e);
6865
} catch (IOException e) {
6966
throw new RuntimeException("Failed to create backup files or folders", e);
7067
}
71-
Log.info("Backup of " + dbFile + " completed");
68+
Log.info("Persisting " + dbFile + " completed");
7269
} finally {
7370
executing.set(false);
7471
}
7572
} else {
76-
Log.trace("Skipping backup as one is already in progress");
73+
Log.trace("Skipping database persistence as the operation is already in progress");
7774
}
7875
}
7976

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
quarkus.http.port=20432
2-
quarkus.datasource.jdbc.url=jdbc:sqlite:power-server.db
2+
quarkus.datasource.jdbc.url=jdbc:sqlite:${power-server.db.backup.location}
33
quarkus.datasource.db-kind=sqlite
44
quarkus.datasource.jdbc.min-size=1
55

66
power-server.db.backup.period=5m
7+
power-server.db.backup.location=${HOME}/.power-server/db.sqlite
78

89
# Only use this property once to create the initial database
910
# quarkus.hibernate-orm.database.generation=create

0 commit comments

Comments
 (0)