expose: idempotent store Open, pg cleanup on failed connect + fallback proof

This commit is contained in:
2026-10-24 01:00:09 +02:00
parent 4996068f42
commit 16a0bb0de4
2 changed files with 54 additions and 0 deletions
+3
View File
@@ -96,6 +96,7 @@ class SqliteStore : public StatsStore {
if (db_) sqlite3_close(db_);
}
bool Open(std::string& err) override {
if (db_) return true; // idempotent: :memory: DBs must survive re-Open
if (sqlite3_open(path_.c_str(), &db_) != SQLITE_OK) {
err = sqlite3_errmsg(db_);
return false;
@@ -197,9 +198,11 @@ class PgStore : public StatsStore {
if (db_) PQfinish(db_);
}
bool Open(std::string& err) override {
if (db_) return true;
db_ = PQconnectdb(url_.c_str());
if (!db_ || PQstatus(db_) != CONNECTION_OK) {
err = db_ ? PQerrorMessage(db_) : "pg connect failed";
if (db_) { PQfinish(db_); db_ = nullptr; }
return false;
}
PGresult* r = PQexec(db_, kSchemaPg);