-- Allow arbitrary level codes/labels per course (not only fixed CEFR bands).
DO $$
DECLARE
    con_name text;
BEGIN
    SELECT c.conname INTO con_name
    FROM pg_constraint c
    JOIN pg_class t ON c.conrelid = t.oid
    JOIN pg_namespace n ON t.relnamespace = n.oid
    WHERE n.nspname = current_schema()
      AND t.relname = 'levels'
      AND c.contype = 'c'
      AND pg_get_constraintdef(c.oid) LIKE '%cefr_level%IN (%A1%';
    IF con_name IS NOT NULL THEN
        EXECUTE format('ALTER TABLE levels DROP CONSTRAINT %I', con_name);
    END IF;
END $$;

ALTER TABLE levels
    ALTER COLUMN cefr_level TYPE VARCHAR(64);
