From 31ca4bff8ca6b4f4f60d998c18b6c5841a48094e Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 26 Dec 2017 17:11:08 -0800 Subject: [PATCH] Break audit date denorm migrations into two separate migrations --- ...010457_normalize_asset_last_audit_date.php | 27 ++++------- ...7_12_26_170856_re_normalize_last_audit.php | 46 +++++++++++++++++++ 2 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 database/migrations/2017_12_26_170856_re_normalize_last_audit.php diff --git a/database/migrations/2017_12_12_010457_normalize_asset_last_audit_date.php b/database/migrations/2017_12_12_010457_normalize_asset_last_audit_date.php index cf80c2faa2..d827e4890d 100644 --- a/database/migrations/2017_12_12_010457_normalize_asset_last_audit_date.php +++ b/database/migrations/2017_12_12_010457_normalize_asset_last_audit_date.php @@ -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'); + }); + } } } diff --git a/database/migrations/2017_12_26_170856_re_normalize_last_audit.php b/database/migrations/2017_12_26_170856_re_normalize_last_audit.php new file mode 100644 index 0000000000..985f03e766 --- /dev/null +++ b/database/migrations/2017_12_26_170856_re_normalize_last_audit.php @@ -0,0 +1,46 @@ +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() + { + // + } +}