Skip to content

A free open source IT asset/license management system

Migration to add location_id de-normed data

snipec70db75de928
diff --git a/database/migrations/2017_10_27_192423_migrate_denormed_asset_locations.php b/database/migrations/2017_10_27_192423_migrate_denormed_asset_locations.php
new file mode 100644
index 0000000000..dc6bee559d
--- /dev/null
+++ b/database/migrations/2017_10_27_192423_migrate_denormed_asset_locations.php
@@ -0,0 +1,92 @@
+<?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();
+ echo "\nUnasigned 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->save();
+ }
+
+ // Assigned to users - ::with('assignedTo') //can't eager-load polymorphic relations?
+ $assigned_user_assets = Asset::where('assigned_type',User::class)->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->save();
+
+ }
+
+ // Assigned to locations // with('assetloc')-> //can't eager-load polymorphic relationships
+ $assigned_location_assets = Asset::where('assigned_type',Location::class)->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->save();
+ }
+
+ // Assigned to assets
+ $assigned_asset_assets = Asset::with('assetloc')->where('assigned_type',Asset::class)->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->location) {
+ \Log::info('They are in '.$assigned_asset_asset->assignedTo->location->name);
+ }
+ \Log::info('User location is: '.$assigned_asset_asset->assetloc->name);
+ \Log::info('Setting asset '.$assigned_asset_asset->id.' location to '.$assigned_asset_asset->assetloc->id.' ('.$assigned_asset_asset->assetloc->name.')');
+ $assigned_asset_asset->location_id=$assigned_asset_asset->assetloc->id;
+
+ }
+
+ $unassigned_assets=Asset::whereNull("location_id")->get();
+ foreach($unassigned_assets as $unassigned_asset) {
+ \Log::info('Asset: ".$unassigned_asset->id." still has no location');
+ }
+
+ Asset::all()->each(function ($asset) {
+ if($asset->location_id != $asset->assetLoc()->id) {
+ \Log::info('MISMATCH MISMATCH '.$asset->id. "doesn't match its location");
+ }
+ });
+
+ die();
+
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ //
+ }
+}