diff --git a/app/Main.hs b/app/Main.hs index 7462a4b..d8dbe39 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -5,6 +5,8 @@ {-# LANGUAGE FlexibleContexts #-} module Main where +import Prelude as P + import Servant import Servant.Server.Experimental.Auth @@ -21,6 +23,8 @@ import qualified Data.CaseInsensitive as CI import Data.IP import Database.PostgreSQL.Simple +import Database.PostgreSQL.Simple.Migration +import Database.PostgreSQL.Simple.Util import Network.Wai import Network.Wai.Logger @@ -81,14 +85,21 @@ main = do "password='" <> fromString (T.unpack db_passwd) <> "'" ) store <- newTVarIO S.empty - tracker <- newTVarIO M.empty - void $ execute_ conn initAvatar - void $ execute_ conn initUser - void $ execute_ conn initProduct - void $ execute_ conn initToken - void $ execute_ conn initAuthData - void $ execute_ conn initAmount - void $ execute_ conn initJournal + -- tracker <- newTVarIO M.empty + migrationsExist <- existsTable conn "schema_migrations" + when (not migrationsExist) $ do + withTransaction conn $ + void $ do + runMigration $ + MigrationContext MigrationInitialization True conn + execute_ conn initAvatar + execute_ conn initUser + execute_ conn initProduct + execute_ conn initToken + execute_ conn initAuthData + execute_ conn initAmount + execute_ conn initJournal + -- TODO: check for Migrations forkCleanProcess conn store withStdoutLogger $ \ilog -> do let settings = setPort (fromIntegral lport) $ diff --git a/mateamt.cabal b/mateamt.cabal index 33fc215..a2066e6 100644 --- a/mateamt.cabal +++ b/mateamt.cabal @@ -34,6 +34,7 @@ executable mateamt , mtl , opaleye , postgresql-simple + , postgresql-simple-migration , stm , network , servant diff --git a/shell.nix b/shell.nix index f46a366..24e7378 100644 --- a/shell.nix +++ b/shell.nix @@ -5,12 +5,13 @@ let inherit (nixpkgs) pkgs; f = { mkDerivation, aeson, base, base16-bytestring - , base64-bytestring, bytestring, case-insensitive, containers - , HsYAML, http-api-data, http-types, iproute, mtl, network, opaleye - , optparse-applicative, postgresql-simple, product-profunctors - , profunctors, pureMD5, random-bytestring, servant, servant-rawm - , servant-server, stdenv, stm, text, time, wai, wai-logger - , wai-middleware-throttle, warp + , base64-bytestring, bytestring, case-insensitive, clock + , containers, HsYAML, http-api-data, http-types, iproute, mtl + , network, opaleye, optparse-applicative, postgresql-simple + , postgresql-simple-migration, product-profunctors, profunctors + , pureMD5, random-bytestring, servant, servant-rawm, servant-server + , stdenv, stm, text, time, wai, wai-logger, wai-middleware-throttle + , warp }: mkDerivation { pname = "mateamt"; @@ -25,10 +26,11 @@ let servant-rawm servant-server stm text time wai wai-logger warp ]; executableHaskellDepends = [ - base base16-bytestring bytestring case-insensitive containers + base base16-bytestring bytestring case-insensitive clock containers HsYAML iproute mtl network opaleye optparse-applicative - postgresql-simple servant servant-server stm text time wai - wai-logger wai-middleware-throttle warp + postgresql-simple postgresql-simple-migration servant + servant-server stm text time wai wai-logger wai-middleware-throttle + warp ]; description = "A whole new matemat"; license = stdenv.lib.licenses.agpl3;