feat: automatic db creation and migration

This commit is contained in:
2026-08-28 16:32:25 +03:00
parent 114ba523f4
commit 4fbb8a8e2e
9 changed files with 104 additions and 120 deletions
+3 -1
View File
@@ -1,4 +1,6 @@
api.lock
_api.lock
data.db
data.db
dist
k-api.updates.php
+3 -3
View File
@@ -13,7 +13,7 @@ function db()
};
function relativePathToUrl($rel)
function relativePathToUrl(string $rel)
{
$parts = explode('/', dirname($_SERVER['SCRIPT_NAME']) . '/' . $rel);
$norm = [];
@@ -37,7 +37,7 @@ function FastDBReq(PDO $db, string $sql, array $params = []): array
}
}
function Login($req)
function Login(mixed $req)
{
return isset($req->data['pass']) && $req->data['pass'] == ADMIN_PASS;
}
@@ -46,7 +46,7 @@ CreateApi(function ($api) {
$api->on('/', 'GET', function ($req) {
return 'hi';
});
$api->on('/phpliteadmin', 'GET', function ($req) {
$api->on('/phpliteadmin', 'GET', function () {
header('Location: ./adm/phpliteadmin.php');
return "";
});
+2
View File
@@ -99,6 +99,8 @@ CreateApi(function ($api) {
die($cnt);
}
$api->useUpdates(db());
$api->on('/', 'GET', function () {
return 'dengi';
});
+23
View File
@@ -0,0 +1,23 @@
set -e
# copy only nessesary files
echo "Copying files..."
mkdir -p dist
cp -r *.php sql-updates/ adm/ dist/
set +e
echo "Downloading phpLiteAdmin"
wget -nc https://bitbucket.org/phpliteadmin/public/downloads/phpLiteAdmin_v1-9-8-2.zip
unzip phpLiteAdmin_v1-9-8-2.zip phpliteadmin.php
mv phpliteadmin.php dist/adm
echo "Cleaning up"
rm -f phpLiteAdmin_v1-9-8-2.zip
set -e
rm -f dist/k-api.updates-status.php
echo "
Done."
+31 -1
View File
@@ -1,4 +1,6 @@
<?php
const UPDATES_STATUS_FILE = "k-api.updates-status.php";
const SQL_UPDATES_FOLDER = "./sql-updates";
/** @param callable(_K_Api): void $apiFun */
function CreateApi(callable $apiFun, bool $devmode = false)
@@ -66,13 +68,41 @@ class _K_Api
$_a = "unknown return type ($_type), (available bool, array, numeric, string)";
}
die("ERROR: " . $_a);
return;
}
return;
}
}
public function useUpdates(PDO $db)
{
$currentVersion = file_get_contents(UPDATES_STATUS_FILE);
if ($currentVersion === false) {
file_put_contents(UPDATES_STATUS_FILE, "<?php // -1");
$currentVersion = -1;
} else {
$currentVersion = intval(substr($currentVersion, 9));
}
$files = glob(SQL_UPDATES_FOLDER . '/*.sql');
$updateVersion = null;
foreach ($files as $file) {
if (!is_file($file)) {
continue;
}
$version = intval(basename($file, '.sql'));
if ($version > $currentVersion) {
$db->exec(file_get_contents($file));
$updateVersion = $version;
}
}
if ($updateVersion !== null) {
file_put_contents(
UPDATES_STATUS_FILE,
"<?php // " . $updateVersion
);
}
}
public function end()
{
if (!$this->has_exec) {
+1
View File
@@ -0,0 +1 @@
<?php // 0
+34
View File
@@ -0,0 +1,34 @@
/* initial sql database (dxv4 2.1) */
CREATE TABLE admins (
adminId TEXT UNIQUE
NOT NULL,
adminName TEXT NOT NULL,
adminClass TEXT NOT NULL,
adminPrems INTEGER DEFAULT (666)
);
CREATE TABLE changes (
classId TEXT,
forDay TEXT,
content TEXT
);
CREATE TABLE classes (
classid TEXT UNIQUE
NOT NULL,
userclassname TEXT NOT NULL
);
CREATE TABLE rings (
classId TEXT,
fromDate TEXT,
content TEXT
);
CREATE TABLE schedules (
classId TEXT,
fromDate TEXT,
dayOfWeek INTEGER,
content TEXT
);