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:
@@ -88,11 +88,6 @@ class DbTableUsage_CodeQualityRule extends CodeQualityRule_Abstract
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip Laravel's sessions table - managed by framework
|
||||
if ($table_name === 'sessions') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip framework internal tables (prefixed with underscore)
|
||||
// These are low-level system tables managed directly for performance
|
||||
if (str_starts_with($table_name, '_')) {
|
||||
|
||||
@@ -323,7 +323,7 @@ class Migrate_Normalize_Schema_Command extends Command
|
||||
*/
|
||||
private function updateDatetimePrecision()
|
||||
{
|
||||
$excludedTables = ['sessions', 'migrations', 'api_clients'];
|
||||
$excludedTables = ['_sessions', '_migrations', 'api_clients'];
|
||||
$tables = DB::select('SHOW TABLES');
|
||||
|
||||
foreach ($tables as $table) {
|
||||
|
||||
@@ -3633,6 +3633,8 @@ class Manifest
|
||||
// Commands that should skip code quality checks
|
||||
$migration_commands = [
|
||||
'migrate',
|
||||
'migrate:begin',
|
||||
'migrate:commit',
|
||||
'migrate:fresh',
|
||||
'migrate:install',
|
||||
'migrate:refresh',
|
||||
@@ -3652,26 +3654,21 @@ class Manifest
|
||||
/**
|
||||
* Verify database has been provisioned before running code quality checks
|
||||
*
|
||||
* Checks that required framework tables (_manifest, _session) exist.
|
||||
* If not, throws a fatal error instructing the user to run migrations.
|
||||
* Checks that:
|
||||
* 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
|
||||
* that migrations haven't been run yet.
|
||||
*/
|
||||
protected static function __verify_database_provisioned(): void
|
||||
{
|
||||
$required_tables = ['_manifest', '_session'];
|
||||
$missing_tables = [];
|
||||
$migrations_table = config('database.migrations', 'migrations');
|
||||
|
||||
foreach ($required_tables as $table) {
|
||||
if (!\Illuminate\Support\Facades\Schema::hasTable($table)) {
|
||||
$missing_tables[] = $table;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($missing_tables)) {
|
||||
$tables_list = implode(', ', $missing_tables);
|
||||
// Check if migrations table exists
|
||||
if (!\Illuminate\Support\Facades\Schema::hasTable($migrations_table)) {
|
||||
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" .
|
||||
" php artisan migrate\n\n" .
|
||||
"If this is a fresh installation, you may also need to:\n" .
|
||||
@@ -3680,6 +3677,16 @@ class Manifest
|
||||
" 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"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -101,7 +101,7 @@ class Session extends Rsx_System_Model_Abstract
|
||||
* The table associated with the model
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'sessions';
|
||||
protected $table = '_sessions';
|
||||
|
||||
/**
|
||||
* The attributes that should be cast
|
||||
|
||||
@@ -36,7 +36,7 @@ class Session_Cleanup_Service extends Rsx_Service_Abstract
|
||||
{
|
||||
// Logged-in sessions: older than 365 days
|
||||
$logged_in_cutoff = now()->subDays(365);
|
||||
$logged_in_deleted = DB::table('sessions')
|
||||
$logged_in_deleted = DB::table('_sessions')
|
||||
->whereNotNull('login_user_id')
|
||||
->where('last_active', '<', $logged_in_cutoff)
|
||||
->delete();
|
||||
@@ -45,7 +45,7 @@ class Session_Cleanup_Service extends Rsx_Service_Abstract
|
||||
|
||||
// Anonymous sessions: older than 14 days
|
||||
$anonymous_cutoff = now()->subDays(14);
|
||||
$anonymous_deleted = DB::table('sessions')
|
||||
$anonymous_deleted = DB::table('_sessions')
|
||||
->whereNull('login_user_id')
|
||||
->where('last_active', '<', $anonymous_cutoff)
|
||||
->delete();
|
||||
|
||||
@@ -7,7 +7,7 @@ use App\RSpade\SchemaQuality\SchemaViolation;
|
||||
abstract class Schema_Rule_Abstract
|
||||
{
|
||||
protected array $violations = [];
|
||||
protected array $excluded_tables = ['migrations', 'sessions'];
|
||||
protected array $excluded_tables = ['_migrations', '_sessions'];
|
||||
|
||||
/**
|
||||
* Get the unique rule identifier
|
||||
|
||||
@@ -326,6 +326,11 @@
|
||||
"created_at": "2025-11-23T16:44:39+00:00",
|
||||
"created_by": "root",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user