[DB] Migration that updates the values year and week to end_date and offset

This commit is contained in:
guillaume91 2024-06-18 16:44:18 +02:00
parent 5153228758
commit 2867288eef
2 changed files with 61 additions and 11 deletions

View file

@ -15,7 +15,8 @@
class ProjectReport extends Model class ProjectReport extends Model
{ {
use HasStatus; use HasStatus;
protected $fillable = ['name', 'path', 'week', 'year','status']; protected $casts = [ 'end_date' => 'datetime'];
protected $fillable = ['name', 'path', 'week', 'year','status', 'end_date'];
@ -24,19 +25,10 @@ public function project()
return $this->belongsTo(Project::class); return $this->belongsTo(Project::class);
} }
public function weeksAgo()
{
return $this->weeksAgoForYearAndWeek($this->year, $this->week);
}
public static function weeksAgoForYearAndWeek($year, $week)
{
return (now()->week - now()->setISODate($year, $week)->week);
}
public function getFileName() public function getFileName()
{ {
return 'week_'.$this->week.'_'.$this->year; return 'Period_'.$this->end_date->copy()->subDays($this->offset).'_'.$this->end_date;
} }
public function getFullPathName() public function getFullPathName()
@ -46,6 +38,7 @@ public function getFullPathName()
public function getReportDate() public function getReportDate()
{ {
// TODO remove year and week
return self::getReportDateForYearAndWeek( return self::getReportDateForYearAndWeek(
$this->project, $this->project,
$this->year, $this->year,

View file

@ -0,0 +1,57 @@
<?php
use App\Models\ProjectReport;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('project_reports', function (Blueprint $table) {
$table->date('end_date')->nullable();
$table->integer('offset')->default(7);
});
ProjectReport::all()->each(function (ProjectReport $project_report) {
$end_date = Carbon::parse(sprintf('%s-W%02d',$project_report->year,$project_report->week))->addDay(Carbon::THURSDAY);
$project_report->update(['end_date' => $end_date]);
});
Schema::table('project_reports', function (Blueprint $table) {
$table->dropColumn('week');
$table->dropColumn('year');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('project_reports', function (Blueprint $table) {
// Add back year and week columns
$table->integer('year');
$table->integer('week');
});
// Extract year and week from end_date (assuming logic applied in up() is reversible)
ProjectReport::all()->each(function (ProjectReport $project_report) {
if (!$project_report->end_date) {
return; // Skip if end_date is null
}
$project_report->update(['year' => $project_report->end_date->format('Y'), 'week' => $project_report->end_date->format('W')]);
});
Schema::table('project_reports', function (Blueprint $table) {
// Drop end_date and offset columns
$table->dropColumn('end_date');
$table->dropColumn('offset');
});
}
};