From b764883aac4e3dd1f10e5923e0a33238cdb858ec Mon Sep 17 00:00:00 2001 From: Betsy Castro <5490820+betsyecastro@users.noreply.github.com> Date: Mon, 24 Mar 2025 09:15:36 -0500 Subject: [PATCH 1/2] Replace the student table slug value with a ULID --- app/Http/Controllers/StudentsController.php | 3 +- database/seeders/v2_1_Seeder.php | 38 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 database/seeders/v2_1_Seeder.php diff --git a/app/Http/Controllers/StudentsController.php b/app/Http/Controllers/StudentsController.php index 264945e2..94a1ff8c 100644 --- a/app/Http/Controllers/StudentsController.php +++ b/app/Http/Controllers/StudentsController.php @@ -13,6 +13,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; use Illuminate\View\View; +use Illuminate\Support\Str; class StudentsController extends Controller { @@ -71,7 +72,7 @@ public function store(Request $request): Student|false $user = $request->user(); $student = $user->studentProfiles()->create([ - 'slug' => $user->pea, + 'slug' => Str::ulid(), 'full_name' => $user->display_name, 'first_name' => $user->firstname, 'last_name' => $user->lastname, diff --git a/database/seeders/v2_1_Seeder.php b/database/seeders/v2_1_Seeder.php new file mode 100644 index 00000000..8bfaa656 --- /dev/null +++ b/database/seeders/v2_1_Seeder.php @@ -0,0 +1,38 @@ +whereNotNull('slug') + ->orderBy('id') + ->chunkById(200, function ($students) { + $this->command->withProgressBar($students, fn($student) => $this->updateStudent($student)); + $this->command->newLine(); + }); + } + + /** + * Replaces a student slug with a unique ULID + */ + public function updateStudent(Student $student): void + { + $existing_student = DB::table('students')->where('id', $student->id) ?? null; + + if ($existing_student) { + $existing_student->update(['slug' => Str::ulid()]); + } + } +} From da6929a5c02bf0dc580b71883c9c31c7af61d011 Mon Sep 17 00:00:00 2001 From: Betsy Castro <5490820+betsyecastro@users.noreply.github.com> Date: Wed, 2 Apr 2025 11:02:47 -0500 Subject: [PATCH 2/2] Renames the 'slug' column to 'pea' and adds a new 'slug' column for a ULID in the students table --- ...8_rename_slug_to_pea_in_students_table.php | 28 +++++++++++++++ ...3_31_164643_add_slug_to_students_table.php | 28 +++++++++++++++ ...ke_slug_not_nullable_in_students_table.php | 34 +++++++++++++++++++ database/seeders/v2_1_Seeder.php | 2 +- 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2025_03_31_163648_rename_slug_to_pea_in_students_table.php create mode 100644 database/migrations/2025_03_31_164643_add_slug_to_students_table.php create mode 100644 database/migrations/2025_04_01_150237_make_slug_not_nullable_in_students_table.php diff --git a/database/migrations/2025_03_31_163648_rename_slug_to_pea_in_students_table.php b/database/migrations/2025_03_31_163648_rename_slug_to_pea_in_students_table.php new file mode 100644 index 00000000..7e3f4e69 --- /dev/null +++ b/database/migrations/2025_03_31_163648_rename_slug_to_pea_in_students_table.php @@ -0,0 +1,28 @@ +renameColumn('slug', 'pea'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('students', function (Blueprint $table) { + $table->renameColumn('pea', 'slug'); + }); + } +}; diff --git a/database/migrations/2025_03_31_164643_add_slug_to_students_table.php b/database/migrations/2025_03_31_164643_add_slug_to_students_table.php new file mode 100644 index 00000000..53bd2e56 --- /dev/null +++ b/database/migrations/2025_03_31_164643_add_slug_to_students_table.php @@ -0,0 +1,28 @@ +ulid('slug')->nullable()->after('pea'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('students', function (Blueprint $table) { + $table->dropColumn('slug'); + }); + } +}; diff --git a/database/migrations/2025_04_01_150237_make_slug_not_nullable_in_students_table.php b/database/migrations/2025_04_01_150237_make_slug_not_nullable_in_students_table.php new file mode 100644 index 00000000..99437841 --- /dev/null +++ b/database/migrations/2025_04_01_150237_make_slug_not_nullable_in_students_table.php @@ -0,0 +1,34 @@ + 'v2_1_Seeder', + '--force' => true + ]); + + Schema::table('students', function (Blueprint $table) { + $table->ulid('slug')->nullable(false)->change(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('students', function (Blueprint $table) { + $table->ulid('slug')->nullable()->change(); + }); + } +}; diff --git a/database/seeders/v2_1_Seeder.php b/database/seeders/v2_1_Seeder.php index 8bfaa656..a0aa2c4c 100644 --- a/database/seeders/v2_1_Seeder.php +++ b/database/seeders/v2_1_Seeder.php @@ -16,7 +16,7 @@ class v2_1_Seeder extends Seeder public function run(): void { Student::query() - ->whereNotNull('slug') + ->whereNull('slug') ->orderBy('id') ->chunkById(200, function ($students) { $this->command->withProgressBar($students, fn($student) => $this->updateStudent($student));