diff --git a/laravel_app/app/Models/ProjectMosaic.php b/laravel_app/app/Models/ProjectMosaic.php index 33cdece..b16dd9a 100644 --- a/laravel_app/app/Models/ProjectMosaic.php +++ b/laravel_app/app/Models/ProjectMosaic.php @@ -11,11 +11,16 @@ class ProjectMosaic extends Model use HasFactory; use \App\Traits\HasStatus; + protected $casts = [ + 'end_date' => 'datetime', + ]; + protected $fillable = [ 'name', 'path', 'week', 'year', + 'end_date', 'status' ]; diff --git a/laravel_app/database/migrations/2024_06_18_082255_update_year_and_week_columns_to_end_date_and_offset_project_mosaic_table.php b/laravel_app/database/migrations/2024_06_18_082255_update_year_and_week_columns_to_end_date_and_offset_project_mosaic_table.php new file mode 100644 index 0000000..c1df568 --- /dev/null +++ b/laravel_app/database/migrations/2024_06_18_082255_update_year_and_week_columns_to_end_date_and_offset_project_mosaic_table.php @@ -0,0 +1,58 @@ +date('end_date')->nullable(); + $table->integer('offset')->default(7); + }); + ProjectMosaic::all()->each(function (ProjectMosaic $project_mosaic) { + $end_date = Carbon::parse(sprintf('%s-W%02d',$project_mosaic->year,$project_mosaic->week))->addDay(Carbon::THURSDAY); + logger(''); + $project_mosaic->update(['end_date' => $end_date]); + }); + Schema::table('project_mosaics', function (Blueprint $table) { + $table->dropColumn('week'); + $table->dropColumn('year'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('project_mosaics', 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) + ProjectMosaic::all()->each(function (ProjectMosaic $project_mosaic) { + if (!$project_mosaic->end_date) { + return; // Skip if end_date is null + } + $project_mosaic->update(['year' => $project_mosaic->end_date->format('Y'), 'week' => $project_mosaic->end_date->format('W')]); + }); + + Schema::table('project_mosaics', function (Blueprint $table) { + // Drop end_date and offset columns + $table->dropColumn('end_date'); + $table->dropColumn('offset'); + }); + } + +};