Files
api-btekno/bin/check_database.php

160 lines
5.0 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
/**
* Script to check database structure
* Usage: php bin/check_database.php
*/
require __DIR__ . '/../vendor/autoload.php';
use App\Config\AppConfig;
use App\Support\Database;
// Load environment variables
AppConfig::loadEnv(__DIR__ . '/..');
// Get database connection
$dbHost = AppConfig::get('DB_HOST', 'localhost');
$dbName = AppConfig::get('DB_NAME', '');
$dbUser = AppConfig::get('DB_USER', '');
$dbPass = AppConfig::get('DB_PASS', '');
if (empty($dbName) || empty($dbUser)) {
echo "Error: Database credentials not configured in .env\n";
exit(1);
}
try {
$db = Database::getConnection($dbHost, $dbName, $dbUser, $dbPass);
echo "=== Database Structure Check ===\n\n";
// Check tariffs table structure
echo "1. Checking tariffs table structure:\n";
$stmt = $db->query("DESCRIBE tariffs");
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($columns)) {
echo " ❌ Table 'tariffs' does not exist!\n";
} else {
echo " ✅ Table 'tariffs' exists with columns:\n";
foreach ($columns as $col) {
echo " - {$col['Field']} ({$col['Type']})\n";
}
}
echo "\n2. Checking locations table structure:\n";
$stmt = $db->query("DESCRIBE locations");
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($columns)) {
echo " ❌ Table 'locations' does not exist!\n";
} else {
echo " ✅ Table 'locations' exists with columns:\n";
foreach ($columns as $col) {
echo " - {$col['Field']} ({$col['Type']})\n";
}
}
echo "\n3. Checking gates table structure:\n";
$stmt = $db->query("DESCRIBE gates");
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($columns)) {
echo " ❌ Table 'gates' does not exist!\n";
} else {
echo " ✅ Table 'gates' exists with columns:\n";
foreach ($columns as $col) {
echo " - {$col['Field']} ({$col['Type']})\n";
}
}
echo "\n4. Checking entry_events table structure:\n";
$stmt = $db->query("DESCRIBE entry_events");
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($columns)) {
echo " ❌ Table 'entry_events' does not exist!\n";
} else {
echo " ✅ Table 'entry_events' exists with columns:\n";
foreach ($columns as $col) {
echo " - {$col['Field']} ({$col['Type']})\n";
}
}
echo "\n5. Checking daily_summary table structure:\n";
$stmt = $db->query("DESCRIBE daily_summary");
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($columns)) {
echo " ❌ Table 'daily_summary' does not exist!\n";
} else {
echo " ✅ Table 'daily_summary' exists with columns:\n";
foreach ($columns as $col) {
echo " - {$col['Field']} ({$col['Type']})\n";
}
}
echo "\n6. Checking hourly_summary table structure:\n";
$stmt = $db->query("DESCRIBE hourly_summary");
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($columns)) {
echo " ⚠️ Table 'hourly_summary' does not exist (optional table)\n";
} else {
echo " ✅ Table 'hourly_summary' exists with columns:\n";
foreach ($columns as $col) {
echo " - {$col['Field']} ({$col['Type']})\n";
}
}
echo "\n7. Testing query (similar to DailySummaryService):\n";
$testSql = "
SELECT
DATE(e.event_time) as summary_date,
e.location_code,
e.gate_code,
e.category,
COUNT(*) as total_count,
COALESCE(t.price, 0) as tariff_amount
FROM entry_events e
INNER JOIN locations l ON e.location_code = l.code AND l.is_active = 1
INNER JOIN gates g ON e.location_code = g.location_code
AND e.gate_code = g.gate_code
AND g.is_active = 1
LEFT JOIN tariffs t ON e.location_code = t.location_code
AND e.gate_code = t.gate_code
AND e.category = t.category
WHERE DATE(e.event_time) = CURDATE()
GROUP BY
DATE(e.event_time),
e.location_code,
e.gate_code,
e.category,
COALESCE(t.price, 0)
LIMIT 1
";
try {
$stmt = $db->query($testSql);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
echo " ✅ Query executed successfully!\n";
if ($result) {
echo " Sample result: " . json_encode($result, JSON_PRETTY_PRINT) . "\n";
} else {
echo " No data found for today.\n";
}
} catch (PDOException $e) {
echo " ❌ Query failed: " . $e->getMessage() . "\n";
}
echo "\n=== Check Complete ===\n";
} catch (PDOException $e) {
echo "Database Error: " . $e->getMessage() . "\n";
exit(1);
}