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); }