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-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)
|
|
|
|
import Database.Persist.Schema.Types
|
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
|
|
|
|
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"
|
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
|