mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
add marcus' migration changes to branch
This commit is contained in:
parent
e028145ad9
commit
7c30261ec8
|
@ -3,6 +3,7 @@
|
||||||
use App\Models\Asset;
|
use App\Models\Asset;
|
||||||
use Carbon\CarbonImmutable;
|
use Carbon\CarbonImmutable;
|
||||||
use Illuminate\Database\Migrations\Migration;
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Query\Expression;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
@ -50,7 +51,7 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration
|
||||||
->whereNotNull('model_id')
|
->whereNotNull('model_id')
|
||||||
->join('models', 'assets.model_id', '=', 'models.id')
|
->join('models', 'assets.model_id', '=', 'models.id')
|
||||||
->update([
|
->update([
|
||||||
'asset_eol_date' => DB::raw('DATE_ADD(purchase_date, INTERVAL ' . DB::getTablePrefix() . 'models.eol MONTH)')
|
'asset_eol_date' => $this->eolUpdateExpression(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,4 +67,22 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration
|
||||||
$table->dropColumn('eol_explicit');
|
$table->dropColumn('eol_explicit');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns the correct database expression for either
|
||||||
|
* mysql, postgres, or sqlite depending on the driver being used.
|
||||||
|
*/
|
||||||
|
private function eolUpdateExpression(): Expression
|
||||||
|
{
|
||||||
|
if (DB::getDriverName() === 'sqlite') {
|
||||||
|
return DB::raw("DATE(purchase_date, '+' || (SELECT eol FROM " . DB::getTablePrefix() . "models WHERE models.id = assets.model_id) || ' months')");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DB::getDriverName() === 'pgsql') {
|
||||||
|
return DB::raw("date(purchase_date + interval '1 month' * (SELECT eol FROM " . DB::getTablePrefix() . "models WHERE models.id = assets.model_id))");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default to MySQL's method
|
||||||
|
return DB::raw('DATE_ADD(purchase_date, INTERVAL ' . DB::getTablePrefix() . 'models.eol MONTH)');
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue