Skip to content

[Refactor] Replace integer IDs with UUIDs across the application #286

@kidiatoliny

Description

@kidiatoliny

Description

Migrate the system from using integer-based primary keys to UUIDs for all major models.
This will improve data security, URL obfuscation, and compatibility with distributed systems and future API integrations.


Expected Behavior

  • All core models (e.g., User, Hunt, Comment, Like, Notification, etc.) should use UUIDs as primary keys.
  • Relationships and foreign keys should be updated accordingly.
  • Application should continue functioning normally with existing features (auth, relationships, queries, etc.).
  • URL slugs and API responses should reflect UUIDs instead of numeric IDs.

Backend Tasks

  • Add a UUID trait to handle automatic generation:
    use Illuminate\Support\Str;
    
    protected static function booted(): void
    {
        static::creating(fn ($model) => $model->id = $model->id ?? (string) Str::uuid());
    }
	•	Update migrations:
	•	Replace $table->id(); with $table->uuid('id')->primary();
	•	Update foreign keys accordingly (e.g., $table->uuid('user_id')).
	•	Adjust model relationships to use uuid instead of integer.
	•	Ensure policies, validation rules, and route model binding handle UUIDs.
	•	Migrate existing data or create a script for ID-to-UUID transition (if needed).
	•	Add database indexing for UUID columns.

⸻

Frontend Tasks
	•	Update routes and API calls to use UUIDs.
	•	Validate all Inertia/React components referencing IDs (profile URLs, hunt pages, comment actions).
	•	Test for consistent rendering and data fetching.

⸻

Optional Enhancements
	•	Use ULID (sortable UUIDs) for better database indexing and performance.
	•	Add fallback compatibility layer for legacy IDs during transition.
	•	Create a custom cast (uuid) for clean Eloquent integration.

⸻

Notes

Switching to UUIDs provides:
	•	Stronger security by preventing predictable ID enumeration.
	•	Easier integration with microservices and distributed systems.
	•	Improved data portability between environments.

This is a breaking change, so it must be carefully rolled out with proper migration strategy and version control.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions