Add polymorphic type references system for efficient integer-based storage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-12-27 23:10:30 +00:00
parent c1485ccbdb
commit 4db772b132
14 changed files with 1071 additions and 87 deletions

View File

@@ -361,6 +361,21 @@
"created_at": "2025-12-26T02:35:44+00:00",
"created_by": "root",
"command": "php artisan make:migration:safe convert_client_status_to_enum"
},
"2025_12_27_225200_create_type_refs_table.php": {
"created_at": "2025-12-27T22:52:00+00:00",
"created_by": "root",
"command": "php artisan make:migration:safe create_type_refs_table"
},
"2025_12_27_225224_convert_polymorphic_columns_to_type_refs.php": {
"created_at": "2025-12-27T22:52:24+00:00",
"created_by": "root",
"command": "php artisan make:migration:safe convert_polymorphic_columns_to_type_refs"
},
"2025_12_27_225305_convert_tasks_polymorphic_to_type_refs.php": {
"created_at": "2025-12-27T22:53:05+00:00",
"created_by": "root",
"command": "php artisan make:migration:safe convert_tasks_polymorphic_to_type_refs"
}
}
}

View File

@@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*
* IMPORTANT: Use raw MySQL queries for clarity and auditability
* DB::statement() with raw SQL
* Schema::create() with Blueprint
*
* REQUIRED: ALL tables MUST have: id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY
* No exceptions - every table needs this exact ID column (SIGNED for easier migrations)
*
* Integer types: Use BIGINT for all integers, TINYINT(1) for booleans only
* Never use unsigned - all integers should be signed
*
* Migrations must be self-contained - no Model/Service references
*
* @return void
*/
public function up()
{
// Create the _type_refs table for polymorphic type reference mapping
// Maps class names to integer IDs for efficient polymorphic column storage
DB::statement("
CREATE TABLE _type_refs (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
class_name VARCHAR(255) NOT NULL,
table_name VARCHAR(255) NULL,
created_at TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3),
updated_at TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
UNIQUE KEY unique_class_name (class_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
");
}
/**
* down() method is prohibited in RSpade framework
* Migrations should only move forward, never backward
* You may remove this comment as soon as you see it and understand.
*/
};

View File

@@ -0,0 +1,51 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*
* IMPORTANT: Use raw MySQL queries for clarity and auditability
* DB::statement("ALTER TABLE type_refs ADD COLUMN new_field VARCHAR(255)")
* Schema::table() with Blueprint
*
* Migrations must be self-contained - no Model/Service references
*
* @return void
*/
public function up()
{
// Convert polymorphic type columns from VARCHAR to BIGINT
// These columns will now store type_ref IDs instead of class name strings
// Existing data is cleared since there's no production data to migrate
// _file_attachments: Convert fileable_type from VARCHAR to BIGINT
// First drop the existing index that uses the VARCHAR column
DB::statement("ALTER TABLE _file_attachments DROP INDEX IF EXISTS idx_fileable");
// Clear existing data and modify column type
DB::statement("UPDATE _file_attachments SET fileable_type = NULL");
DB::statement("ALTER TABLE _file_attachments MODIFY fileable_type BIGINT NULL");
// Recreate index with BIGINT column
DB::statement("ALTER TABLE _file_attachments ADD INDEX idx_fileable (fileable_type, fileable_id)");
// _search_indexes: Convert indexable_type from VARCHAR to BIGINT
// First drop the existing indexes that use the VARCHAR column
DB::statement("ALTER TABLE _search_indexes DROP INDEX IF EXISTS idx_indexable");
DB::statement("ALTER TABLE _search_indexes DROP INDEX IF EXISTS unique_indexable");
// Clear existing data and modify column type
DB::statement("TRUNCATE TABLE _search_indexes");
DB::statement("ALTER TABLE _search_indexes MODIFY indexable_type BIGINT NOT NULL");
// Recreate indexes with BIGINT column
DB::statement("ALTER TABLE _search_indexes ADD INDEX idx_indexable (indexable_type, indexable_id)");
DB::statement("ALTER TABLE _search_indexes ADD UNIQUE KEY unique_indexable (indexable_type, indexable_id)");
}
/**
* down() method is prohibited in RSpade framework
* Migrations should only move forward, never backward
* You may remove this comment as soon as you see it and understand.
*/
};