/** * @param SS_HTTPRequest $request * @return SS_HTTPResponse */ public function run($request) { parent::run($request); // Disable filters if (class_exists('ContentNotifierExtension')) { ContentNotifierExtension::disable_filtering(); } if (class_exists('Post')) { Config::inst()->update('Post', 'allow_reading_spam', true); } // Init tasks $taskGroup = $request->getVar('tasks') ?: 'tasks'; $this->message("Beginning import tasks {$taskGroup}"); $this->connectToRemoteSite(); Versioned::reading_stage('Stage'); // Check if we only want to do a single step if ($pass = $request->requestVar('pass')) { $this->message("Resuming at {$pass} pass"); switch ($pass) { case 'identify': $this->identifyPass($taskGroup); return; case 'import': $this->importPass($taskGroup); return; case 'link': $this->linkPass($taskGroup); return; } } $this->identifyPass($taskGroup); $this->importPass($taskGroup); $this->linkPass($taskGroup); }
public function run($request) { parent::run($request); $tables = explode(',', $request->getVar('tables')); if (empty($tables)) { throw new InvalidArgumentException("No 'tables' parameter specified"); } // Check relation we want to keep field $keepRelations = $request->getVar('keeprelations'); switch ($keepRelations) { case 'true': $keepRelations = true; break; case 'false': case '0': $keepRelations = false; break; default: $keepRelations = explode(',', $keepRelations); } $this->message("== Importing bulk data =="); // Create temp file $this->message(" * Creating temp file"); $this->tempFile = tempnam(TEMP_FOLDER, get_class() . '_mysqldump' . date('Y-m-d')); // Run mysql export on the remote table $this->message(" * Exporting tables with mysqldump"); $this->exportTables($tables); // Import data into local table $this->message(" * Importing tables into mysql"); $this->importTables(); // Rebuild DB $this->message("== Rebuilding database =="); singleton("DatabaseAdmin")->doBuild(false); // Fix relation IDs if ($keepRelations === true) { $this->message("Keeping relations; Not resetting RelationIDs to zero"); } else { $this->message("Invalidate has_one fields (bypass this with keeprelations=relationid)"); foreach ($tables as $table) { $hasOne = Config::inst()->get($table, 'has_one', Config::UNINHERITED); if (empty($hasOne)) { continue; } foreach ($hasOne as $relation => $class) { $field = $relation . "ID"; if (is_array($keepRelations) && in_array($field, $keepRelations)) { $this->message(" * Keeping relation {$table}.{$field}"); } else { $this->message(" * Resetting relation {$table}.{$field} to zero on migrated table"); DB::query('UPDATE "' . $table . '" SET "' . $field . '" = 0'); } } } } // Mark _ImportedID, _ImportedDate and LegacyID $this->connectToRemoteSite(); $this->message("== Marking records as migrated =="); foreach ($tables as $table) { // Don't mark non-base tables (subclasses of Member, etc) if (ImportHelper::is_a($table, 'DataObject') && $table != ClassInfo::baseDataClass($table)) { continue; } $remoteConn = $this->getRemoteConnection(); // Setup schema $this->ensureTableHasColumn(DB::getConn(), $table, 'LegacyID', 'int(11) not null default 0'); $this->ensureTableHasColumn($remoteConn, $table, '_ImportedID', 'int(11) not null default 0'); $this->ensureTableHasColumn($remoteConn, $table, '_ImportedDate', 'datetime'); // Bulk update $this->message(" * Updating {$table}._ImportedID and {$table}._ImportedDate"); $remoteConn->query('UPDATE "' . $table . '" SET "_ImportedID" = "ID", "_ImportedDate" = NOW()'); $this->message(" * Updating {$table}.LegacyID"); DB::query('UPDATE "' . $table . '" SET "LegacyID" = "ID"'); } // Done $this->message("Done!"); }