connect(); $columns = $db->query('SHOW COLUMNS FROM users')->fetchAll(PDO::FETCH_COLUMN); foreach (['user_password_hash', 'user_first_name', 'user_last_name', 'user_status', 'user_last_login'] as $column) { if (!in_array($column, $columns, true)) throw new RuntimeException("Missing users.{$column}. Align the database with tables/users.php before migrating; existing account data must be preserved."); } $sql = file_get_contents(__DIR__ . '/../migrations/001_auth.sql'); if (in_array('user_auth_version', $columns, true)) { $sql = str_replace('ALTER TABLE users ADD COLUMN user_auth_version INT UNSIGNED NOT NULL DEFAULT 0;', '', $sql); } foreach (explode(';', $sql) as $statement) { if (trim($statement)) $db->exec($statement); } // Refresh the view so users.* includes the new session version column. require __DIR__ . '/../views/view_users.php'; $db->exec(str_replace('create view', 'CREATE OR REPLACE VIEW', $SQL)); echo "Authentication schema is ready.\n";