initial db config
This commit is contained in:
parent
27745dff46
commit
5a3bd7eadb
35
src/webserver/config/db.php
Normal file
35
src/webserver/config/db.php
Normal 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);
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user