Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Leone25 committed Jul 3, 2024
1 parent e644dcc commit 434566c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions database/update-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
exit(1);
}

preg_match("#\(.SchemaVersion., (\d+)\)#", $database, $matches);
preg_match("#\(.SchemaVersion., '(\d+)'\)#", $database, $matches);
if (sizeof($matches) < 1) {
$schema = 0;
echo 'Schema version not found, assuming 0';
Expand All @@ -47,8 +47,13 @@
$db = new Database();
try {
$currentVersion = (int) $db->getConfigValue('SchemaVersion');

echo 'Current database version: ' . $currentVersion;
echo PHP_EOL;
} catch (\Exception $e) {
if ($e->getCode() === 404) {
echo 'Database missing version, assuming 0';
echo PHP_EOL;
$currentVersion = 0;
} else {
throw $e;
Expand All @@ -57,6 +62,7 @@

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

Expand All @@ -74,10 +80,13 @@
}

$sql = file_get_contents($filename);
$rawDb->exec('BEGIN');
$rawDb->exec($sql);
var_dump($rawDb->exec('BEGIN'));
$result = $rawDb->exec($sql);
if ($result === false) {
throw new \Exception('Error executing query: ' . $rawDb->lastErrorMsg(), 3);
}
$db->setConfigValue('SchemaVersion', $currentVersion);
$rawDb->exec('COMMIT');
var_dump($rawDb->exec('COMMIT'));
echo 'Updated to version ' . $currentVersion;
echo PHP_EOL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function unsetConfigValue(string $option)
*/
public function setConfigValue(string $option, string $value)
{
$stmt = $this->db->prepare('UPDATE config SET value = :value WHERE id = :id');
$stmt = $this->db->prepare('INSERT OR REPLACE INTO config (id, value) VALUES (:id, :value)');
$stmt->bindValue(':value', $value, SQLITE3_TEXT);
$stmt->bindValue(':id', $option, SQLITE3_TEXT);
$result = $stmt->execute();
Expand Down

0 comments on commit 434566c

Please sign in to comment.