SchoolId change to Shorthand completed
This commit is contained in:
parent
41e52609c5
commit
f1f80bf814
@ -75,6 +75,14 @@ migrateAll = do
|
|||||||
|
|
||||||
runMigration migrateAll'
|
runMigration migrateAll'
|
||||||
|
|
||||||
|
{-
|
||||||
|
Confusion about quotes, from the PostgreSQL Manual:
|
||||||
|
Single quotes for string constants, double quotes for table/column names.
|
||||||
|
|
||||||
|
QuasiQuoter: ^{TableName} @{ColumnName} (includes Escaping);
|
||||||
|
#{anything} (no escaping);
|
||||||
|
-}
|
||||||
|
|
||||||
|
|
||||||
customMigrations :: MonadIO m => Map (Key AppliedMigration) (ReaderT SqlBackend m ())
|
customMigrations :: MonadIO m => Map (Key AppliedMigration) (ReaderT SqlBackend m ())
|
||||||
customMigrations = Map.fromListWith (>>)
|
customMigrations = Map.fromListWith (>>)
|
||||||
@ -105,47 +113,62 @@ customMigrations = Map.fromListWith (>>)
|
|||||||
_other -> return ()
|
_other -> return ()
|
||||||
)
|
)
|
||||||
, ( AppliedMigrationKey [migrationVersion|1.0.0|] [version|2.0.0|]
|
, ( AppliedMigrationKey [migrationVersion|1.0.0|] [version|2.0.0|]
|
||||||
, do -- SchoolId is the Shorthand CI Text now
|
, whenM (tableExists "school") $ do -- SchoolId is the Shorthand CI Text now
|
||||||
{-
|
|
||||||
Confusion about quotes, from the SQL Manual:
|
|
||||||
Single quotes for string constants, double quotes for table/column names.
|
|
||||||
|
|
||||||
QuasiQuoter: ^{TableName} @{ColumnName} (includes Escaping);
|
|
||||||
#{anything} (no escaping);
|
|
||||||
-}
|
|
||||||
-- Read old table into memory
|
-- Read old table into memory
|
||||||
-- schoolTable :: [(Single Int64, Single Text)]
|
|
||||||
schoolTable <- [sqlQQ| SELECT "id", "shorthand" FROM "school"; |]
|
schoolTable <- [sqlQQ| SELECT "id", "shorthand" FROM "school"; |]
|
||||||
let _sT = schoolTable :: [(Single Int64, Single (CI Text))] -- Types needed
|
let _sT = schoolTable :: [(Single Int64, Single (CI Text))] -- Types needed
|
||||||
-- Convert columns containing SchoolId
|
-- Convert columns containing SchoolId
|
||||||
let convert0 = [executeQQ|
|
whenM (tableExists "user_admin") $ do
|
||||||
ALTER TABLE "user_admin" DROP CONSTRAINT user_admin_school_fkey;
|
[executeQQ|
|
||||||
ALTER TABLE "user_lecturer" DROP CONSTRAINT user_lecturer_school_fkey;
|
ALTER TABLE "user_admin" DROP CONSTRAINT user_admin_school_fkey;
|
||||||
ALTER TABLE "course" DROP CONSTRAINT course_school_fkey;
|
ALTER TABLE "user_admin" ALTER COLUMN school TYPE citext USING school::citext;
|
||||||
|]
|
|]
|
||||||
-- let convert1 = [executeQQ|
|
forM_ schoolTable $ \(Single idnr, Single ssh) ->
|
||||||
-- ALTER TABLE "user_admin" ALTER COLUMN school TYPE citext USING CAST(school AS citext);
|
[executeQQ|
|
||||||
-- ALTER TABLE "user_lecturer" ALTER COLUMN school TYPE citext USING CAST(school AS citext);
|
UPDATE "user_admin" SET "school" = #{ssh} WHERE school = #{tshow idnr};
|
||||||
-- ALTER TABLE "course" ALTER COLUMN school TYPE citext USING CAST(school AS citext);
|
|]
|
||||||
-- |]
|
[executeQQ|
|
||||||
-- Convert Number-Strings to Shorthands
|
ALTER TABLE "user_admin" ADD CONSTRAINT "user_admin_school_fkey"
|
||||||
let convert2 = forM_ schoolTable (\(Single idnr, Single ssh) ->
|
FOREIGN KEY (school) REFERENCES school(shorthand);
|
||||||
[executeQQ|
|
|]
|
||||||
UPDATE "user_admin" SET "school" = #{ssh} WHERE school = #{idnr};
|
whenM (tableExists "user_lecturer") $ do
|
||||||
UPDATE "user_lecturer" SET "school" = #{ssh} WHERE school = #{idnr};
|
[executeQQ|
|
||||||
UPDATE "user_course" SET "school" = #{ssh} WHERE school = #{idnr};
|
ALTER TABLE "user_lecturer" DROP CONSTRAINT user_lecturer_school_fkey;
|
||||||
|]
|
ALTER TABLE "user_lecturer" ALTER COLUMN school TYPE citext USING school::citext;
|
||||||
)
|
|]
|
||||||
-- Recreate constraints
|
forM_ schoolTable $ \(Single idnr, Single ssh) ->
|
||||||
let convert3 = [executeQQ|
|
[executeQQ|
|
||||||
ALTER TABLE "user_admin" ADD CONSTRAINT "user_admin_school_fkey"
|
UPDATE "user_lecturer" SET "school" = #{ssh} WHERE school = #{tshow idnr};
|
||||||
FOREIGN KEY (school) REFERENCES school(shorthand);
|
|]
|
||||||
ALTER TABLE "user_lecturer" ADD CONSTRAINT "user_lecturer_school_fkey"
|
[executeQQ|
|
||||||
|
ALTER TABLE "user_lecturer" ADD CONSTRAINT "user_lecturer_school_fkey"
|
||||||
FOREIGN KEY (school) REFERENCES school(shorthand);;
|
FOREIGN KEY (school) REFERENCES school(shorthand);;
|
||||||
ALTER TABLE "course" ADD CONSTRAINT "course_school_fkey"
|
|]
|
||||||
|
whenM (tableExists "course") $ do
|
||||||
|
[executeQQ|
|
||||||
|
ALTER TABLE "course" DROP CONSTRAINT course_school_fkey;
|
||||||
|
ALTER TABLE "course" ALTER COLUMN school TYPE citext USING school::citext;
|
||||||
|
|]
|
||||||
|
forM_ schoolTable $ \(Single idnr, Single ssh) ->
|
||||||
|
[executeQQ|
|
||||||
|
UPDATE "course" SET "school" = #{ssh} WHERE school = #{tshow idnr};
|
||||||
|
|]
|
||||||
|
[executeQQ|
|
||||||
|
ALTER TABLE "course" ADD CONSTRAINT "course_school_fkey"
|
||||||
FOREIGN KEY (school) REFERENCES school(shorthand);
|
FOREIGN KEY (school) REFERENCES school(shorthand);
|
||||||
|]
|
|]
|
||||||
let convert = convert0 >> convert2 >> convert3
|
[executeQQ|
|
||||||
convert
|
ALTER TABLE "school" DROP COLUMN "id";
|
||||||
|
ALTER TABLE "school" ADD PRIMARY KEY (shorthand);
|
||||||
|
|]
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
tableExists :: MonadIO m => Text -> ReaderT SqlBackend m Bool
|
||||||
|
tableExists table = do
|
||||||
|
haveSchoolTable <- [sqlQQ| SELECT to_regclass(#{table}); |]
|
||||||
|
case haveSchoolTable :: [Maybe (Single Text)] of
|
||||||
|
[Just _] -> return True
|
||||||
|
_other -> return False
|
||||||
|
|||||||
Reference in New Issue
Block a user