-- 会话 + 设备元数据（设备 & 会话管理 P2）。见 sqlite/000016 注释。
CREATE TABLE sessions (
  id             BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  user_id        BIGINT UNSIGNED NOT NULL,
  device_id      BIGINT UNSIGNED NOT NULL,
  refresh_jti    VARCHAR(64) NOT NULL,
  client_ip      VARCHAR(64) NULL,
  client_version VARCHAR(64) NULL,
  created_at     DATETIME(6) NOT NULL,
  last_active    DATETIME(6) NULL,
  revoked_at     DATETIME(6) NULL,
  UNIQUE KEY uk_sessions_jti (refresh_jti),
  INDEX idx_sessions_user (user_id),
  INDEX idx_sessions_device (device_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE devices ADD COLUMN client_version VARCHAR(64) NULL;
ALTER TABLE devices ADD COLUMN totp_trusted_until DATETIME(6) NULL;
