refactor(templates): plantillas globales + asignación por proyecto (pivot)
- Migración: tabla pivot inspection_template_project; drop de phase_id (asociación a fase descartada); project_id se mantiene en inspection_templates por compat. Migra automáticamente las plantillas existentes (project_id → pivot). - Modelos: Project::inspectionTemplates() ↔ InspectionTemplate::projects() (BTM); retirada la relación phase() de InspectionTemplate. - GlobalTemplateManager (nuevo, ruta /inspection-templates, permiso manage templates): catálogo único global, CRUD + import CSV; sin asociación a fase ni a proyecto. - ProjectTemplatesPicker (nuevo, ruta projects.templates, permiso edit projects): lista global con checkbox para asignar/desasignar plantillas al proyecto. - ProjectMap: el selector de plantillas del mapa lee SOLO las asignadas al proyecto vía pivot. API móvil (bundle + /templates) adaptado al pivot. - Eliminado el viejo TemplateManager por-proyecto, su vista wrapper y la tabla ImportTemplatesTable (ya no aplica: todas son globales). - Acceso "Plantillas" añadido al menú principal (gate manage templates). Tests: GlobalTemplatesTest (4). Suite 89 passing. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Templates pasan a ser GLOBALES. Se introduce una tabla pivot many-to-many
|
||||
* para "asignar" plantillas globales a proyectos. Drop de la columna phase_id
|
||||
* (asociación a fase descartada). Se mantiene project_id en la tabla por
|
||||
* compatibilidad — pero ya no es la fuente de pertenencia.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// Pivot template ↔ project
|
||||
Schema::create('inspection_template_project', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('inspection_template_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('project_id')->constrained()->cascadeOnDelete();
|
||||
$table->timestamps();
|
||||
$table->unique(['inspection_template_id', 'project_id'], 'itp_template_project_unique');
|
||||
});
|
||||
|
||||
// Migrar la asignación existente (project_id en inspection_templates → pivot)
|
||||
$rows = DB::table('inspection_templates')->whereNotNull('project_id')->get(['id', 'project_id']);
|
||||
foreach ($rows as $r) {
|
||||
// Evitar duplicados; si el proyecto ya no existe lo ignoramos
|
||||
$projectExists = DB::table('projects')->where('id', $r->project_id)->exists();
|
||||
if (! $projectExists) continue;
|
||||
|
||||
DB::table('inspection_template_project')->updateOrInsert(
|
||||
['inspection_template_id' => $r->id, 'project_id' => $r->project_id],
|
||||
['created_at' => now(), 'updated_at' => now()],
|
||||
);
|
||||
}
|
||||
|
||||
// Drop columna phase_id (la asociación a fase deja de existir)
|
||||
if (Schema::hasColumn('inspection_templates', 'phase_id')) {
|
||||
Schema::table('inspection_templates', function (Blueprint $table) {
|
||||
// Algunos motores (SQLite) requieren dropear el índice antes de la columna.
|
||||
try { $table->dropIndex('inspection_templates_phase_id_index'); } catch (\Throwable $e) { /* ya no existe */ }
|
||||
try { $table->dropForeign(['phase_id']); } catch (\Throwable $e) { /* sqlite sin FKs */ }
|
||||
$table->dropColumn('phase_id');
|
||||
});
|
||||
}
|
||||
|
||||
// project_id se mantiene en inspection_templates (puede usarse como
|
||||
// "proyecto creador/origen" pero ya no determina visibilidad).
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
if (! Schema::hasColumn('inspection_templates', 'phase_id')) {
|
||||
Schema::table('inspection_templates', function (Blueprint $table) {
|
||||
$table->foreignId('phase_id')->nullable()->constrained('phases')->onDelete('set null');
|
||||
$table->index('phase_id');
|
||||
});
|
||||
}
|
||||
|
||||
Schema::dropIfExists('inspection_template_project');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user