Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Http/Controllers/StudentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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('students', function (Blueprint $table) {
$table->renameColumn('slug', 'pea');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('students', function (Blueprint $table) {
$table->renameColumn('pea', 'slug');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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('students', function (Blueprint $table) {
$table->ulid('slug')->nullable()->after('pea');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('students', function (Blueprint $table) {
$table->dropColumn('slug');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Artisan::call('db:seed', [
'--class' => '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();
});
}
};
38 changes: 38 additions & 0 deletions database/seeders/v2_1_Seeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Database\Seeders;

use App\Student;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class v2_1_Seeder extends Seeder
{
/**
* Updates student records by chunks
*/
public function run(): void
{
Student::query()
->whereNull('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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $existing_student retrieval/update can probably be skipped, while updating the $student instance directly:

$student?->update(['slug' => Str::ulid()]);


if ($existing_student) {
$existing_student->update(['slug' => Str::ulid()]);
}
}
}
Loading