Files
api-wipay/run_migration_simple.php

139 lines
4.8 KiB
PHP
Raw Normal View History

<?php
/**
* QRIS Migration Script (Simple Version)
* Run: php run_migration_simple.php
*/
require __DIR__ . '/vendor/autoload.php';
// Load environment variables manually
if (file_exists(__DIR__ . '/.env')) {
$lines = file(__DIR__ . '/.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) {
continue; // Skip comments
}
if (strpos($line, '=') !== false) {
list($key, $value) = explode('=', $line, 2);
$key = trim($key);
$value = trim($value);
$_ENV[$key] = $value;
putenv("$key=$value");
}
}
}
use App\Config\Database;
try {
$db = Database::getInstance();
$connection = $db->getConnection();
echo "🚀 Starting QRIS migration...\n\n";
$tableName = 'pembayaran';
$columns = [
'qris_qr_code' => "TEXT NULL",
'qris_invoiceid' => "VARCHAR(100) NULL",
'qris_nmid' => "VARCHAR(100) NULL",
'qris_request_date' => "DATETIME NULL",
'qris_expired_at' => "DATETIME NULL",
'qris_check_count' => "INT DEFAULT 0",
'qris_last_check_at' => "DATETIME NULL",
'qris_status' => "ENUM('unpaid', 'paid', 'expired') DEFAULT 'unpaid'",
'qris_payment_method' => "VARCHAR(50) NULL",
'qris_payment_customer_name' => "VARCHAR(255) NULL",
'qris_paid_at' => "DATETIME NULL"
];
$indexes = [
'idx_qris_invoiceid' => 'qris_invoiceid',
'idx_qris_status' => 'qris_status',
'idx_qris_expired_at' => 'qris_expired_at'
];
$successCount = 0;
$errorCount = 0;
// Add columns
foreach ($columns as $columnName => $columnDef) {
try {
// Check if column exists
$checkSql = "SELECT COUNT(*) as cnt FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = :table
AND COLUMN_NAME = :column";
$result = $db->fetchOne($checkSql, [
'table' => $tableName,
'column' => $columnName
]);
if ($result && $result->cnt == 0) {
// Column doesn't exist, add it
$addSql = "ALTER TABLE `{$tableName}` ADD COLUMN `{$columnName}` {$columnDef}";
$connection->exec($addSql);
echo "✅ Added column: {$tableName}.{$columnName}\n";
$successCount++;
} else {
echo "⏭️ Column already exists: {$tableName}.{$columnName}\n";
}
} catch (\PDOException $e) {
if (strpos($e->getMessage(), 'Duplicate column') !== false) {
echo "⏭️ Column already exists: {$tableName}.{$columnName}\n";
} else {
echo "❌ Error adding column {$columnName}: " . $e->getMessage() . "\n";
$errorCount++;
}
}
}
// Create indexes
foreach ($indexes as $indexName => $columnName) {
try {
// Check if index exists
$checkSql = "SELECT COUNT(*) as cnt FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = :table
AND INDEX_NAME = :index";
$result = $db->fetchOne($checkSql, [
'table' => $tableName,
'index' => $indexName
]);
if ($result && $result->cnt == 0) {
// Index doesn't exist, create it
$createSql = "CREATE INDEX `{$indexName}` ON `{$tableName}` (`{$columnName}`)";
$connection->exec($createSql);
echo "✅ Created index: {$indexName} on {$tableName}({$columnName})\n";
$successCount++;
} else {
echo "⏭️ Index already exists: {$indexName}\n";
}
} catch (\PDOException $e) {
if (strpos($e->getMessage(), 'Duplicate key name') !== false ||
strpos($e->getMessage(), 'already exists') !== false) {
echo "⏭️ Index already exists: {$indexName}\n";
} else {
echo "❌ Error creating index {$indexName}: " . $e->getMessage() . "\n";
$errorCount++;
}
}
}
echo "\n📊 Migration Summary:\n";
echo " ✅ Success: {$successCount}\n";
echo " ❌ Errors: {$errorCount}\n";
if ($errorCount == 0) {
echo "\n🎉 Migration completed successfully!\n";
exit(0);
} else {
echo "\n⚠️ Migration completed with errors. Please review above.\n";
exit(1);
}
} catch (\Exception $e) {
echo "❌ Migration failed: " . $e->getMessage() . "\n";
exit(1);
}