require sqlite foreign_key ON

This commit is contained in:
Thomas Winget 2021-10-04 21:58:05 -04:00
parent 5597fc748b
commit df07ae3e40
1 changed files with 13 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#include <SQLiteCpp/SQLiteCpp.h>
#include <sqlite3.h>
#include <fmt/format.h>
#include <chrono>
#include <cstdlib>
@ -281,6 +282,18 @@ namespace db
if (int rc = db.tryExec("PRAGMA synchronous = NORMAL"); rc != SQLITE_OK)
MERROR("Failed to set synchronous mode to NORMAL: {}" << sqlite3_errstr(rc));
if (int rc = db.tryExec("PRAGMA foreign_keys = ON");
rc != SQLITE_OK) {
auto m = fmt::format("Failed to enable foreign keys constraints: {}", sqlite3_errstr(rc));
MERROR(m);
throw std::runtime_error{m};
}
int fk_enabled = db.execAndGet("PRAGMA foreign_keys").getInt();
if (fk_enabled != 1) {
MERROR("Failed to enable foreign key constraints; perhaps this sqlite3 is compiled without it?");
throw std::runtime_error{"Foreign key support is required"};
}
// FIXME: SQLite / SQLiteCPP may not have encryption available
// so this may fail, or worse silently fail and do nothing
if (not db_password.empty())