<commit_before><?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Models\Asset;
use App\Models\User;
use App\Models\Location;

class MigrateDenormedAssetLocations extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        // Unassigned
        $rtd_assets = Asset::whereNull('assigned_to')->with('defaultLoc')->get();
        \Log::info('Unasigned assets: ');
        foreach ($rtd_assets as $rtd_asset) {
            \Log::info('Setting asset '.$rtd_asset->id.' to  location: '.$rtd_asset->rtd_location_id." Because asset's default location is: ".$rtd_asset->rtd_location_id);
            $rtd_asset->location_id=$rtd_asset->rtd_location_id;
            $rtd_asset->unsetEventDispatcher();
            $rtd_asset->save();
        }

        // Assigned to users - ::with('assignedTo') //can't eager-load polymorphic relations?
        $assigned_user_assets = Asset::where('assigned_type',User::class)->whereNotNull('assigned_to')->get();
        \Log::debug('User-assigned assets:');
        foreach ($assigned_user_assets as $assigned_user_asset) {
            if ($assigned_user_asset->assignedTo->userLoc) {
                $new_location=$assigned_user_asset->assignedTo->userloc->id;
                \Log::info(' They are in '.$assigned_user_asset->assignedTo->userloc->name.' which is id: '.$new_location);
            } else {
                \Log::info('They have no location! ');
                $new_location=$assigned_user_asset->rtd_location_id;
            }
            $assigned_user_asset->location_id=$new_location;
            $assigned_user_asset->unsetEventDispatcher();
            $assigned_user_asset->save();

        }

        // Assigned to locations // with('assetloc')-> //can't eager-load polymorphic relationships
        $assigned_location_assets = Asset::where('assigned_type',Location::class)->whereNotNull('assigned_to')->get();
        \Log::info('Location-assigned assets: ');
        foreach ($assigned_location_assets as $assigned_location_asset) {
            $assigned_location_asset->location_id=$assigned_location_asset->assignedTo->id;
            \Log::info('(calculated to be: '.$assigned_location_asset->assetLoc());
            $assigned_location_asset->unsetEventDispatcher();
            $assigned_location_asset->save();
        }

        // Assigned to assets
        $assigned_asset_assets = Asset::where('assigned_type',Asset::class)->whereNotNull('assigned_to')->with('assetloc')->get();
        \Log::info('Asset-assigned assets: ');
        foreach ($assigned_asset_assets as $assigned_asset_asset) {
            \Log::info('This asset is: '.$assigned_asset_asset->assignedTo->asset_tag);
            if (($assigned_asset_asset->assignedTo) && ($assigned_asset_asset->assignedTo->location)) {
                \Log::info('They are in '.$assigned_asset_asset->assignedTo->location->name);
            }<commit_msg>No idea why this is needed, but…<commit_after><?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Models\Asset;
use App\Models\User;
use App\Models\Location;

class MigrateDenormedAssetLocations extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        // Unassigned
        $rtd_assets = Asset::whereNull('assigned_to')->with('defaultLoc')->get();
        \Log::info('Unasigned assets: ');
        foreach ($rtd_assets as $rtd_asset) {
            \Log::info('Setting asset '.$rtd_asset->id.' to  location: '.$rtd_asset->rtd_location_id." Because asset's default location is: ".$rtd_asset->rtd_location_id);
            $rtd_asset->location_id=$rtd_asset->rtd_location_id;
            $rtd_asset->unsetEventDispatcher();
            $rtd_asset->save();
        }

        // Assigned to users - ::with('assignedTo') //can't eager-load polymorphic relations?
        $assigned_user_assets = Asset::where('assigned_type',User::class)->whereNotNull('assigned_to')->get();
        \Log::debug('User-assigned assets:');
        foreach ($assigned_user_assets as $assigned_user_asset) {
            if (($assigned_user_asset->assignedTo) && ($assigned_user_asset->assignedTo->userLoc)) {
                $new_location=$assigned_user_asset->assignedTo->userloc->id;
                \Log::info(' They are in '.$assigned_user_asset->assignedTo->userloc->name.' which is id: '.$new_location);
            } else {
                \Log::info('They have no location! ');
                $new_location=$assigned_user_asset->rtd_location_id;
            }
            $assigned_user_asset->location_id=$new_location;
            $assigned_user_asset->unsetEventDispatcher();
            $assigned_user_asset->save();

        }

        // Assigned to locations // with('assetloc')-> //can't eager-load polymorphic relationships
        $assigned_location_assets = Asset::where('assigned_type',Location::class)->whereNotNull('assigned_to')->get();
        \Log::info('Location-assigned assets: ');
        foreach ($assigned_location_assets as $assigned_location_asset) {
            $assigned_location_asset->location_id=$assigned_location_asset->assignedTo->id;
            \Log::info('(calculated to be: '.$assigned_location_asset->assetLoc());
            $assigned_location_asset->unsetEventDispatcher();
            $assigned_location_asset->save();
        }

        // Assigned to assets
        $assigned_asset_assets = Asset::where('assigned_type',Asset::class)->whereNotNull('assigned_to')->with('assetloc')->get();
        \Log::info('Asset-assigned assets: ');
        foreach ($assigned_asset_assets as $assigned_asset_asset) {
            \Log::info('This asset is: '.$assigned_asset_asset->assignedTo->asset_tag);
            if (($assigned_asset_asset->assignedTo) && ($assigned_asset_asset->assignedTo->location)) {
                \Log::info('They are in '.$assigned_asset_asset->assignedTo->location->name);
            }
{'fraction_non_alphanumeric': 0.14453699167074963, 'fraction_numerical': 0.0, 'mean_word_length': 3.093582887700535, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 90, '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': '18865', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1809, 'n_words': 269}