-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): allow querying for user profile by ULID (#3141)
- Loading branch information
1 parent
5e93a28
commit 62672aa
Showing
7 changed files
with
147 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Community\Commands; | ||
|
||
use App\Models\User; | ||
use DB; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Str; | ||
|
||
class SyncUserUlids extends Command | ||
{ | ||
protected $signature = 'ra:sync:user-ulids'; | ||
|
||
protected $description = 'Sync user ulid field values'; | ||
|
||
public function handle(): void | ||
{ | ||
$total = User::whereNull('ulid')->count(); | ||
|
||
if ($total === 0) { | ||
$this->info('No records need ULIDs.'); | ||
|
||
return; | ||
} | ||
|
||
$progressBar = $this->output->createProgressBar($total); | ||
$progressBar->start(); | ||
|
||
User::withTrashed() | ||
->whereNull('ulid') | ||
->chunkById(4000, function ($users) use ($progressBar) { | ||
$updates = []; | ||
$milliseconds = 0; | ||
|
||
/** @var User $user */ | ||
foreach ($users as $user) { | ||
$milliseconds += rand(1, 20); | ||
$milliseconds %= 1000; | ||
|
||
$timestamp = $user->Created->clone()->addMilliseconds($milliseconds); | ||
$ulid = (string) Str::ulid($timestamp); | ||
|
||
$updates[] = [ | ||
'ID' => $user->id, | ||
'ulid' => $ulid, | ||
]; | ||
} | ||
|
||
// Perform a batch update for speed. | ||
DB::table('UserAccounts') | ||
->upsert( | ||
$updates, | ||
['ID'], | ||
['ulid'] | ||
); | ||
|
||
$progressBar->advance(count($updates)); | ||
}); | ||
|
||
$progressBar->finish(); | ||
|
||
$this->newLine(); | ||
$this->info('Done.'); | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
database/migrations/2025_01_26_000000_update_useraccounts_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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class() extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::table('UserAccounts', function (Blueprint $table) { | ||
$table->ulid('ulid')->after('ID')->unique()->nullable(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('UserAccounts', function (Blueprint $table) { | ||
$table->dropColumn('ulid'); | ||
}); | ||
} | ||
}; |
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