Rename sessions to _sessions, drop legacy migrations table

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-12-08 04:27:07 +00:00
parent b5be754ef8
commit 2da7a6da08
8 changed files with 72 additions and 23 deletions

View File

@@ -88,11 +88,6 @@ class DbTableUsage_CodeQualityRule extends CodeQualityRule_Abstract
continue; continue;
} }
// Skip Laravel's sessions table - managed by framework
if ($table_name === 'sessions') {
continue;
}
// Skip framework internal tables (prefixed with underscore) // Skip framework internal tables (prefixed with underscore)
// These are low-level system tables managed directly for performance // These are low-level system tables managed directly for performance
if (str_starts_with($table_name, '_')) { if (str_starts_with($table_name, '_')) {

View File

@@ -323,7 +323,7 @@ class Migrate_Normalize_Schema_Command extends Command
*/ */
private function updateDatetimePrecision() private function updateDatetimePrecision()
{ {
$excludedTables = ['sessions', 'migrations', 'api_clients']; $excludedTables = ['_sessions', '_migrations', 'api_clients'];
$tables = DB::select('SHOW TABLES'); $tables = DB::select('SHOW TABLES');
foreach ($tables as $table) { foreach ($tables as $table) {

View File

@@ -3633,6 +3633,8 @@ class Manifest
// Commands that should skip code quality checks // Commands that should skip code quality checks
$migration_commands = [ $migration_commands = [
'migrate', 'migrate',
'migrate:begin',
'migrate:commit',
'migrate:fresh', 'migrate:fresh',
'migrate:install', 'migrate:install',
'migrate:refresh', 'migrate:refresh',
@@ -3652,26 +3654,21 @@ class Manifest
/** /**
* Verify database has been provisioned before running code quality checks * Verify database has been provisioned before running code quality checks
* *
* Checks that required framework tables (_manifest, _session) exist. * Checks that:
* If not, throws a fatal error instructing the user to run migrations. * 1. The _migrations table exists (created by Laravel migrate)
* 2. At least one migration has been applied
*
* This prevents confusing code quality errors when the real issue is * This prevents confusing code quality errors when the real issue is
* that migrations haven't been run yet. * that migrations haven't been run yet.
*/ */
protected static function __verify_database_provisioned(): void protected static function __verify_database_provisioned(): void
{ {
$required_tables = ['_manifest', '_session']; $migrations_table = config('database.migrations', 'migrations');
$missing_tables = [];
foreach ($required_tables as $table) { // Check if migrations table exists
if (!\Illuminate\Support\Facades\Schema::hasTable($table)) { if (!\Illuminate\Support\Facades\Schema::hasTable($migrations_table)) {
$missing_tables[] = $table;
}
}
if (!empty($missing_tables)) {
$tables_list = implode(', ', $missing_tables);
throw new \RuntimeException( throw new \RuntimeException(
"Database not provisioned - required tables missing: {$tables_list}\n\n" . "Database not provisioned - migrations table '{$migrations_table}' does not exist.\n\n" .
"Run migrations before continuing:\n" . "Run migrations before continuing:\n" .
" php artisan migrate\n\n" . " php artisan migrate\n\n" .
"If this is a fresh installation, you may also need to:\n" . "If this is a fresh installation, you may also need to:\n" .
@@ -3680,6 +3677,16 @@ class Manifest
" 3. Run: php artisan migrate" " 3. Run: php artisan migrate"
); );
} }
// Check if at least one migration has been applied
$result = \Illuminate\Support\Facades\DB::select("SELECT COUNT(*) as cnt FROM `{$migrations_table}`");
if ($result[0]->cnt === 0) {
throw new \RuntimeException(
"Database not provisioned - no migrations have been applied.\n\n" .
"Run migrations before continuing:\n" .
" php artisan migrate"
);
}
} }
/** /**

View File

@@ -101,7 +101,7 @@ class Session extends Rsx_System_Model_Abstract
* The table associated with the model * The table associated with the model
* @var string * @var string
*/ */
protected $table = 'sessions'; protected $table = '_sessions';
/** /**
* The attributes that should be cast * The attributes that should be cast

View File

@@ -36,7 +36,7 @@ class Session_Cleanup_Service extends Rsx_Service_Abstract
{ {
// Logged-in sessions: older than 365 days // Logged-in sessions: older than 365 days
$logged_in_cutoff = now()->subDays(365); $logged_in_cutoff = now()->subDays(365);
$logged_in_deleted = DB::table('sessions') $logged_in_deleted = DB::table('_sessions')
->whereNotNull('login_user_id') ->whereNotNull('login_user_id')
->where('last_active', '<', $logged_in_cutoff) ->where('last_active', '<', $logged_in_cutoff)
->delete(); ->delete();
@@ -45,7 +45,7 @@ class Session_Cleanup_Service extends Rsx_Service_Abstract
// Anonymous sessions: older than 14 days // Anonymous sessions: older than 14 days
$anonymous_cutoff = now()->subDays(14); $anonymous_cutoff = now()->subDays(14);
$anonymous_deleted = DB::table('sessions') $anonymous_deleted = DB::table('_sessions')
->whereNull('login_user_id') ->whereNull('login_user_id')
->where('last_active', '<', $anonymous_cutoff) ->where('last_active', '<', $anonymous_cutoff)
->delete(); ->delete();

View File

@@ -7,7 +7,7 @@ use App\RSpade\SchemaQuality\SchemaViolation;
abstract class Schema_Rule_Abstract abstract class Schema_Rule_Abstract
{ {
protected array $violations = []; protected array $violations = [];
protected array $excluded_tables = ['migrations', 'sessions']; protected array $excluded_tables = ['_migrations', '_sessions'];
/** /**
* Get the unique rule identifier * Get the unique rule identifier

View File

@@ -326,6 +326,11 @@
"created_at": "2025-11-23T16:44:39+00:00", "created_at": "2025-11-23T16:44:39+00:00",
"created_by": "root", "created_by": "root",
"command": "php artisan make:migration:safe reindex_user_roles_to_100_based" "command": "php artisan make:migration:safe reindex_user_roles_to_100_based"
},
"2025_12_08_042258_drop_migrations_and_rename_sessions_table.php": {
"created_at": "2025-12-08T04:22:58+00:00",
"created_by": "root",
"command": "php artisan make:migration:safe drop_migrations_and_rename_sessions_table"
} }
} }
} }

View File

@@ -0,0 +1,42 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Clean up legacy migrations table and rename sessions to _sessions
*
* 1. Drop 'migrations' table if it exists (should only be '_migrations')
* 2. Rename 'sessions' to '_sessions' for consistency with other system tables
*/
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Drop legacy 'migrations' table if it exists
// Laravel should only use '_migrations' as configured in database.php
if (Schema::hasTable('migrations')) {
DB::statement("DROP TABLE migrations");
}
// Rename sessions to _sessions for system table consistency
// First drop foreign key from _flash_alerts
if (Schema::hasTable('_flash_alerts')) {
DB::statement("ALTER TABLE _flash_alerts DROP FOREIGN KEY flash_alerts_session_fk");
}
// Rename the table
DB::statement("RENAME TABLE sessions TO _sessions");
// Re-add foreign key constraint
if (Schema::hasTable('_flash_alerts')) {
DB::statement("ALTER TABLE _flash_alerts ADD CONSTRAINT flash_alerts_session_fk FOREIGN KEY (session_id) REFERENCES _sessions(id) ON DELETE CASCADE");
}
}
};