feat: introduce multi-tenancy by adding a structures table, structure_id to key models, and updating seeders for structure management.

This commit is contained in:
jeremy bayse
2026-02-21 20:38:40 +01:00
parent 6a3de5847d
commit abca346b3e
7 changed files with 122 additions and 40 deletions

View File

@@ -1,32 +0,0 @@
<?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('agents', function (Blueprint $table) {
$table->foreignId('structure_id')->nullable()->constrained()->onDelete('cascade');
});
// Rattacher les agents existants au CABM
\Illuminate\Support\Facades\DB::table('agents')->whereNull('structure_id')->update(['structure_id' => 1]);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('agents', function (Blueprint $table) {
$table->dropForeign(['structure_id']);
$table->dropColumn('structure_id');
});
}
};

View File

@@ -0,0 +1,60 @@
<?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
{
$tables = [
'users',
'integration_templates',
'services',
'integration_requests',
'comments',
'service_tasks'
];
foreach ($tables as $tableName) {
if (Schema::hasTable($tableName)) {
Schema::table($tableName, function (Blueprint $table) {
// Si la colonne n'existe pas déjà (sécurité)
if (!Schema::hasColumn($table->getTable(), 'structure_id')) {
$table->foreignId('structure_id')->nullable()->constrained()->onDelete('cascade');
}
});
}
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
$tables = [
'users',
'integration_templates',
'services',
'integration_requests',
'comments',
'service_tasks'
];
foreach ($tables as $tableName) {
if (Schema::hasTable($tableName)) {
Schema::table($tableName, function (Blueprint $table) {
if (Schema::hasColumn($table->getTable(), 'structure_id')) {
$table->dropForeign([$table->getTable() . '_structure_id_foreign']);
$table->dropColumn('structure_id');
}
});
}
}
}
};