2016-09-01 01:51:02 +09:00
|
|
|
{- This file is part of Vervis.
|
|
|
|
-
|
2018-03-04 06:33:59 +09:00
|
|
|
- Written in 2016, 2018 by fr33domlover <fr33domlover@riseup.net>.
|
2016-09-01 01:51:02 +09:00
|
|
|
-
|
|
|
|
- ♡ Copying is an act of love. Please copy, reuse and share.
|
|
|
|
-
|
|
|
|
- The author(s) have dedicated all copyright and related and neighboring
|
|
|
|
- rights to this software to the public domain worldwide. This software is
|
|
|
|
- distributed without any warranty.
|
|
|
|
-
|
|
|
|
- You should have received a copy of the CC0 Public Domain Dedication along
|
|
|
|
- with this software. If not, see
|
|
|
|
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Vervis.Migration
|
|
|
|
( migrateDB
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
2018-03-27 23:28:56 +09:00
|
|
|
import Control.Monad (unless)
|
2016-09-01 01:51:02 +09:00
|
|
|
import Control.Monad.IO.Class (MonadIO)
|
2018-03-27 23:28:56 +09:00
|
|
|
import Control.Monad.Trans.Class (lift)
|
2016-09-01 01:51:02 +09:00
|
|
|
import Control.Monad.Trans.Reader (ReaderT, runReaderT)
|
2018-03-04 06:33:59 +09:00
|
|
|
import Data.ByteString (ByteString)
|
2018-03-27 23:28:56 +09:00
|
|
|
import Data.Foldable (traverse_, for_)
|
|
|
|
import Data.Maybe (fromMaybe, listToMaybe)
|
2018-02-27 12:03:24 +09:00
|
|
|
import Data.Proxy
|
|
|
|
import Data.Text (Text)
|
2016-09-01 01:51:02 +09:00
|
|
|
import Database.Persist
|
2018-03-27 23:28:56 +09:00
|
|
|
import Database.Persist.Migration
|
2016-09-01 01:51:02 +09:00
|
|
|
import Database.Persist.Schema
|
|
|
|
import Database.Persist.Schema.PostgreSQL (schemaBackend)
|
2018-03-27 23:28:56 +09:00
|
|
|
import Database.Persist.Sql (SqlBackend, toSqlKey)
|
|
|
|
import Web.PathPieces (toPathPiece)
|
2016-09-01 01:51:02 +09:00
|
|
|
|
2018-03-27 23:28:56 +09:00
|
|
|
import Vervis.Migration.Model
|
2016-09-01 01:51:02 +09:00
|
|
|
import Vervis.Model
|
2018-03-27 23:28:56 +09:00
|
|
|
import Vervis.Model.Ident
|
|
|
|
import Vervis.Model.Workflow
|
2016-09-01 01:51:02 +09:00
|
|
|
|
|
|
|
changes :: MonadIO m => [SchemaT SqlBackend m ()]
|
|
|
|
changes =
|
2018-03-27 23:28:56 +09:00
|
|
|
[ -- 1
|
|
|
|
traverse_ addEntity model_2016_08_04
|
|
|
|
-- 2
|
|
|
|
, unsetFieldDefault "Sharer" "created"
|
|
|
|
-- 3
|
|
|
|
, unsetFieldDefault "Project" "nextTicket"
|
|
|
|
-- 4
|
|
|
|
, unsetFieldDefault "Repo" "vcs"
|
|
|
|
-- 5
|
|
|
|
, unsetFieldDefault "Repo" "mainBranch"
|
|
|
|
-- 6
|
|
|
|
, removeField "Ticket" "done"
|
|
|
|
-- 7
|
|
|
|
, addField "Ticket" (Just "'TSNew'") $ Field
|
|
|
|
"status"
|
|
|
|
(FTPrim $ backendDataType (Proxy :: Proxy Text))
|
|
|
|
FieldRequired
|
|
|
|
-- 8
|
|
|
|
, traverse_ addEntity model_2016_09_01_just_workflow
|
|
|
|
-- 9
|
|
|
|
, traverse_ addEntity model_2016_09_01_rest
|
|
|
|
-- 10
|
|
|
|
, do
|
|
|
|
let key = fromBackendKey defaultBackendKey :: Key Workflow2016
|
|
|
|
noProjects <-
|
|
|
|
lift $ null <$> selectKeysList [] [LimitTo 1 :: SelectOpt Project]
|
|
|
|
unless noProjects $ lift $ do
|
|
|
|
msid <- listToMaybe <$> selectKeysList [] [Asc SharerId, LimitTo 1]
|
|
|
|
for_ msid $ \ sid -> do
|
|
|
|
let ident = text2wfl "dummy"
|
|
|
|
w = Workflow2016 sid ident Nothing Nothing WSPublic
|
|
|
|
insertKey key w
|
|
|
|
addField "Project" (Just $ toPathPiece key) $ Field
|
|
|
|
"workflow"
|
|
|
|
(FTRef "Workflow")
|
|
|
|
FieldRequired
|
|
|
|
-- 11
|
|
|
|
, addField "Workflow"
|
2016-09-02 02:40:02 +09:00
|
|
|
(Just "'WSSharer'")
|
2018-02-27 12:03:24 +09:00
|
|
|
(Field
|
|
|
|
"scope"
|
|
|
|
(FTPrim $ backendDataType (Proxy :: Proxy Text))
|
|
|
|
FieldRequired
|
|
|
|
)
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 12
|
2018-03-04 06:33:59 +09:00
|
|
|
, changeFieldType "Person" "hash" $
|
|
|
|
backendDataType (Proxy :: Proxy ByteString)
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 13
|
2018-03-04 06:33:59 +09:00
|
|
|
, unsetFieldMaybe "Person" "email" "'no@email'"
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 14
|
2018-03-04 06:33:59 +09:00
|
|
|
, addField "Person" (Just "TRUE") Field
|
|
|
|
{ fieldName = "verified"
|
|
|
|
, fieldType = FTPrim $ backendDataType (Proxy :: Proxy Bool)
|
|
|
|
, fieldMaybe = FieldRequired
|
|
|
|
}
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 15
|
2018-03-04 06:33:59 +09:00
|
|
|
, addField "Person" (Just "''") Field
|
|
|
|
{ fieldName = "verifiedKey"
|
|
|
|
, fieldType = FTPrim $ backendDataType (Proxy :: Proxy Text)
|
|
|
|
, fieldMaybe = FieldRequired
|
|
|
|
}
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 16
|
2018-03-04 06:33:59 +09:00
|
|
|
, addField "Person" (Just "''") Field
|
|
|
|
{ fieldName = "resetPassphraseKey"
|
|
|
|
, fieldType = FTPrim $ backendDataType (Proxy :: Proxy Text)
|
|
|
|
, fieldMaybe = FieldRequired
|
|
|
|
}
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 17
|
2018-03-04 06:33:59 +09:00
|
|
|
, renameField "Person" "hash" "passphraseHash"
|
2016-09-01 01:51:02 +09:00
|
|
|
]
|
|
|
|
|
|
|
|
migrateDB :: MonadIO m => ReaderT SqlBackend m ()
|
|
|
|
migrateDB = runMigrations schemaBackend changes
|