mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fix foreign keys.
This commit is contained in:
parent
30d155a8e2
commit
4f2e7ac0d4
@ -9,6 +9,10 @@ use Illuminate\Support\Facades\Schema;
|
||||
*/
|
||||
class UserGroups extends Migration
|
||||
{
|
||||
private array $tables
|
||||
= ['accounts', 'attachments', 'available_budgets', 'bills', 'budgets', 'categories', 'recurrences', 'rule_groups', 'rules', 'tags',
|
||||
'transaction_groups', 'transaction_journals', 'webhooks'];
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
@ -16,6 +20,20 @@ class UserGroups extends Migration
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
// remove columns from tables
|
||||
/** @var string $tableName */
|
||||
foreach ($this->tables as $tableName) {
|
||||
Schema::table(
|
||||
$tableName, function (Blueprint $table) use ($tableName) {
|
||||
|
||||
$table->dropForeign(sprintf('%s_to_ugi', $tableName));
|
||||
if (Schema::hasColumn($tableName, 'user_group_id')) {
|
||||
$table->dropColumn('user_group_id');
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Schema::table(
|
||||
'users', function (Blueprint $table) {
|
||||
|
||||
@ -85,10 +103,24 @@ class UserGroups extends Migration
|
||||
'users', function (Blueprint $table) {
|
||||
if (!Schema::hasColumn('users', 'user_group_id')) {
|
||||
$table->bigInteger('user_group_id', false, true)->nullable();
|
||||
$table->foreign('user_group_id', 'type_user_group_id')->references('id')->on('user_groups')->onDelete('set null');
|
||||
$table->foreign('user_group_id', 'type_user_group_id')->references('id')->on('user_groups')->onDelete('set null')->onUpdate('cascade');
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// ADD columns from tables
|
||||
/** @var string $tableName */
|
||||
foreach ($this->tables as $tableName) {
|
||||
Schema::table(
|
||||
$tableName, function (Blueprint $table) use ($tableName) {
|
||||
|
||||
if (!Schema::hasColumn($tableName, 'user_group_id')) {
|
||||
$table->bigInteger('user_group_id', false, true)->nullable()->after('user_id');
|
||||
$table->foreign('user_group_id', sprintf('%s_to_ugi', $tableName))->references('id')->on('user_groups')->onDelete('set null')->onUpdate('cascade');
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user