<commit_before><?php

namespace App\Console\Commands\Migrations;

use App\Entities\Contest;
use App\Entities\User;
use App\Services\UserService;

class ContestMigration extends Migration
{
    public function handle($command)
    {
        $command->info('Migrating Contest...');
        $this->table('contest')->orderBy('contest_id', 'asc')->chunk(100, function ($objects) {
            $objects->map(function ($object) {
                $this->transform($object);
            });
        });
        $command->info('Migrating Contest Done');
    }

    private function transform($object)
    {
        $contest = new Contest();
        $contest->fill(get_object_vars($object));
        $contest->id = $object->contest_id;
        if ($contest->description === null) {
            $contest->description = '';
        }
        if ($object->defunct === 'Y') {
            $contest->hidden = 1;
        }
        $contest->save();

        $this->migrateProblemRelation($contest);

        if ($contest->private) {
            $this->migratePrivilege($contest);
        }
    }

    /**
     * @param Contest $contest
     */
    private function migrateProblemRelation($contest)
    {
        $oldRelations = $this->table('contest_problem')->where('contest_id', $contest->id)->get();
        $relations = [];
        foreach ($oldRelations as $relation) {
            $relations[$relation->problem_id] = [
                'order' => $relation->num,
                'title' => $relation->title,
            ];
        }
        $contest->problems()->sync($relations);
    }

    /**
     * @param Contest $contest
     */
    private function migratePrivilege($contest)
    {
        $perm = 'c'.$contest->id;
        $perms = $this->table('privilege')->where('rightstr', $perm)->get();

        $perms->map(function ($perm) use ($contest) {
            /** @var User $user */
            $user = app(UserService::class)->findByName($perm->user_id);
            $contest->users()->attach($user);
        });
    }
}<commit_msg>fix(migration): fix migrate from old database<commit_after><?php

namespace App\Console\Commands\Migrations;

use App\Entities\Contest;
use App\Entities\User;
use App\Services\UserService;

class ContestMigration extends Migration
{
    public function handle($command)
    {
        $command->info('Migrating Contest...');
        $this->table('contest')->orderBy('contest_id', 'asc')->chunk(100, function ($objects) {
            $objects->map(function ($object) {
                $this->transform($object);
            });
        });
        $command->info('Migrating Contest Done');
    }

    private function transform($object)
    {
        $contest = new Contest();
        $contest->fill(get_object_vars($object));
        $contest->id = $object->contest_id;
        if ($contest->description === null) {
            $contest->description = '';
        }
        if ($object->defunct === 'Y') {
            $contest->hidden = 1;
        }
        $contest->save();

        $this->migrateProblemRelation($contest);

        if ($contest->private) {
            $this->migratePrivilege($contest);
        }
    }

    /**
     * @param Contest $contest
     */
    private function migrateProblemRelation($contest)
    {
        $oldRelations = $this->table('contest_problem')->where('contest_id', $contest->id)->get();
        $relations = [];
        foreach ($oldRelations as $relation) {
            $relations[$relation->problem_id] = [
                'order' => $relation->num,
                'title' => $relation->title,
            ];
        }
        $contest->problems()->sync($relations);
    }

    /**
     * @param Contest $contest
     */
    private function migratePrivilege($contest)
    {
        $perm = 'c'.$contest->id;
        $perms = $this->table('privilege')->where('rightstr', $perm)->distinct('user_id')->get();

        $perms->map(function ($perm) use ($contest) {
            /** @var User $user */
            $user = app(UserService::class)->findByName($perm->user_id);
            $contest->users()->attach($user);
        });
    }
}
{'fraction_non_alphanumeric': 0.17360603847090333, 'fraction_numerical': 0.0019478938397857316, 'mean_word_length': 2.186966640806827, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3828703', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1193, 'n_words': 189}