Skip to content

Commit

Permalink
added automatic db update
Browse files Browse the repository at this point in the history
  • Loading branch information
Leone25 committed Jul 3, 2024
1 parent 1ed4634 commit e644dcc
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Run `./launch.sh` in the main directory of the repository to run the server with

Once launched the application is available at `http://localhost:8082`

To update the database schema run `docker-compose exec -T app php /var/www/html/database/update-db.php`.

### Without containers

You will need:
Expand Down
3 changes: 3 additions & 0 deletions database/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ create table notes (
foreign key (candidate_id) references users (id) on delete cascade on update cascade,
primary key (uid, candidate_id)
);

-- Be careful not to change this line other than the number of the version, this is parsed with regex by the updater
insert into config (id, value) values ('SchemaVersion', '0');
95 changes: 95 additions & 0 deletions database/update-db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/**
* ! HOW TO USE !
*
* You don't need to look in here. All you need to do is write your querys in the database/update folder. They will be executed in order.
* Remember also to update the version at the bottom of the weeehire.sql file.
*
*/



namespace WEEEOpen\WEEEHire;


if (php_sapi_name() !== 'cli') {
http_response_code(403);
header('Content-Type', 'text/plain');
echo 'Available only in PHP CLI';
return;
}

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Database.php';

$database = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'database.sql');

if ($database === false) {
echo 'Database file not found';
echo PHP_EOL;
exit(1);
}

preg_match("#\(.SchemaVersion., (\d+)\)#", $database, $matches);
if (sizeof($matches) < 1) {
$schema = 0;
echo 'Schema version not found, assuming 0';
echo PHP_EOL;
} else {
$schema = (int) $matches[1];
}

echo "Last versions found in sql files: schema $schema";
echo PHP_EOL;

try {
$db = new Database();
try {
$currentVersion = (int) $db->getConfigValue('SchemaVersion');
} catch (\Exception $e) {
if ($e->getCode() === 404) {
$currentVersion = 0;
} else {
throw $e;
}
}

if ($currentVersion === $schema) {
echo 'Database is up to date';
return;
}

if ($currentVersion > $schema) {
throw new \Exception('Database version is newer than the one in the sql file', 1);
}

$rawDb = $db->getDb();

while ($currentVersion < $schema) {
$currentVersion++;
$filename = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . $currentVersion . '.sql';
if (!file_exists($filename)) {
throw new \Exception('Update file not found: ' . $filename, 2);
}

$sql = file_get_contents($filename);
$rawDb->exec('BEGIN');
$rawDb->exec($sql);
$db->setConfigValue('SchemaVersion', $currentVersion);
$rawDb->exec('COMMIT');
echo 'Updated to version ' . $currentVersion;
echo PHP_EOL;
}
} catch(\Exception $e) {
echo get_class($e);
echo PHP_EOL;
echo $e->getMessage();
echo PHP_EOL;
echo $e->getTraceAsString();
exit(1);
}

echo 'Update completed';
echo PHP_EOL;
exit(0);
3 changes: 3 additions & 0 deletions database/update/1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


-- TODO: actually use this
13 changes: 12 additions & 1 deletion src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public function __construct()
$this->db = new SQLite3(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'weeehire.db', SQLITE3_OPEN_READWRITE);
}

/**
* ⚠️ THIS METHOD IS UNSAFE, NEVER EXPOSE IT, ONLY FOR DB UPGRADE ⚠️
* Get the SQLite3 object
* @return SQLite3
*/

public function getDb(): SQLite3
{
return $this->db;
}

/**
* Insert a new User into the database
*
Expand Down Expand Up @@ -205,7 +216,7 @@ public function getConfigValue(string $option)
$result->finalize();

if ($row === false) {
throw new DatabaseException("Config value $option not found");
throw new DatabaseException("Config value $option not found", 404);
}

return $row['value'];
Expand Down

0 comments on commit e644dcc

Please sign in to comment.