-- Migration 001: Add missing columns to routers table
-- Date: 2025-11-21
-- Description: Adds api_port and last_sync columns if missing

-- Add api_port column if not exists
ALTER TABLE routers 
ADD COLUMN IF NOT EXISTS api_port integer NOT NULL DEFAULT 8728;

-- Add last_sync column if not exists
ALTER TABLE routers 
ADD COLUMN IF NOT EXISTS last_sync timestamp;

-- Add created_at if missing (fallback for older schemas)
ALTER TABLE routers 
ADD COLUMN IF NOT EXISTS created_at timestamp DEFAULT now() NOT NULL;

-- Verify columns exist
DO $$
BEGIN
  IF EXISTS (
    SELECT 1 FROM information_schema.columns 
    WHERE table_name = 'routers' 
    AND column_name = 'api_port'
  ) THEN
    RAISE NOTICE 'Column api_port exists';
  END IF;
  
  IF EXISTS (
    SELECT 1 FROM information_schema.columns 
    WHERE table_name = 'routers' 
    AND column_name = 'last_sync'
  ) THEN
    RAISE NOTICE 'Column last_sync exists';
  END IF;
END $$;
