From 479601a8cf456c00eacccd91d83a1160c0d8ffd1 Mon Sep 17 00:00:00 2001 From: SJost Date: Fri, 24 Aug 2018 10:22:41 +0200 Subject: [PATCH] Remove/recreate constrainsts, BUT sql type conversion has a syntax error that I don't understand --- src/Model/Migration.hs | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) 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 ) ]