Created
April 24, 2014 15:08
-
-
Save justinmccombs/11258185 to your computer and use it in GitHub Desktop.
Laravel Import Seeder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class NewTableSeeder extends ImportSeeder | |
| { | |
| public function run() | |
| { | |
| // Set up connections | |
| $this->oldConnection = 'old_mysql'; | |
| $this->newConnection = 'new_mysql'; | |
| // Set number of records to migrate, useful for limiting | |
| // the number of imported records during development | |
| $this->max = 100; | |
| // Old Column Name => New Column Name | |
| $this->fields = array( | |
| 'id' => 'id', | |
| 'oldtimestamp' => 'created_at', | |
| 'anotherColumn' => 'another_column' | |
| ); | |
| // anotherColumn must be greater than 5, or record will not be imported | |
| $this->rules = array( | |
| 'anotherColumn' => array(['>', 5]) | |
| ); | |
| // Call run on parent | |
| parent::run(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class ImportSeeder extends Seeder | |
| { | |
| /** | |
| * Old Connection Name | |
| * | |
| * @var string | |
| */ | |
| var $oldConnection; | |
| /** | |
| * New Connection Name | |
| * | |
| * @var string | |
| */ | |
| var $newConnection; | |
| /** | |
| * Table into which we will put new records | |
| * | |
| * @var string $table | |
| */ | |
| var $table; | |
| /** | |
| * Table into which we will pull old records | |
| * | |
| * @var string $oldTable | |
| */ | |
| var $oldTable; | |
| /** | |
| * Associative array to hold field mapping from old to new. | |
| * Format: | |
| * array( | |
| * 'newField' => 'oldField', | |
| * ... | |
| * ); | |
| * | |
| * @var array $fields | |
| */ | |
| var $fields; | |
| /** | |
| * Array of rules set on fields that, if not passed, the new record is not created. | |
| * Format: | |
| * array( | |
| * 'field' => array( ['===', 'value'] ), | |
| * 'field2' => array( ['!==', 'value'] , ['<', 'value'] ), | |
| * ); | |
| * | |
| * @var array $rules | |
| */ | |
| var $rules; | |
| /** | |
| * Record Number to start from old table | |
| * | |
| * @var int $start | |
| */ | |
| var $start = 0; | |
| /** | |
| * Number of records to fetch in each loop. | |
| * | |
| * @var int $fetch | |
| */ | |
| var $fetch = 100; | |
| /** | |
| * Total number of records in old table. | |
| * | |
| * @var int $fullCount | |
| */ | |
| var $fullCount; | |
| /** | |
| * Maximum number of records to import. This defaults to the | |
| * number in the config file, but can be overridden before run(); | |
| * If $max is equal to 0 or null, all records are seeded. | |
| * | |
| * @var int $max | |
| */ | |
| var $max; | |
| /** | |
| * Number of records added to new database. | |
| * | |
| * @var int $added | |
| */ | |
| var $added; | |
| /** | |
| * Number of records not added due to rule violations | |
| * | |
| * @var int $failed | |
| */ | |
| var $failed; | |
| public function __construct() | |
| { | |
| $this->start = 0; | |
| $this->fetch = 100; | |
| $this->max = 0; | |
| $this->rules = array(); | |
| } | |
| public function run() | |
| { | |
| $newObjects = array(); | |
| $this->fullCount = DB::connection($this->oldConection)->table($this->oldTable)->count(); | |
| for ($i = $this->start; $i <= ($this->fullCount + $this->fetch); $i += $this->fetch) { | |
| $oldObjects = DB::connection($this->oldConection)->table($this->oldTable) | |
| ->limit($this->fetch)->skip($i)->get(); | |
| foreach ($oldObjects as $oldObject) { | |
| $newObjectData = array(); | |
| $itemFailsRules = false; | |
| foreach ($this->fields as $oldField => $newField) { | |
| if ($this->hasRulesForKey($oldField)) { | |
| foreach($this->getRulesForKey($oldField) as $rule) { | |
| if (!$this->rulePasses($rule, $oldObject->$oldField)) $itemFailsRules = true; | |
| } | |
| } | |
| $newObjectData[$newField] = $oldObject->$oldField; | |
| } | |
| if ($itemFailsRules) { | |
| $this->failed++; | |
| continue; // Continue loop through old objects | |
| } | |
| $newObjects[] = $newObjectData; | |
| $this->added++; | |
| if ($this->max > 0) { | |
| if ($this->added == $this->max) { | |
| DB::connection($this->newConnection)->table($this->table)->insert($newObjects); | |
| return; | |
| } | |
| } | |
| } | |
| } | |
| DB::connection($this->newConnection)->table($this->table)->insert($newObjects); | |
| } | |
| private function hasRulesForKey($key) { | |
| return (array_key_exists($key, $this->rules)); | |
| } | |
| private function getRulesForKey($key) { | |
| if (!$this->hasRulesForKey($key)) return array(); | |
| return $this->rules[$key]; | |
| } | |
| private function rulePasses($rule, $value) { | |
| if (count($rule) !== 2) throw new Exception("ImportSeeder: Rule is malformed."); | |
| $operator = $rule[0]; | |
| $comparisonValue = $rule[1]; | |
| switch($operator) { | |
| case "==": return $value == $comparisonValue; | |
| case "===": return $value === $comparisonValue; | |
| case "<": return $value < $comparisonValue; | |
| case "<=": return $value <= $comparisonValue; | |
| case ">": return $value > $comparisonValue; | |
| case ">=": return $value >= $comparisonValue; | |
| case "!=": return $value != $comparisonValue; | |
| case "!==": return $value !== $comparisonValue; | |
| } | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment