feat(usernames): update unique constraint to composite with type and provider
- Remove single unique constraint on username - Add composite unique constraint on username, username_type, and provider_type - Update Filament username form validation with custom unique rule - Add descriptive validation message for duplicate usernames
This commit is contained in:
@@ -9,7 +9,9 @@ use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\ToggleButtons;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Schema;
|
||||
use Illuminate\Validation\Rules\Unique;
|
||||
|
||||
class UsernameForm
|
||||
{
|
||||
@@ -21,7 +23,19 @@ class UsernameForm
|
||||
->columnSpan(1)
|
||||
->alphaDash()
|
||||
->helperText('Email: myusername@gmail.com | Username: myusername')
|
||||
->required(),
|
||||
->required()
|
||||
->unique(
|
||||
table: 'usernames',
|
||||
ignoreRecord: true,
|
||||
modifyRuleUsing: function (Unique $rule, Get $get) {
|
||||
return $rule
|
||||
->where('username_type', $get('username_type'))
|
||||
->where('provider_type', $get('provider_type'));
|
||||
},
|
||||
)
|
||||
->validationMessages([
|
||||
'unique' => 'The username already exists for this type and provider.',
|
||||
]),
|
||||
TextInput::make('daily_mailbox_limit')
|
||||
->integer()
|
||||
->minValue(1)
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('usernames', function (Blueprint $table) {
|
||||
// Drop the existing unique constraint on 'username' column
|
||||
$table->dropUnique('usernames_username_unique');
|
||||
|
||||
// Add a composite unique constraint on username, username_type, and provider_type
|
||||
// This allows the same username with different type combinations
|
||||
$table->unique(['username', 'username_type', 'provider_type'], 'usernames_username_type_provider_unique');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('usernames', function (Blueprint $table) {
|
||||
// Drop the composite unique constraint
|
||||
$table->dropUnique('usernames_username_type_provider_unique');
|
||||
|
||||
// Restore the original single unique constraint on 'username'
|
||||
$table->unique('username', 'usernames_username_unique');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user