-- =============================================
-- ADD INACTIVITY TIMEOUT COLUMN TO DB_STORE
-- =============================================
-- This script adds the inactivity_timeout column to the db_store table
-- for existing installations that don't have this column yet.
-- 
-- Run this script if you're updating an existing installation.
-- Default value: 600 seconds (10 minutes)

-- Check if column exists before adding (to avoid errors on re-run)
SET @dbname = DATABASE();
SET @tablename = "db_store";
SET @columnname = "inactivity_timeout";
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (COLUMN_NAME = @columnname)
  ) > 0,
  "SELECT 'Column already exists.' AS result;",
  CONCAT("ALTER TABLE ", @tablename, " ADD COLUMN ", @columnname, " int(11) DEFAULT 600 COMMENT 'Inactivity timeout in seconds (default 10 minutes = 600 seconds)';")
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- Update existing records to have the default value if they're NULL
UPDATE db_store SET inactivity_timeout = 600 WHERE inactivity_timeout IS NULL;

-- Verify the column was added
SELECT 'Inactivity timeout column added successfully!' AS result;
