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:
@@ -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');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -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');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user