ALTER TABLE wxgzh_ebook_chapters
ADD COLUMN IF NOT EXISTS access_level TEXT NOT NULL DEFAULT 'free';

ALTER TABLE wxgzh_course_chapters
ADD COLUMN IF NOT EXISTS access_level TEXT NOT NULL DEFAULT 'free';

UPDATE wxgzh_ebook_chapters
SET access_level = CASE
  WHEN is_free THEN 'free'
  ELSE 'vip'
END
WHERE access_level IS NULL OR access_level = 'free';

UPDATE wxgzh_course_chapters
SET access_level = CASE
  WHEN is_free THEN 'free'
  ELSE 'vip'
END
WHERE access_level IS NULL OR access_level = 'free';

ALTER TABLE wxgzh_ebook_chapters
DROP CONSTRAINT IF EXISTS wxgzh_ebook_chapters_access_level_check;

ALTER TABLE wxgzh_course_chapters
DROP CONSTRAINT IF EXISTS wxgzh_course_chapters_access_level_check;

ALTER TABLE wxgzh_ebook_chapters
ADD CONSTRAINT wxgzh_ebook_chapters_access_level_check
CHECK (access_level IN ('free', 'vip', 'svip'));

ALTER TABLE wxgzh_course_chapters
ADD CONSTRAINT wxgzh_course_chapters_access_level_check
CHECK (access_level IN ('free', 'vip', 'svip'));
