Files

35 lines
1.2 KiB
PHP

<?php
if (PHP_SAPI !== 'cli' || $argc !== 2) {
exit(1);
}
umask(0077);
try {
$db = new PDO('sqlite:' . $argv[1], null, null, [PDO::ATTR_TIMEOUT => 5]);
$db->exec('BEGIN IMMEDIATE');
$applicationId = (int) $db->query('PRAGMA application_id')->fetchColumn();
$version = (int) $db->query('PRAGMA user_version')->fetchColumn();
if ($applicationId === 1163547478 && $version === 1) {
$db->exec('COMMIT');
exit(0);
}
$objects = (int) $db->query("SELECT count(*) FROM sqlite_schema WHERE name NOT GLOB 'sqlite_*'")->fetchColumn();
if ($applicationId !== 0 || $version !== 0 || $objects !== 0) {
throw new RuntimeException('Expected a new database or an EzServer PHP database at schema version 1.');
}
$schema = file_get_contents(__DIR__ . '/patients.sqlite.sql');
if ($schema === false) {
throw new RuntimeException('Could not read the patient schema.');
}
$db->exec($schema);
$db->exec('PRAGMA application_id = 1163547478');
$db->exec('PRAGMA user_version = 1');
$db->exec('COMMIT');
} catch (Throwable $error) {
fwrite(STDERR, 'SQLite initialization failed: ' . $error->getMessage() . PHP_EOL);
exit(1);
}