<?php
/**
* CLI tool for restoring user data. FOR DEVELOPMENT USE ONLY.
*/
require_once 'vendor/autoload.php'; // Assuming you're using Composer
use Firebase\Firestore\Firestore;
// Configuration - Replace with your actual values
define('FIRESTORE_COLLECTION', 'users'); // Collection name
define('RESTORE_FILE', 'restore_data.json'); // File containing the data to restore
/**
* Restores user data from a JSON file to Firestore.
*
* @param string $restoreFilePath Path to the JSON file containing the data.
* @return bool True on success, false on failure.
*/
function restoreData(string $restoreFilePath): bool
{
try {
// Load data from the JSON file
$data = json_decode(file_get_contents($restoreFilePath), true);
if ($data === null) {
error_log("Error: Failed to decode JSON from " . $restoreFilePath);
return false;
}
// Initialize Firestore
$db = Firestore::create();
// Iterate through the data and insert each user
foreach ($data as $user) {
$db->collection(FIRESTORE_COLLECTION)->add($user);
}
error_log("Data restored successfully from " . $restoreFilePath . " to Firestore.");
return true;
} catch (\Exception $e) {
error_log("Error restoring data: " . $e->getMessage());
return false;
}
}
// Check if the restore file is provided as a command-line argument
if (isset($argv[1])) {
$restoreFilePath = $argv[1];
if (restoreData($restoreFilePath)) {
echo "Data restoration completed.\n";
} else {
echo "Data restoration failed.\n";
}
} else {
echo "Usage: php restore.php <path_to_restore_file.json>\n";
}
?>
Add your comment