Implement bulk export of client answers for coaches and enhance database handling in db_init.php
Some checks failed
PHPUnit / test (push) Has been cancelled
Some checks failed
PHPUnit / test (push) Has been cancelled
This commit is contained in:
17
db_init.php
17
db_init.php
@ -197,6 +197,11 @@ function qdb_acquire_lock() {
|
||||
* qdb_save() or qdb_discard() afterwards.
|
||||
*/
|
||||
function qdb_open(bool $writable = false): array {
|
||||
if (!$writable) {
|
||||
require_once __DIR__ . '/lib/read_db_cache.php';
|
||||
return qdb_read_db_open();
|
||||
}
|
||||
|
||||
$dbPath = QDB_PATH;
|
||||
|
||||
if (!is_dir(dirname($dbPath))) {
|
||||
@ -281,6 +286,12 @@ function qdb_open(bool $writable = false): array {
|
||||
*/
|
||||
function qdb_save(string $tmpDb, $lockFp): void {
|
||||
$masterKey = get_master_key_bytes();
|
||||
try {
|
||||
$ck = new PDO('sqlite:' . $tmpDb);
|
||||
$ck->exec('PRAGMA wal_checkpoint(FULL);');
|
||||
} catch (Throwable $e) {
|
||||
// best-effort before reading plaintext bytes
|
||||
}
|
||||
$plainDb = file_get_contents($tmpDb);
|
||||
if ($plainDb === false) throw new Exception("Could not read temp DB for save");
|
||||
$enc = aes256_cbc_encrypt_bytes($plainDb, $masterKey);
|
||||
@ -302,12 +313,18 @@ function qdb_save(string $tmpDb, $lockFp): void {
|
||||
flock($lockFp, LOCK_UN);
|
||||
fclose($lockFp);
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/lib/read_db_cache.php';
|
||||
qdb_read_cache_invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Discard changes -- clean up temp file and release lock without saving.
|
||||
*/
|
||||
function qdb_discard(string $tmpDb, $lockFp): void {
|
||||
if (defined('QDB_READONLY_CACHE_HANDLE') && $tmpDb === QDB_READONLY_CACHE_HANDLE) {
|
||||
return;
|
||||
}
|
||||
@unlink($tmpDb);
|
||||
if ($lockFp) {
|
||||
@flock($lockFp, LOCK_UN);
|
||||
|
||||
Reference in New Issue
Block a user