diff --git a/src/Model/Migration.hs b/src/Model/Migration.hs index d5f10dfcb..3328e0939 100644 --- a/src/Model/Migration.hs +++ b/src/Model/Migration.hs @@ -116,20 +116,36 @@ customMigrations = Map.fromListWith (>>) -- Read old table into memory -- schoolTable :: [(Single Int64, Single Text)] schoolTable <- [sqlQQ| SELECT "id", "shorthand" FROM "school"; |] - let _sT = schoolTable :: [(Single Int64, Single (CI Text))] + let _sT = schoolTable :: [(Single Int64, Single (CI Text))] -- Types needed -- Convert columns containing SchoolId - [executeQQ| - ALTER TABLE "user_admin" ALTER COLUMN "school" TYPE citext; - ALTER TABLE "user_lecturer" ALTER COLUMN "school" TYPE citext; - ALTER TABLE "course" ALTER COLUMN "school" TYPE citext; - |] + let convert0 = [executeQQ| + ALTER TABLE "user_admin" DROP CONSTRAINT user_admin_school_fkey; + ALTER TABLE "user_lecturer" DROP CONSTRAINT user_lecturer_school_fkey; + ALTER TABLE "course" DROP CONSTRAINT course_school_fkey; + |] + let convert1 = [executeQQ| + ALTER TABLE "user_admin" ALTER COLUMN "school" TYPE citext USING to_char(); + ALTER TABLE "user_lecturer" ALTER COLUMN "school" TYPE citext USING to_char(); + ALTER TABLE "course" ALTER COLUMN "school" TYPE citext USING to_char(); + |] -- Convert Number-Strings to Shorthands - forM_ schoolTable (\(Single idnr, Single ssh) -> - [executeQQ| - UPDATE "user_admin" SET "school" = '#{ssh}' WHERE "school" = '#{idnr}'; - UPDATE "user_lecturer" SET "school" = '#{ssh}' WHERE "school" = '#{idnr}'; - UPDATE "user_course" SET "school" = '#{ssh}' WHERE "school" = '#{idnr}'; - |] - ) + let convert2 = forM_ schoolTable (\(Single idnr, Single ssh) -> + [executeQQ| + UPDATE "user_admin" SET "school" = #{ssh} WHERE "school" = #{idnr}; + UPDATE "user_lecturer" SET "school" = #{ssh} WHERE "school" = #{idnr}; + UPDATE "user_course" SET "school" = #{ssh} WHERE "school" = #{idnr}; + |] + ) + -- Recreate constraints + let convert3 = [executeQQ| + ALTER TABLE "user_admin" ADD CONSTRAINT "user_admin_school_fkey" + FOREIGN KEY (school) REFERENCES school(shorthand); + ALTER TABLE "user_lecturer" ADD CONSTRAINT "user_lecturer_school_fkey" + FOREIGN KEY (school) REFERENCES school(shorthand);; + ALTER TABLE "course" ADD CONSTRAINT "course_school_fkey" + FOREIGN KEY (school) REFERENCES school(shorthand); + |] + let convert = convert0 >> convert1 >> convert2 >> convert3 + convert ) ]