Break audit date denorm migrations into two separate migrations

This commit is contained in:
snipe 2017-12-26 17:11:08 -08:00
parent 8864f81402
commit 31ca4bff8c
2 changed files with 56 additions and 17 deletions

View file

@ -16,23 +16,14 @@ class NormalizeAssetLastAuditDate extends Migration
public function up()
{
Schema::table('assets', function (Blueprint $table) {
$table->datetime('last_audit_date')->after('assigned_type')->nullable()->default(null);
});
// Grab the latest info from the Actionlog table where the action is 'audit'
$audits = Actionlog::selectRaw('MAX(created_at) AS created_at, item_id')->where('action_type', 'audit')->where('item_type', Asset::class)->groupBy('item_id')->orderBy('created_at', 'desc')->get();
if ($audits) {
foreach ($audits as $audit) {
$assets = Asset::where('id', $audit->item_id)->first();
$assets->last_audit_date = $audit->created_at;
$assets->unsetEventDispatcher();
$assets->save();
}
if (!Schema::hasColumn('assets', 'last_audit_date')) {
Schema::table('assets', function (Blueprint $table) {
$table->datetime('last_audit_date')->after('assigned_type')->nullable()->default(null);
});
}
}
/**
@ -42,8 +33,10 @@ class NormalizeAssetLastAuditDate extends Migration
*/
public function down()
{
Schema::table('assets', function (Blueprint $table) {
$table->dropColumn('last_audit_date');
});
if (Schema::hasColumn('assets', 'last_audit_date')) {
Schema::table('assets', function (Blueprint $table) {
$table->dropColumn('last_audit_date');
});
}
}
}

View file

@ -0,0 +1,46 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ReNormalizeLastAudit extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (Schema::hasColumn('assets', 'last_audit_date')) {
// Grab the latest info from the Actionlog table where the action is 'audit'
$audits = Actionlog::selectRaw('MAX(created_at) AS created_at, item_id')
->where('action_type', 'audit')
->where('item_type', Asset::class)
->groupBy('item_id')
->orderBy('created_at', 'desc')
->get();
if ($audits) {
foreach ($audits as $audit) {
$assets = Asset::where('id', $audit->item_id)->first();
$assets->last_audit_date = $audit->created_at;
$assets->unsetEventDispatcher();
$assets->save();
}
}
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}