user affiliation schema completed
This commit is contained in:
33
backend/database/factories/UserAffiliationFactory.php
Normal file
33
backend/database/factories/UserAffiliationFactory.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Institution;
|
||||
use App\Models\Lists\UserPosition;
|
||||
use App\Models\User;
|
||||
use App\Models\UserAffiliation;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<UserAffiliation>
|
||||
*/
|
||||
class UserAffiliationFactory extends Factory
|
||||
{
|
||||
protected $model = UserAffiliation::class;
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'institution_id' => Institution::factory(),
|
||||
'user_id' => User::factory(),
|
||||
'user_position_id' => UserPosition::factory(),
|
||||
'start_year' => fake()->numberBetween(1980, (int) date('Y')),
|
||||
'end_year' => null,
|
||||
'legacy_user_id' => null,
|
||||
'legacy_institution_id' => null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
}
|
||||
};
|
||||
@@ -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');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user