1. <?php
  2. /**
  3. * Truncates data from a database queue table for data migration.
  4. *
  5. * @param string $table_name The name of the queue table.
  6. * @param int $truncate_amount The number of records to truncate. Use 0 to truncate all.
  7. * @param PDO $pdo The PDO database connection object.
  8. * @return bool True on success, false on failure.
  9. */
  10. function truncateQueueTable(string $table_name, int $truncate_amount = 0, PDO $pdo): bool
  11. {
  12. try {
  13. // Prepare the SQL statement for truncation.
  14. $sql = "DELETE FROM `$table_name`";
  15. // Add a limit if a specific number of records should be truncated
  16. if ($truncate_amount > 0) {
  17. $sql .= " LIMIT $truncate_amount";
  18. }
  19. // Execute the SQL statement.
  20. $stmt = $pdo->prepare($sql);
  21. $stmt->execute();
  22. // Check if the operation was successful.
  23. return true;
  24. } catch (PDOException $e) {
  25. // Handle database errors.
  26. error_log("Error truncating queue table: " . $e->getMessage()); // Log the error
  27. return false;
  28. }
  29. }
  30. // Example Usage (replace with your actual values)
  31. /*
  32. try {
  33. $pdo = new PDO("mysql:host=localhost;dbname=your_database", "your_user", "your_password");
  34. $table = "your_queue_table";
  35. if (truncateQueueTable($table, 1000, $pdo)) {
  36. echo "Queue table truncated successfully.\n";
  37. } else {
  38. echo "Queue table truncation failed.\n";
  39. }
  40. } catch (PDOException $e) {
  41. echo "Database connection error: " . $e->getMessage();
  42. }
  43. */
  44. ?>

Add your comment