user affiliation schema completed

This commit is contained in:
Giuseppe Naponiello
2026-06-24 16:30:21 +02:00
parent 032f6a08df
commit 6a07c3e922
13 changed files with 890 additions and 3 deletions

View File

@@ -0,0 +1,53 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('user_affiliations', function (Blueprint $table) {
$table->id();
$table->foreignId('institution_id')
->constrained('institutions')
->cascadeOnUpdate()
->cascadeOnDelete();
$table->foreignId('user_id')
->constrained('users')
->cascadeOnUpdate()
->cascadeOnDelete();
$table->foreignId('user_position_id')
->constrained('user_positions')
->cascadeOnUpdate()
->cascadeOnDelete();
$table->integer('start_year');
$table->integer('end_year')->nullable();
$table->softDeletes();
// Aperta = end_year nullo E non cestinata: un soft delete libera lo slot
// per un nuovo incarico aperto, senza dover aspettare un restore/force.
$table->boolean('is_open')->virtualAs('IF(`end_year` IS NULL AND `deleted_at` IS NULL, 1, NULL)');
// Legacy: in v1 affiliazione/posizione vivevano dentro "person" (un solo
// incarico per persona), qui normalizzate per permettere più affiliazioni.
// legacy_user_id identifica univocamente la riga "person" d'origine (chiave
// di upsert per l'ETL); legacy_institution_id è solo un riferimento
// informativo, condiviso da più affiliazioni verso lo stesso ente.
$table->unsignedBigInteger('legacy_user_id')->nullable()->unique();
$table->unsignedBigInteger('legacy_institution_id')->nullable();
$table->timestamps();
$table->unique(['institution_id', 'user_id', 'is_open']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('user_affiliations');
}
};

View File

@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
// Id del record nel DB legacy (v1): chiave di upsert per l'ETL e per
// risolvere le FK delle tabelle dipendenti (come institutions.legacy_id).
$table->unsignedBigInteger('legacy_id')->nullable()->unique()->after('id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('legacy_id');
});
}
};