initial commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
include __DIR__ . '/../src/config/koneksi.php';
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
|
||||
echo "Checking bantuan_terakhir column...\n";
|
||||
$st = $conn->prepare("SELECT COUNT(*) AS c FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='lokasi' AND COLUMN_NAME='bantuan_terakhir'");
|
||||
$st->execute();
|
||||
$res = $st->get_result()->fetch_assoc();
|
||||
$st->close();
|
||||
if($res && intval($res['c'])>0){
|
||||
echo "Column exists.\n";
|
||||
} else {
|
||||
echo "Column missing. Adding...\n";
|
||||
if($conn->query("ALTER TABLE lokasi ADD COLUMN bantuan_terakhir DATETIME NULL")){
|
||||
echo "Column added.\n";
|
||||
} else {
|
||||
echo "Failed to add column: " . $conn->error . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// populate bantuan_terakhir from last_assisted_at where appropriate
|
||||
echo "Populating bantuan_terakhir from last_assisted_at...\n";
|
||||
if($conn->query("UPDATE lokasi SET bantuan_terakhir = last_assisted_at WHERE bantuan_terakhir IS NULL AND last_assisted_at IS NOT NULL")){
|
||||
echo "Populate step finished. Affected rows: " . $conn->affected_rows . "\n";
|
||||
} else {
|
||||
echo "Populate failed: " . $conn->error . "\n";
|
||||
}
|
||||
|
||||
// ensure index exists
|
||||
echo "Checking index idx_lokasi_assisted...\n";
|
||||
$st = $conn->prepare("SELECT COUNT(*) AS c FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='lokasi' AND INDEX_NAME='idx_lokasi_assisted'");
|
||||
$st->execute();
|
||||
$res2 = $st->get_result()->fetch_assoc();
|
||||
$st->close();
|
||||
if($res2 && intval($res2['c'])>0){
|
||||
echo "Index exists.\n";
|
||||
} else {
|
||||
echo "Creating index...\n";
|
||||
if($conn->query('CREATE INDEX idx_lokasi_assisted ON lokasi (assisted)')){
|
||||
echo "Index created.\n";
|
||||
} else {
|
||||
echo "Failed to create index: " . $conn->error . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
echo "Done.\n";
|
||||
?>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
include __DIR__ . '/../src/config/koneksi.php';
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
|
||||
echo "Running assistance expiry (30 days)...\n";
|
||||
$q = "UPDATE lokasi SET assisted = 0, last_assisted_at = NULL, sumber_bantuan = NULL, bentuk_bantuan = NULL, assistance_notes = NULL WHERE assisted = 1 AND last_assisted_at IS NOT NULL AND last_assisted_at <= DATE_SUB(NOW(), INTERVAL 30 DAY)";
|
||||
if($conn->query($q)){
|
||||
echo "OK. Affected rows: " . $conn->affected_rows . "\n";
|
||||
} else {
|
||||
echo "Failed: " . $conn->error . "\n";
|
||||
}
|
||||
echo "Done.\n";
|
||||
?>
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
// Simple migration runner for the current WebGIS schema.
|
||||
// Run from the project root:
|
||||
// php scripts/run_migrations.php
|
||||
|
||||
include __DIR__ . '/../src/config/koneksi.php';
|
||||
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
|
||||
$conn->query("CREATE TABLE IF NOT EXISTS migrations (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, applied_at DATETIME NOT NULL)");
|
||||
|
||||
function migration_applied($conn, $name){
|
||||
$st = $conn->prepare('SELECT COUNT(*) AS c FROM migrations WHERE name = ?');
|
||||
if(!$st){ return false; }
|
||||
$st->bind_param('s', $name);
|
||||
$st->execute();
|
||||
$r = $st->get_result();
|
||||
$row = $r ? $r->fetch_assoc() : null;
|
||||
$st->close();
|
||||
return $row && intval($row['c']) > 0;
|
||||
}
|
||||
|
||||
function run_migration_file($conn, $path){
|
||||
$name = basename($path);
|
||||
if(migration_applied($conn, $name)){
|
||||
echo "Skipping already applied: $name\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
$sql = file_get_contents($path);
|
||||
if($sql === false){
|
||||
echo "Failed to read $name\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!$conn->multi_query($sql)){
|
||||
echo "Failed executing $name: " . $conn->error . "\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
do {
|
||||
if($res = $conn->store_result()){
|
||||
$res->free();
|
||||
}
|
||||
} while($conn->more_results() && $conn->next_result());
|
||||
|
||||
if($conn->errno){
|
||||
echo "Failed executing $name: " . $conn->error . "\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
$st = $conn->prepare('INSERT INTO migrations (name, applied_at) VALUES (?, NOW())');
|
||||
if($st){
|
||||
$st->bind_param('s', $name);
|
||||
$st->execute();
|
||||
$st->close();
|
||||
}
|
||||
|
||||
echo "Applied: $name\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
$migDir = __DIR__ . '/../sql/migrations';
|
||||
$files = array_values(array_filter(scandir($migDir), function($f){ return preg_match('/^\d+_.*\.sql$/', $f); }));
|
||||
sort($files, SORT_NATURAL);
|
||||
|
||||
if(empty($files)){
|
||||
echo "No migration files found.\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
foreach($files as $file){
|
||||
if(!run_migration_file($conn, $migDir . '/' . $file)){
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
echo "Migrations complete.\n";
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
include __DIR__ . '/../src/config/koneksi.php';
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
|
||||
echo "Simulating an assisted row older than 31 days...\n";
|
||||
|
||||
$q = "SELECT id FROM lokasi WHERE 1 LIMIT 1";
|
||||
$res = $conn->query($q);
|
||||
if(!$res){
|
||||
echo "Failed to select: " . $conn->error . "\n";
|
||||
exit(1);
|
||||
}
|
||||
$row = $res->fetch_assoc();
|
||||
if(!$row){
|
||||
echo "No rows in lokasi to simulate.\n";
|
||||
exit(1);
|
||||
}
|
||||
$id = intval($row['id']);
|
||||
echo "Using id = $id\n";
|
||||
|
||||
$u = "UPDATE lokasi SET assisted = 1, last_assisted_at = DATE_SUB(NOW(), INTERVAL 31 DAY) WHERE id = $id";
|
||||
if($conn->query($u)){
|
||||
echo "Updated id $id. Affected rows: " . $conn->affected_rows . "\n";
|
||||
} else {
|
||||
echo "Failed to update: " . $conn->error . "\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
echo "Done.\n";
|
||||
?>
|
||||
Reference in New Issue
Block a user