fix: Use file-based session until database is migrated

- Prevents circular dependency where login requires session, but session requires database table created by migrations
- Fresh installs: use file session until migrations run, then switch to database session
- Upgrades: use file session during migration, then switch to database session
- Simplified: removed DDL from Load_config, migrations remain source of truth

Fixes: Sessions table missing on fresh install prevents login

Addresses CodeRabbit feedback:
- Remove duplicate session table DDL (migrations are source of truth)
- Remove MySQL-specific information_schema query
- Use proper session config cloning to avoid modifying shared config
This commit is contained in:
Ollama
2026-04-01 21:17:00 +00:00
parent 56670271d6
commit 67c93d4741

View File

@@ -4,6 +4,8 @@ namespace App\Events;
use App\Libraries\MY_Migration;
use App\Models\Appconfig;
use CodeIgniter\Session\Handlers\DatabaseHandler;
use CodeIgniter\Session\Handlers\FileHandler;
use CodeIgniter\Session\Session;
use Config\OSPOS;
use Config\Services;
@@ -28,7 +30,8 @@ class Load_config
$migration_config = config('Migrations');
$migration = new MY_Migration($migration_config);
$this->session = session();
// Use file-based session until database is migrated
$this->session = $this->createSession($migration->is_latest());
// Database Configuration
$config = config(OSPOS::class);
@@ -53,4 +56,27 @@ class Load_config
bcscale(max(2, totals_decimals() + tax_decimals()));
}
/**
* Creates session with appropriate handler.
* Uses file-based session until database is migrated, then switches to database session.
*
* This prevents a circular dependency where login requires session, but the sessions
* database table doesn't exist yet because migrations run after login.
*/
private function createSession(bool $isDbMigrated): Session
{
$sessionConfig = config('Session');
// If database is not migrated and we're configured to use database sessions,
// temporarily fall back to file-based sessions to allow migrations to complete.
// Once migrations run, subsequent requests will use database sessions.
if (!$isDbMigrated && $sessionConfig->driver === DatabaseHandler::class) {
$sessionConfig = clone $sessionConfig;
$sessionConfig->driver = FileHandler::class;
$sessionConfig->savePath = WRITEPATH . 'session';
}
return Services::session($sessionConfig);
}
}