freescout-restricted-customers/database/migrations/2024_07_03_135202_add_mailbox_id_column_to_customers_table.php

50 lines
1.4 KiB
PHP
Raw Normal View History

<?php
/*
SPDX-License-Identifier: AGPL
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
*/
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use MillionsMissingFrance\FreescoutRestrictedCustomers\Customer;
class AddMailboxIdColumnToCustomersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::table('customers', function (Blueprint $table) {
// Add a "mailbox_id" field to the customers table, linking each customer entry to a specific mailbox.
$table
->integer('mailbox_id')
->unsigned()
// The column is nullable because entries without a linked mailbox might already exist.
->nullable();
$table
->foreign('mailbox_id')
->references('id')
->on('mailboxes')
// On mailbox deletion, delete all customer entries that are linked to it.
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::table('customers', function (Blueprint $table) {
// Delete all entries from the customers table that are linked to a specific mailbox.
Customer::has('mailbox')->delete();
// Delete the extra "mailbox_id" field from the customers table.
$table->dropForeign(['mailbox_id']);
$table->dropColumn('mailbox_id');
});
}
}