|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands\Migrations; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entities\Contest; |
|
6
|
|
|
use App\Entities\User; |
|
7
|
|
|
use App\Services\UserService; |
|
8
|
|
|
|
|
9
|
|
|
class ContestMigration extends Migration |
|
10
|
|
|
{ |
|
11
|
|
|
public function handle($command) |
|
12
|
|
|
{ |
|
13
|
|
|
$command->info('Migrating Contest...'); |
|
14
|
|
|
$this->table('contest')->orderBy('contest_id', 'asc')->chunk(100, function ($objects) { |
|
15
|
|
|
$objects->map(function ($object) { |
|
16
|
|
|
$this->transform($object); |
|
17
|
|
|
}); |
|
18
|
|
|
}); |
|
19
|
|
|
$command->info('Migrating Contest Done'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
private function transform($object) |
|
23
|
|
|
{ |
|
24
|
|
|
$contest = new Contest(); |
|
25
|
|
|
$contest->fill(get_object_vars($object)); |
|
26
|
|
|
$contest->id = $object->contest_id; |
|
27
|
|
|
if ($contest->description === null) { |
|
28
|
|
|
$contest->description = ''; |
|
29
|
|
|
} |
|
30
|
|
|
$contest->status = 0; |
|
31
|
|
|
if ($object->defunct === 'Y') { |
|
32
|
|
|
$contest->status = Contest::ST_HIDE; |
|
33
|
|
|
} |
|
34
|
|
|
$contest->save(); |
|
35
|
|
|
|
|
36
|
|
|
$this->migrateProblemRelation($contest); |
|
37
|
|
|
|
|
38
|
|
|
if ($contest->private) { |
|
39
|
|
|
$this->migratePrivilege($contest); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Contest $contest |
|
45
|
|
|
*/ |
|
46
|
|
|
private function migrateProblemRelation($contest) |
|
47
|
|
|
{ |
|
48
|
|
|
$oldRelations = $this->table('contest_problem')->where('contest_id', $contest->id)->get(); |
|
49
|
|
|
$relations = []; |
|
50
|
|
|
foreach ($oldRelations as $relation) { |
|
51
|
|
|
$relations[$relation->problem_id] = [ |
|
52
|
|
|
'order' => $relation->num, |
|
53
|
|
|
'title' => $relation->title, |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
$contest->problems()->sync($relations); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param Contest $contest |
|
61
|
|
|
*/ |
|
62
|
|
|
private function migratePrivilege($contest) |
|
63
|
|
|
{ |
|
64
|
|
|
$perm = 'c'.$contest->id; |
|
65
|
|
|
$perms = $this->table('privilege')->where('rightstr', $perm)->distinct('user_id')->get(); |
|
66
|
|
|
|
|
67
|
|
|
$perms->map(function ($perm) use ($contest) { |
|
68
|
|
|
/** @var User $user */ |
|
69
|
|
|
$user = app(UserService::class)->findByName($perm->user_id); |
|
70
|
|
|
$contest->users()->attach($user); |
|
71
|
|
|
}); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|