From 6084247248e61c1227dc144eb04a3ce271f6ec1e Mon Sep 17 00:00:00 2001 From: Antoine Le Gonidec Date: Tue, 16 Jul 2024 12:04:08 +0200 Subject: [PATCH] Update the database schema --- ...6_115310_create_customers_groups_table.php | 42 ++++++++++++++++ ..._07_16_115333_link_customers_to_groups.php | 49 +++++++++++++++++++ .../MMFCustomersGroupsServiceProvider.php | 1 + 3 files changed, 92 insertions(+) create mode 100644 Database/Migrations/2024_07_16_115310_create_customers_groups_table.php create mode 100644 Database/Migrations/2024_07_16_115333_link_customers_to_groups.php diff --git a/Database/Migrations/2024_07_16_115310_create_customers_groups_table.php b/Database/Migrations/2024_07_16_115310_create_customers_groups_table.php new file mode 100644 index 0000000..d02cae1 --- /dev/null +++ b/Database/Migrations/2024_07_16_115310_create_customers_groups_table.php @@ -0,0 +1,42 @@ + + */ + +use Illuminate\Support\Facades\Schema; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class CreateCustomersGroupsTable extends Migration { + /** + * Run the migrations. + * + * @return void + */ + public function up() { + Schema::create('customers_groups', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + // Include a "mailbox_id" column in the customers groups table, linking each group entry to a specific mailbox. + $table + ->integer('mailbox_id') + ->unsigned(); + $table + ->foreign('mailbox_id') + ->references('id') + ->on('mailboxes') + // On mailbox deletion, delete all customers group entries that are linked to it. + ->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() { + Schema::dropIfExists('customers_groups'); + } +} diff --git a/Database/Migrations/2024_07_16_115333_link_customers_to_groups.php b/Database/Migrations/2024_07_16_115333_link_customers_to_groups.php new file mode 100644 index 0000000..3c29c78 --- /dev/null +++ b/Database/Migrations/2024_07_16_115333_link_customers_to_groups.php @@ -0,0 +1,49 @@ + + */ + +use Illuminate\Support\Facades\Schema; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class LinkCustomersToGroups extends Migration { + /** + * Run the migrations. + * + * @return void + */ + public function up() { + Schema::create('customers_group_customers', function (Blueprint $table) { + // Set a link between customers groups and customers. + $table + ->integer('customers_group_id') + ->unsigned(); + $table + ->foreign('customers_group_id') + ->references('id') + ->on('customers_groups') + ->onDelete('cascade'); + $table + ->integer('customer_id') + ->unsigned(); + $table + ->foreign('customer_id') + ->references('id') + ->on('customers') + ->onDelete('cascade'); + // Prevent the inclusion of duplicated entries. + $table->unique(['customers_group_id', 'customer_id']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() { + Schema::dropIfExists('customers_group_customers'); + } +} diff --git a/Providers/MMFCustomersGroupsServiceProvider.php b/Providers/MMFCustomersGroupsServiceProvider.php index 66492eb..43c30c0 100644 --- a/Providers/MMFCustomersGroupsServiceProvider.php +++ b/Providers/MMFCustomersGroupsServiceProvider.php @@ -21,6 +21,7 @@ class MMFCustomersGroupsServiceProvider extends ServiceProvider { */ public function boot() { $this->registerConfig(); + $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); } /**