initial db config

This commit is contained in:
Sundog Jones 2024-05-06 10:22:10 -04:00
parent 27745dff46
commit 5a3bd7eadb
1 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,35 @@
<?php
// this file configures the database connection
// and initializes the database if it does not
// already exist
// the database file itself, located in the 'db/sqlite' directory of this repo
$db_file = realpath(dirname(__FILE__) . '/../../../db/sqlite/') . "/wander.sqlite";
// if the file doesn't exist, we need to populate it with the initial data schema
$populate = false;
if (!file_exists($db_file)) {
$populate = true;
}
$conn = new SQLite3($db_file);
// check for errors opening the database
if (!$conn) {
error_log("Failed opening sqlite database: " . $conn->lastErrorMsg());
}
$conn->exec('PRAGMA journal_mode = wal;');
$conn->exec('PRAGMA synchronous = NORMAL;');
if ($populate) {
// load the initial schema
$schema_file = realpath(dirname(__FILE__) . '/../../../db/sqlite/20240506-initialize-db.sql');
$sql = file_get_contents($schema_file) or die("cannot get file contents for " . $schema_file);
$conn->exec($sql);
}
?>