Files
opensourcepos/app/Events/Load_config.php
Ollama 67c93d4741 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
2026-04-02 05:54:35 +00:00

83 lines
2.7 KiB
PHP

<?php
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;
/**
* @property my_migration migration;
* @property session session;
* @property appconfig appconfig;
* @property mixed $migration_config
* @property mixed $config
*/
class Load_config
{
public Session $session;
/**
* Loads configuration from database into App CI config and then applies those settings
*/
public function load_config(): void
{
// Migrations
$migration_config = config('Migrations');
$migration = new MY_Migration($migration_config);
// Use file-based session until database is migrated
$this->session = $this->createSession($migration->is_latest());
// Database Configuration
$config = config(OSPOS::class);
if (!$migration->is_latest()) {
$this->session->destroy();
}
// Language
$language_exists = file_exists('../app/Language/' . current_language_code());
if (current_language_code() == null || current_language() == null || !$language_exists) { // TODO: current_language() is undefined
$config->settings['language'] = 'english';
$config->settings['language_code'] = 'en';
}
$language = Services::language();
$language->setLocale($config->settings['language_code']);
// Time Zone
date_default_timezone_set($config->settings['timezone'] ?? ini_get('date.timezone'));
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);
}
}