Files
rspade_system/app/RSpade/SchemaQuality/Rules/SessionIdForeignKeyRule.php
root f6fac6c4bc Fix bin/publish: copy docs.dist from project root
Fix bin/publish: use correct .env path for rspade_system
Fix bin/publish script: prevent grep exit code 1 from terminating script

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 02:08:33 +00:00

67 lines
2.4 KiB
PHP
Executable File

<?php
namespace App\RSpade\SchemaQuality\Rules;
class SessionIdForeignKeyRule extends Schema_Rule_Abstract
{
public function get_id(): string
{
return 'SCHEMA-FK-01';
}
public function get_name(): string
{
return 'Session ID Foreign Key Rule';
}
public function get_description(): string
{
return 'Ensures session_id columns have proper foreign key constraints to sessions.id with ON DELETE CASCADE';
}
public function check(array $schema): void
{
foreach ($schema['tables'] as $table_name => $table_info) {
if ($this->is_excluded_table($table_name)) {
continue;
}
// Check each column for session_id
foreach ($table_info['columns'] as $column) {
if ($column['name'] === 'session_id') {
// Check if nullable
if ($column['nullable'] !== 'YES') {
$this->add_violation(
$table_name,
'session_id',
'Column session_id must be nullable',
'ALTER TABLE ' . $table_name . ' MODIFY session_id VARCHAR(255) NULL'
);
}
// Check for foreign key
$has_proper_fk = false;
foreach ($table_info['foreign_keys'] as $fk) {
if ($fk['column'] === 'session_id' &&
$fk['referenced_table'] === 'sessions' &&
$fk['referenced_column'] === 'id' &&
$fk['delete_rule'] === 'CASCADE') {
$has_proper_fk = true;
break;
}
}
if (!$has_proper_fk) {
$this->add_violation(
$table_name,
'session_id',
'Column session_id must have foreign key to sessions.id with ON DELETE CASCADE',
'ALTER TABLE ' . $table_name . ' ADD CONSTRAINT fk_' . $table_name . '_session ' .
'FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE'
);
}
}
}
}
}
}