Untitled
unknown
plain_text
2 months ago
5.0 kB
1
Indexable
<?php // Database connection parameters for both old and new FusionPBX instances $oldDb = new PDO('mysql:host=OLD_DB_HOST;dbname=OLD_DB_NAME', 'OLD_DB_USER', 'OLD_DB_PASS'); $newDb = new PDO('mysql:host=NEW_DB_HOST;dbname=NEW_DB_NAME', 'NEW_DB_USER', 'NEW_DB_PASS'); // Set both connections to handle exceptions for error catching $oldDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $newDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Batch size for batch insert $batchSize = 100; // Function to fetch old extensions ordered by the date column (created_at or updated_at) function fetchOldExtensions($oldDb, $batchSize, $lastFetchedId = 0) { $sql = "SELECT * FROM v_extensions WHERE id > :lastFetchedId ORDER BY created_at ASC LIMIT :batchSize"; $stmt = $oldDb->prepare($sql); $stmt->bindValue(':lastFetchedId', $lastFetchedId, PDO::PARAM_INT); $stmt->bindValue(':batchSize', $batchSize, PDO::PARAM_INT); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } // Function to insert a batch of extensions into the new database function insertExtensionsBatch($newDb, $extensions) { if (empty($extensions)) return; // Prepare SQL for batch insert $sql = "INSERT INTO v_extensions (extension, password, user_id, created_at) VALUES "; $values = []; foreach ($extensions as $extension) { // Prepare values for batch insert $values[] = "(:extension, :password, :user_id, :created_at)"; } // Join values with commas to form the batch insert query $sql .= implode(", ", $values); $stmt = $newDb->prepare($sql); // Bind parameters for each batch insert $paramIndex = 1; foreach ($extensions as $extension) { $stmt->bindValue(":extension{$paramIndex}", $extension['extension']); $stmt->bindValue(":password{$paramIndex}", $extension['password']); $stmt->bindValue(":user_id{$paramIndex}", $extension['user_id']); $stmt->bindValue(":created_at{$paramIndex}", $extension['created_at']); $paramIndex++; } // Execute the batch insert $stmt->execute(); } // Main script execution try { $lastFetchedId = 0; $totalMigrated = 0; // Continue fetching and migrating extensions in batches while (true) { // Fetch a batch of extensions from the old database $oldExtensions = fetchOldExtensions($oldDb, $batchSize, $lastFetchedId); if (empty($oldExtensions)) { // No more data to migrate break; } // Insert the batch of extensions into the new database insertExtensionsBatch($newDb, $oldExtensions); // Track the number of migrated extensions $totalMigrated += count($oldExtensions); // Update the last fetched ID to continue with the next batch $lastFetchedId = $oldExtensions[count($oldExtensions) - 1]['id']; echo "Migrated $totalMigrated extensions so far...\n"; } echo "Migration completed successfully. Total extensions migrated: $totalMigrated.\n"; } catch (Exception $e) { echo "Error: " . $e->getMessage(); } ?> } } } } } } } }
Editor is loading...
Leave a Comment