-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix foreign keys and deletion service
- Loading branch information
Showing
8 changed files
with
344 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
database/migrations/2024_11_04_164807_add_foreign_key_to_organizations_and_members_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
$foreignKeyProblems = DB::table('organizations') | ||
->select(['organizations.id', 'organizations.user_id']) | ||
->whereNotExists(function (Builder $query): void { | ||
$query->select('id') | ||
->from('users') | ||
->whereColumn('organizations.user_id', 'users.id'); | ||
}) | ||
->get(); | ||
foreach ($foreignKeyProblems as $foreignKeyProblem) { | ||
Log::error('Organization with ID '.$foreignKeyProblem->id.' has non-existing owner with ID '.$foreignKeyProblem->user_id); | ||
} | ||
if ($foreignKeyProblems->count() > 0) { | ||
throw new Exception('There are organizations with non-existing owners, check the logs for more information'); | ||
} | ||
Schema::table('organizations', function (Blueprint $table): void { | ||
$table->foreign('user_id') | ||
->references('id') | ||
->on('users') | ||
->onDelete('restrict') | ||
->onUpdate('cascade'); | ||
}); | ||
$foreignKeyProblems = DB::table('members') | ||
->select(['members.id', 'members.organization_id']) | ||
->whereNotExists(function (Builder $query): void { | ||
$query->select('id') | ||
->from('organizations') | ||
->whereColumn('members.organization_id', 'organizations.id'); | ||
}) | ||
->get(); | ||
foreach ($foreignKeyProblems as $foreignKeyProblem) { | ||
Log::error('Member with ID '.$foreignKeyProblem->id.' has non-existing organization with ID '.$foreignKeyProblem->organization_id); | ||
} | ||
if ($foreignKeyProblems->count() > 0) { | ||
throw new Exception('There are members with non-existing organizations, check the logs for more information'); | ||
} | ||
$foreignKeyProblems = DB::table('members') | ||
->select(['members.id', 'members.user_id']) | ||
->whereNotExists(function (Builder $query): void { | ||
$query->select('id') | ||
->from('users') | ||
->whereColumn('members.user_id', 'users.id'); | ||
}) | ||
->get(); | ||
foreach ($foreignKeyProblems as $foreignKeyProblem) { | ||
Log::error('Member with ID '.$foreignKeyProblem->id.' has non-existing user with ID '.$foreignKeyProblem->user_id); | ||
} | ||
if ($foreignKeyProblems->count() > 0) { | ||
throw new Exception('There are members with non-existing users, check the logs for more information'); | ||
} | ||
Schema::table('members', function (Blueprint $table): void { | ||
$table->foreign('organization_id') | ||
->references('id') | ||
->on('organizations') | ||
->onDelete('restrict') | ||
->onUpdate('cascade'); | ||
$table->foreign('user_id') | ||
->references('id') | ||
->on('users') | ||
->onDelete('restrict') | ||
->onUpdate('cascade'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('organizations', function (Blueprint $table): void { | ||
$table->dropForeign(['user_id']); | ||
}); | ||
Schema::table('members', function (Blueprint $table): void { | ||
$table->dropForeign(['organization_id']); | ||
$table->dropForeign(['user_id']); | ||
}); | ||
} | ||
}; |
114 changes: 114 additions & 0 deletions
114
database/migrations/2024_11_04_170614_add_foreign_keys_to_oauth_tables.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
DB::table('oauth_access_tokens') | ||
->whereNotNull('user_id') | ||
->whereNotExists(function (Builder $query): void { | ||
$query->select('id') | ||
->from('users') | ||
->whereColumn('oauth_access_tokens.user_id', 'users.id'); | ||
}) | ||
->delete(); | ||
DB::table('oauth_access_tokens') | ||
->whereNotExists(function (Builder $query): void { | ||
$query->select('id') | ||
->from('oauth_clients') | ||
->whereColumn('oauth_access_tokens.client_id', 'oauth_clients.id'); | ||
}) | ||
->delete(); | ||
Schema::table('oauth_access_tokens', function (Blueprint $table): void { | ||
$table->foreign('user_id') | ||
->references('id') | ||
->on('users') | ||
->onDelete('restrict') | ||
->onUpdate('cascade'); | ||
$table->foreign('client_id') | ||
->references('id') | ||
->on('oauth_clients') | ||
->onDelete('restrict') | ||
->onUpdate('cascade'); | ||
}); | ||
DB::table('oauth_auth_codes') | ||
->whereNotExists(function (Builder $query): void { | ||
$query->select('id') | ||
->from('users') | ||
->whereColumn('oauth_auth_codes.user_id', 'users.id'); | ||
}) | ||
->delete(); | ||
DB::table('oauth_auth_codes') | ||
->whereNotExists(function (Builder $query): void { | ||
$query->select('id') | ||
->from('oauth_clients') | ||
->whereColumn('oauth_auth_codes.client_id', 'oauth_clients.id'); | ||
}) | ||
->delete(); | ||
Schema::table('oauth_auth_codes', function (Blueprint $table): void { | ||
$table->foreign('user_id') | ||
->references('id') | ||
->on('users') | ||
->onDelete('restrict') | ||
->onUpdate('cascade'); | ||
$table->foreign('client_id') | ||
->references('id') | ||
->on('oauth_clients') | ||
->onDelete('restrict') | ||
->onUpdate('cascade'); | ||
}); | ||
DB::table('oauth_clients') | ||
->whereNotNull('user_id') | ||
->whereNotExists(function (Builder $query): void { | ||
$query->select('id') | ||
->from('users') | ||
->whereColumn('oauth_clients.user_id', 'users.id'); | ||
}) | ||
->delete(); | ||
Schema::table('oauth_clients', function (Blueprint $table): void { | ||
$table->foreign('user_id') | ||
->references('id') | ||
->on('users') | ||
->onDelete('restrict') | ||
->onUpdate('cascade'); | ||
}); | ||
Schema::table('oauth_personal_access_clients', function (Blueprint $table): void { | ||
$table->foreign('client_id') | ||
->references('id') | ||
->on('oauth_clients') | ||
->onDelete('restrict') | ||
->onUpdate('cascade'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('oauth_access_tokens', function (Blueprint $table): void { | ||
$table->dropForeign(['user_id']); | ||
$table->dropForeign(['client_id']); | ||
}); | ||
Schema::table('oauth_auth_codes', function (Blueprint $table): void { | ||
$table->dropForeign(['user_id']); | ||
$table->dropForeign(['client_id']); | ||
}); | ||
Schema::table('oauth_clients', function (Blueprint $table): void { | ||
$table->dropForeign(['user_id']); | ||
}); | ||
Schema::table('oauth_personal_access_clients', function (Blueprint $table): void { | ||
$table->dropForeign(['client_id']); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.