Skip to content

A free open source IT asset/license management system

Break audit date denorm migrations into two separate migrations

snipe31ca4bff8ca6
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 @@
+<?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()
+ {
+ //
+ }
+}