Skip to content

Commit dcdc294

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents 2a9ed09 + 04f6f39 commit dcdc294

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

app/Observers/AssetObserver.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,30 @@ public function deleting(Asset $asset)
120120
$logAction->user_id = Auth::id();
121121
$logAction->logaction('delete');
122122
}
123-
123+
124+
/**
125+
* Executes every time an asset is saved.
126+
*
127+
* This matters specifically because any database fields affected here MUST already exist on
128+
* the assets table (and/or any related models), or related migrations WILL fail.
129+
*
130+
* For example, if there is a database migration that's a bit older and modifies an asset, if the save
131+
* fires before a field gets created in a later migration and that field in the later migration
132+
* is used in this observer, it doesn't actually exist yet and the migration will break unless we
133+
* use saveQuietly() in the migration which skips this observer.
134+
*
135+
* @see https://github.com/snipe/snipe-it/issues/13723#issuecomment-1761315938
136+
*/
124137
public function saving(Asset $asset)
125138
{
126-
//determine if calculated eol and then calculate it - this should only happen on a new asset
127-
if(is_null($asset->asset_eol_date) && !is_null($asset->purchase_date) && !is_null($asset->model->eol)){
139+
// determine if calculated eol and then calculate it - this should only happen on a new asset
140+
if (is_null($asset->asset_eol_date) && !is_null($asset->purchase_date) && !is_null($asset->model->eol)){
128141
$asset->asset_eol_date = $asset->purchase_date->addMonths($asset->model->eol)->format('Y-m-d');
129142
$asset->eol_explicit = false;
130143
}
131144

132-
//determine if explicit and set eol_explit to true
133-
if(!is_null($asset->asset_eol_date) && !is_null($asset->purchase_date)) {
145+
// determine if explicit and set eol_explicit to true
146+
if (!is_null($asset->asset_eol_date) && !is_null($asset->purchase_date)) {
134147
if($asset->model->eol) {
135148
$months = Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date);
136149
if($months != $asset->model->eol) {

database/migrations/2023_01_21_225350_add_eol_date_on_assets_table.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,30 @@ public function up()
1717
{
1818

1919
Schema::table('assets', function (Blueprint $table) {
20+
2021
if (!Schema::hasColumn('assets', 'asset_eol_date')) {
2122
$table->date('asset_eol_date')->after('purchase_date')->nullable()->default(null);
2223
}
24+
25+
// This is a temporary shim so we don't have to modify the asset observer for migrations where
26+
// there is a large version difference. (See the AssetObserver notes). This column gets created
27+
// later in 2023_07_13_052204_denormalized_eol_and_add_column_for_explicit_date_to_assets.php
28+
// but we have to temporarily create it now so the save method below doesn't break
29+
if (!Schema::hasColumn('assets', 'eol_explicit')) {
30+
$table->boolean('eol_explicit')->default(false)->after('asset_eol_date');
31+
}
2332
});
2433

2534
// Chunk the model query to get the models that do have an EOL date
35+
// We use saveQuietly() here to skip the AssetObserver, since it modifies fields
36+
// that do not yet exist on the assets table.
2637
AssetModel::whereNotNull('eol')->chunk(10, function ($models) {
2738
foreach ($models as $model) {
2839
foreach ($model->assets as $asset) {
2940

3041
if ($asset->purchase_date!='') {
3142
$asset->asset_eol_date = $asset->present()->eol_date();
32-
$asset->save();
43+
$asset->saveQuietly();
3344
}
3445

3546
}

database/migrations/2023_07_13_052204_denormalized_eol_and_add_column_for_explicit_date_to_assets.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration
1818
public function up()
1919
{
2020
Schema::table('assets', function (Blueprint $table) {
21-
$table->boolean('eol_explicit')->default(false)->after('asset_eol_date');
21+
if (!Schema::hasColumn('assets', 'eol_explicit')) {
22+
$table->boolean('eol_explicit')->default(false)->after('asset_eol_date');
23+
}
2224
});
2325

2426

0 commit comments

Comments
 (0)