diff --git a/src/Model/Migration.hs b/src/Model/Migration.hs index e7ad3c9af..bd957ffe1 100644 --- a/src/Model/Migration.hs +++ b/src/Model/Migration.hs @@ -524,21 +524,31 @@ customMigrations = Map.fromListWith (>>) runConduit $ getExamEntries .| C.mapM_ renameExamParts ) , ( AppliedMigrationKey [migrationVersion|21.0.0|] [version|22.0.0|] - , whenM (tableExists "exam") $ - [executeQQ| - ALTER TABLE "exam" DROP COLUMN IF EXISTS "grading_key"; - ALTER TABLE "exam" ADD COLUMN IF NOT EXISTS "grading_rule" jsonb; + , whenM (tableExists "exam") $ do + oldVersion <- columnExists "exam" "grading_key" + if + | oldVersion -> do + -- Major changes happend to the structure of exams without appropriate + -- migration, try to remedy that here + tableDropEmpty "exam_part_corrector" + tableDropEmpty "exam_corrector" + tableDropEmpty "exam_result" + tableDropEmpty "exam_registration" + tableDropEmpty "exam_occurrence" + tableDropEmpty "exam_part" + tableDropEmpty "exam" + | otherwise -> + [executeQQ| + ALTER TABLE "exam" ALTER COLUMN "grading_rule" DROP NOT NULL; + ALTER TABLE "exam" ALTER COLUMN "bonus_rule" DROP NOT NULL; + ALTER TABLE "exam" ALTER COLUMN "occurrence_rule" DROP NOT NULL; - ALTER TABLE "exam" ALTER COLUMN "grading_rule" DROP NOT NULL; - ALTER TABLE "exam" ALTER COLUMN "bonus_rule" DROP NOT NULL; - ALTER TABLE "exam" ALTER COLUMN "occurrence_rule" DROP NOT NULL; + UPDATE "exam" SET "grading_rule" = NULL WHERE "grading_rule" = '{ "rule": "manual" }'; + UPDATE "exam" SET "bonus_rule" = NULL WHERE "bonus_rule" = '{ "rule": "no-bonus"}'; + UPDATE "exam" SET "occurrence_rule" = NULL WHERE "occurrence_rule" = '"manual"'; - UPDATE "exam" SET "grading_rule" = NULL WHERE "grading_rule" = '{ "rule": "manual" }'; - UPDATE "exam" SET "bonus_rule" = NULL WHERE "bonus_rule" = '{ "rule": "no-bonus"}'; - UPDATE "exam" SET "occurrence_rule" = NULL WHERE "occurrence_rule" = '"manual"'; - - UPDATE "exam" SET "occurrence_rule" = json_build_object('rule', "occurrence_rule"); - |] + UPDATE "exam" SET "occurrence_rule" = json_build_object('rule', "occurrence_rule"); + |] ) ] diff --git a/src/Utils.hs b/src/Utils.hs index f257bc312..ca5330ea6 100644 --- a/src/Utils.hs +++ b/src/Utils.hs @@ -979,5 +979,5 @@ clampMin, clampMax :: Ord a => a -- ^ Boundary -> a -- ^ Value -> a -- ^ Clamped Value -clampMin minVal = max minVal -clampMax maxVal = min maxVal +clampMin = max +clampMax = min diff --git a/test/Model/TypesSpec.hs b/test/Model/TypesSpec.hs index 49faef34f..c27083034 100644 --- a/test/Model/TypesSpec.hs +++ b/test/Model/TypesSpec.hs @@ -240,8 +240,7 @@ instance Arbitrary ExamGrade where instance Arbitrary ExamGradingRule where arbitrary = oneof - [ pure ExamGradingManual - , ExamGradingKey . reverse . fromOffsets . map getNonNegative <$> replicateM 11 arbitrary + [ ExamGradingKey . reverse . fromOffsets . map getNonNegative <$> replicateM 10 arbitrary ] where fromOffsets [] = []