|
5 | 5 | class ConsistentForeignKeyNames extends Migration { |
6 | 6 |
|
7 | 7 | public function up() { |
8 | | - $this->chunkKeyRenaming(new DocumentAPI, 'lrs', 'lrs_id'); |
9 | | - $this->chunkKeyRenaming(new Statement, 'lrs._id', 'lrs_id'); |
10 | | - $this->chunkKeyRenaming(new Lrs, 'owner._id', 'owner_id'); |
11 | | - $this->chunkKeyRenaming(new Report, 'lrs', 'lrs_id'); |
12 | | - $this->chunkKeyRenaming(new Export, 'lrs', 'lrs_id'); |
13 | | - } |
14 | | - |
15 | | - public function down() { |
16 | | - $this->chunkMongoIdRemoval(new DocumentAPI, 'lrs', 'lrs_id'); |
17 | | - $this->chunkMongoIdRemoval(new Statement, 'lrs._id', 'lrs_id'); |
18 | | - $this->chunkMongoIdRemoval(new Lrs, 'owner._id', 'owner_id'); |
19 | | - $this->chunkMongoIdRemoval(new Report, 'lrs', 'lrs_id'); |
20 | | - $this->chunkMongoIdRemoval(new Export, 'lrs', 'lrs_id'); |
21 | | - } |
| 8 | + $db = \DB::getMongoDB(); |
22 | 9 |
|
23 | | - private function chunkKeyRenaming($model, $old_name, $new_name) { |
24 | | - $this->chunkModelMigration($model, $this->renameKey($old_name, $new_name)); |
25 | | - } |
| 10 | + Lrs::get()->each(function (Lrs $lrs) use ($db) { |
| 11 | + $convertToMongoId = function ($value) { |
| 12 | + return new \MongoId($value); |
| 13 | + }; |
| 14 | + $this->changeForeignKey($db->statements, 'lrs._id', 'lrs_id', $lrs->_id, $convertToMongoId); |
| 15 | + $this->changeForeignKey($db->documentapi, 'lrs', 'lrs_id', $lrs->_id, $convertToMongoId); |
| 16 | + $this->changeForeignKey($db->reports, 'lrs', 'lrs_id', $lrs->_id, $convertToMongoId); |
| 17 | + $this->changeForeignKey($db->exports, 'lrs', 'lrs_id', $lrs->_id, $convertToMongoId); |
26 | 18 |
|
27 | | - private function chunkMongoIdRemoval($model, $old_name, $new_name) { |
28 | | - $this->chunkModelMigration($model, $this->removeMongoId($old_name, $new_name)); |
29 | | - } |
| 19 | + $lrs->owner_id = $convertToMongoId($lrs->owner['_id']); |
| 20 | + $lrs->save(); |
30 | 21 |
|
31 | | - private function removeMongoId($old_name, $new_name) { |
32 | | - return function ($model) use ($old_name, $new_name) { |
33 | | - $value = $model->$new_name; |
34 | | - $model = $this->setKey($model, explode('.', $old_name), 0, (string) $value); |
35 | | - $model->save(); |
36 | | - }; |
37 | | - } |
| 22 | + echo 'Models for "'.$lrs->title.'" converted.'.PHP_EOL; |
| 23 | + }); |
38 | 24 |
|
39 | | - private function setKey($model, $keys, $key_index, $value) { |
40 | | - if ($key_index < count($keys) - 1) { |
41 | | - $model->{$keys[$key_index]} = (object) []; |
42 | | - $this->setKey($model->{$keys[$key_index]}, $keys, $key_index + 1, $value); |
43 | | - } else { |
44 | | - $model->{$keys[$key_index]} = $value; |
45 | | - } |
46 | | - return $model; |
47 | | - } |
| 25 | + echo 'All finished, hopefully!'.PHP_EOL; |
| 26 | + } |
48 | 27 |
|
49 | | - private function renameKey($old_name, $new_name) { |
50 | | - return function ($model) use ($old_name, $new_name) { |
51 | | - $value = array_reduce(explode('.', $old_name), function ($value, $key) { |
52 | | - return is_object($value) ? $value->{$key} : $value[$key]; |
53 | | - }, $model); |
54 | | - $model->$new_name = new \MongoId($value); |
55 | | - $model->save(); |
56 | | - }; |
| 28 | + private function changeForeignKey($collection, $old_key, $new_key, $old_value, $modifier) { |
| 29 | + $collection->update([ |
| 30 | + $old_key => $old_value |
| 31 | + ], [ |
| 32 | + '$set' => [ |
| 33 | + $new_key => $modifier($old_value) |
| 34 | + ] |
| 35 | + ], [ |
| 36 | + 'multiple' => true |
| 37 | + ]); |
57 | 38 | } |
58 | 39 |
|
59 | | - private function chunkModelMigration($model, Callable $migration) { |
60 | | - $model->chunk(1000, function ($models) use ($migration) { |
61 | | - foreach ($models as $model){ |
62 | | - $migration($model); |
63 | | - } |
64 | | - echo count($models) . ' converted.'.PHP_EOL; |
| 40 | + public function down() { |
| 41 | + $db = \DB::getMongoDB(); |
| 42 | + |
| 43 | + Lrs::get()->each(function (Lrs $lrs) use ($db) { |
| 44 | + $convertToString = function ($value) { |
| 45 | + return (string) $value; |
| 46 | + }; |
| 47 | + $this->changeForeignKey($db->statements, 'lrs_id', 'lrs._id', $lrs->_id, $convertToString); |
| 48 | + $this->changeForeignKey($db->documentapi, 'lrs_id', 'lrs', $lrs->_id, $convertToString); |
| 49 | + $this->changeForeignKey($db->reports, 'lrs_id', 'lrs', $lrs->_id, $convertToString); |
| 50 | + $this->changeForeignKey($db->exports, 'lrs_id', 'lrs', $lrs->_id, $convertToString); |
| 51 | + |
| 52 | + $lrs->owner = [ |
| 53 | + '_id' => $convertToString($lrs->owner_id) |
| 54 | + ]; |
| 55 | + $lrs->save(); |
| 56 | + |
| 57 | + echo 'Models for "'.$lrs->title.'" converted.'.PHP_EOL; |
65 | 58 | }); |
66 | 59 |
|
67 | 60 | echo 'All finished, hopefully!'.PHP_EOL; |
|
0 commit comments