2024-07-03 12:02:11 +00:00
|
|
|
<?php
|
|
|
|
/*
|
2024-07-06 16:56:09 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
2024-07-03 12:02:11 +00:00
|
|
|
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
|
|
|
|
*/
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
use Illuminate\Database\Migrations\Migration;
|
2024-07-08 10:59:47 +00:00
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput;
|
2024-07-10 15:26:43 +00:00
|
|
|
use Modules\MMFRestrictedCustomers\Entities\Customer;
|
2024-07-03 12:02:11 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
2024-07-08 10:44:40 +00:00
|
|
|
// Link Customers to Mailboxes, through their Conversation information.
|
2024-07-08 10:59:47 +00:00
|
|
|
$output = new ConsoleOutput();
|
|
|
|
$output->writeln('<info>Linking existing Customers to Mailboxes, it might take a couple minutes…</info>');
|
2024-07-08 10:44:40 +00:00
|
|
|
$customers = Customer::all();
|
|
|
|
$customers->each(function ($customer) {
|
|
|
|
$customer->linkToMailboxThroughConversations();
|
|
|
|
});
|
2024-07-08 10:59:47 +00:00
|
|
|
$output->writeln('<info>Linking existing Customers to Mailboxes done.</info>');
|
2024-07-03 12:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|