2016-09-01 01:51:02 +09:00
|
|
|
{- This file is part of Vervis.
|
|
|
|
-
|
2019-01-26 21:56:15 +09:00
|
|
|
- Written in 2016, 2018, 2019 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-04-05 09:04:39 +09:00
|
|
|
import Data.Default.Class
|
|
|
|
import Data.Default.Instances.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)
|
2018-04-05 09:04:39 +09:00
|
|
|
import Data.Text.Encoding (encodeUtf8)
|
2018-04-03 10:20:24 +09:00
|
|
|
import Data.Time.Calendar (Day (..))
|
|
|
|
import Data.Time.Clock (UTCTime (..))
|
2016-09-01 01:51:02 +09:00
|
|
|
import Database.Persist
|
2018-04-05 09:04:39 +09:00
|
|
|
import Database.Persist.BackendDataType (backendDataType, PersistDefault (..))
|
2018-03-27 23:28:56 +09:00
|
|
|
import Database.Persist.Migration
|
2018-04-01 04:22:37 +09:00
|
|
|
import Database.Persist.Schema (SchemaT, Migration)
|
2019-03-20 17:07:37 +09:00
|
|
|
import Database.Persist.Schema.Types hiding (Entity)
|
2016-09-01 01:51:02 +09:00
|
|
|
import Database.Persist.Schema.PostgreSQL (schemaBackend)
|
2018-03-27 23:28:56 +09:00
|
|
|
import Database.Persist.Sql (SqlBackend, toSqlKey)
|
2018-04-05 09:04:39 +09:00
|
|
|
--import Text.Email.QuasiQuotation (email
|
|
|
|
import Text.Email.Validate (unsafeEmailAddress)
|
2018-03-27 23:28:56 +09:00
|
|
|
import Web.PathPieces (toPathPiece)
|
2016-09-01 01:51:02 +09:00
|
|
|
|
2019-01-30 07:24:32 +09:00
|
|
|
import qualified Database.Esqueleto as E
|
2018-04-01 04:22:37 +09:00
|
|
|
import qualified Database.Persist.Schema as U (addEntity, unsetFieldDefault)
|
|
|
|
|
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
|
|
|
|
2018-04-05 09:04:39 +09:00
|
|
|
instance PersistDefault ByteString where
|
|
|
|
pdef = def
|
|
|
|
|
2018-04-01 04:22:37 +09:00
|
|
|
type Apply m = SchemaT SqlBackend m ()
|
|
|
|
type Mig m = Migration SqlBackend m
|
|
|
|
|
2018-04-03 10:20:24 +09:00
|
|
|
defaultTime :: UTCTime
|
|
|
|
defaultTime = UTCTime (ModifiedJulianDay 0) 0
|
|
|
|
|
2018-04-01 04:22:37 +09:00
|
|
|
withPrepare :: Monad m => Mig m -> Apply m -> Mig m
|
|
|
|
withPrepare (validate, apply) prepare = (validate, prepare >> apply)
|
|
|
|
|
|
|
|
changes :: MonadIO m => [Mig m]
|
2016-09-01 01:51:02 +09:00
|
|
|
changes =
|
2018-03-27 23:28:56 +09:00
|
|
|
[ -- 1
|
2018-04-05 09:04:39 +09:00
|
|
|
addEntities model_2016_08_04
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 2
|
2018-04-01 04:22:37 +09:00
|
|
|
, unchecked $ U.unsetFieldDefault "Sharer" "created"
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 3
|
2018-04-01 04:22:37 +09:00
|
|
|
, unchecked $ U.unsetFieldDefault "Project" "nextTicket"
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 4
|
2018-04-01 04:22:37 +09:00
|
|
|
, unchecked $ U.unsetFieldDefault "Repo" "vcs"
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 5
|
2018-04-01 04:22:37 +09:00
|
|
|
, unchecked $ U.unsetFieldDefault "Repo" "mainBranch"
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 6
|
|
|
|
, removeField "Ticket" "done"
|
|
|
|
-- 7
|
2018-04-03 10:20:24 +09:00
|
|
|
, addFieldPrimRequired "Ticket" ("TSNew" :: Text) "status"
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 8
|
2018-04-05 09:04:39 +09:00
|
|
|
, addEntities model_2016_09_01_just_workflow
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 9
|
2018-04-05 09:04:39 +09:00
|
|
|
, addEntities model_2016_09_01_rest
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 10
|
2018-04-01 04:22:37 +09:00
|
|
|
, let key = fromBackendKey defaultBackendKey :: Key Workflow2016
|
|
|
|
in withPrepare
|
2018-04-03 10:20:24 +09:00
|
|
|
(addFieldRefRequired "Project"
|
|
|
|
(toBackendKey key)
|
2018-04-01 04:22:37 +09:00
|
|
|
"workflow"
|
2018-04-03 10:20:24 +09:00
|
|
|
"Workflow"
|
2018-04-01 04:22:37 +09:00
|
|
|
) $ do
|
|
|
|
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"
|
2018-04-05 09:04:39 +09:00
|
|
|
w = Workflow2016 sid ident Nothing Nothing
|
2018-04-01 04:22:37 +09:00
|
|
|
insertKey key w
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 11
|
2018-04-03 10:20:24 +09:00
|
|
|
, addFieldPrimRequired "Workflow" ("WSSharer" :: Text) "scope"
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 12
|
2018-04-05 09:04:39 +09:00
|
|
|
, unsetFieldPrimMaybe "Person" "hash" ("" :: Text)
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 13
|
2018-04-05 09:04:39 +09:00
|
|
|
, changeFieldTypePrimRequiredFreeHs "Person" "hash" encodeUtf8
|
|
|
|
-- 14
|
2018-04-03 10:20:24 +09:00
|
|
|
--, unsetFieldPrimMaybe "Person" "email" [email|noreply@no.such.email|]
|
|
|
|
, unsetFieldPrimMaybe "Person" "email" $
|
|
|
|
unsafeEmailAddress "noreply" "no.such.email"
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 15
|
2018-04-05 09:04:39 +09:00
|
|
|
, addFieldPrimRequired "Person" True "verified"
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 16
|
2018-04-05 09:04:39 +09:00
|
|
|
, addFieldPrimRequired "Person" ("" :: Text) "verifiedKey"
|
2018-03-27 23:28:56 +09:00
|
|
|
-- 17
|
2018-04-05 09:04:39 +09:00
|
|
|
, addFieldPrimRequired "Person" ("" :: Text) "resetPassphraseKey"
|
2018-04-01 12:02:35 +09:00
|
|
|
-- 18
|
2018-04-05 09:04:39 +09:00
|
|
|
, renameField "Person" "hash" "passphraseHash"
|
2018-04-01 12:02:35 +09:00
|
|
|
-- 19
|
2018-04-05 09:04:39 +09:00
|
|
|
, renameField "Person" "resetPassphraseKey" "resetPassKey"
|
2018-04-01 12:02:35 +09:00
|
|
|
-- 20
|
2018-04-05 09:04:39 +09:00
|
|
|
, addFieldPrimRequired "Person" defaultTime "verifiedKeyCreated"
|
|
|
|
-- 21
|
2018-04-03 10:20:24 +09:00
|
|
|
, addFieldPrimRequired "Person" defaultTime "resetPassKeyCreated"
|
2018-04-11 20:09:42 +09:00
|
|
|
-- 22
|
|
|
|
, addUnique "Person" $ Unique "UniquePersonEmail" ["email"]
|
2019-01-26 21:56:15 +09:00
|
|
|
-- 23
|
|
|
|
, renameField "ProjectCollabAnon" "repo" "project"
|
|
|
|
-- 24
|
|
|
|
, renameField "ProjectCollabUser" "repo" "project"
|
2019-01-27 08:39:13 +09:00
|
|
|
-- 25
|
|
|
|
, addFieldPrimRequired "Person" ("" :: Text) "about"
|
2019-01-28 23:43:07 +09:00
|
|
|
-- 26
|
|
|
|
, setFieldMaybe "ProjectCollab" "role"
|
2019-01-30 07:24:32 +09:00
|
|
|
-- 27
|
|
|
|
, removeField "RepoCollab" "role"
|
|
|
|
-- 28
|
|
|
|
, addFieldRefOptional "RepoCollab" Nothing "role" "ProjectRole"
|
|
|
|
-- 29
|
|
|
|
, removeEntity "RepoCollabAnon"
|
|
|
|
-- 30
|
|
|
|
, removeEntity "RepoCollabUser"
|
|
|
|
-- 31
|
|
|
|
, addFieldRefOptional "Repo" Nothing "collabUser" "ProjectRole"
|
|
|
|
-- 32
|
|
|
|
, addFieldRefOptional "Repo" Nothing "collabAnon" "ProjectRole"
|
|
|
|
-- 33
|
|
|
|
, addFieldRefOptional "Project" Nothing "collabUser" "ProjectRole"
|
|
|
|
-- 34
|
|
|
|
, addFieldRefOptional "Project" Nothing "collabAnon" "ProjectRole"
|
|
|
|
-- 35
|
|
|
|
, unchecked $ lift $ do
|
|
|
|
l <- E.select $ E.from $ \ (j `E.LeftOuterJoin`
|
2019-02-12 20:53:24 +09:00
|
|
|
jcu `E.LeftOuterJoin`
|
|
|
|
jca) -> do
|
2019-01-30 07:24:32 +09:00
|
|
|
E.on $
|
|
|
|
E.just (j E.^. Project2018Id) E.==.
|
|
|
|
jca E.?. ProjectCollabAnon2018Project
|
|
|
|
E.on $
|
|
|
|
E.just (j E.^. Project2018Id) E.==.
|
|
|
|
jcu E.?. ProjectCollabUser2018Project
|
|
|
|
E.where_ $ E.not_ $
|
|
|
|
E.isNothing (jcu E.?. ProjectCollabUser2018Project) E.&&.
|
|
|
|
E.isNothing (jca E.?. ProjectCollabAnon2018Project)
|
|
|
|
return
|
|
|
|
( j E.^. Project2018Id
|
|
|
|
, jca E.?. ProjectCollabAnon2018Role
|
|
|
|
, jcu E.?. ProjectCollabUser2018Role
|
|
|
|
)
|
|
|
|
for_ l $ \ (E.Value jid, E.Value malid, E.Value mulid) ->
|
|
|
|
update jid
|
|
|
|
[ Project2018CollabAnon =. malid
|
|
|
|
, Project2018CollabUser =. mulid
|
|
|
|
]
|
|
|
|
-- 36
|
|
|
|
, removeEntity "ProjectCollabAnon"
|
|
|
|
-- 37
|
|
|
|
, removeEntity "ProjectCollabUser"
|
|
|
|
-- 38
|
|
|
|
, removeEntity "RepoAccess"
|
|
|
|
-- 39
|
|
|
|
, removeEntity "RepoRoleInherit"
|
|
|
|
-- 40
|
|
|
|
, removeEntity "RepoRole"
|
2019-02-03 22:58:14 +09:00
|
|
|
-- 41
|
|
|
|
, addEntities model_2019_02_03_verifkey
|
2019-03-11 08:15:42 +09:00
|
|
|
-- 42
|
|
|
|
, unchecked $ lift $ do
|
2019-03-20 17:07:37 +09:00
|
|
|
deleteWhere ([] :: [Filter VerifKeySharedUsage2019])
|
|
|
|
deleteWhere ([] :: [Filter VerifKey2019])
|
2019-03-16 01:36:02 +09:00
|
|
|
-- 43
|
|
|
|
, removeUnique "Message" "UniqueMessage"
|
|
|
|
-- 44
|
|
|
|
, removeField "Message" "number"
|
2019-03-19 05:18:25 +09:00
|
|
|
-- 45
|
|
|
|
, removeField "Discussion" "nextMessage"
|
2019-03-20 17:07:37 +09:00
|
|
|
-- 46
|
|
|
|
, addEntities model_2019_03_19
|
|
|
|
-- 47
|
|
|
|
, unchecked $ lift $ do
|
|
|
|
msgs <- selectList ([] :: [Filter Message2019]) []
|
|
|
|
let mklocal (Entity mid m) = LocalMessage2019 (message2019Author m) mid
|
|
|
|
insertMany_ $ map mklocal msgs
|
|
|
|
-- 48
|
|
|
|
, removeField "Message" "author"
|
2019-03-23 05:46:42 +09:00
|
|
|
-- 49
|
|
|
|
, addUnique "Ticket" $ Unique "UniqueTicketDiscussion" ["discuss"]
|
2016-09-01 01:51:02 +09:00
|
|
|
]
|
|
|
|
|
2018-04-01 04:22:37 +09:00
|
|
|
migrateDB :: MonadIO m => ReaderT SqlBackend m (Either Text (Int, Int))
|
|
|
|
migrateDB =
|
|
|
|
let f cs = fmap (, length cs) <$> runMigrations schemaBackend 1 cs
|
|
|
|
in f changes
|