<?php
/**
* Truncates data from a database queue table for data migration.
*
* @param string $table_name The name of the queue table.
* @param int $truncate_amount The number of records to truncate. Use 0 to truncate all.
* @param PDO $pdo The PDO database connection object.
* @return bool True on success, false on failure.
*/
function truncateQueueTable(string $table_name, int $truncate_amount = 0, PDO $pdo): bool
{
try {
// Prepare the SQL statement for truncation.
$sql = "DELETE FROM `$table_name`";
// Add a limit if a specific number of records should be truncated
if ($truncate_amount > 0) {
$sql .= " LIMIT $truncate_amount";
}
// Execute the SQL statement.
$stmt = $pdo->prepare($sql);
$stmt->execute();
// Check if the operation was successful.
return true;
} catch (PDOException $e) {
// Handle database errors.
error_log("Error truncating queue table: " . $e->getMessage()); // Log the error
return false;
}
}
// Example Usage (replace with your actual values)
/*
try {
$pdo = new PDO("mysql:host=localhost;dbname=your_database", "your_user", "your_password");
$table = "your_queue_table";
if (truncateQueueTable($table, 1000, $pdo)) {
echo "Queue table truncated successfully.\n";
} else {
echo "Queue table truncation failed.\n";
}
} catch (PDOException $e) {
echo "Database connection error: " . $e->getMessage();
}
*/
?>
Add your comment