diff --git a/flake.nix b/flake.nix index 817baa1..c003d33 100644 --- a/flake.nix +++ b/flake.nix @@ -6,32 +6,49 @@ flake-utils.url = "github:numtide/flake-utils"; }; - outputs = inputs : + outputs = inputs: inputs.flake-utils.lib.eachDefaultSystem (system: - let pkgs = inputs.nixpkgs.legacyPackages.${system}; - haskell = pkgs.haskell.packages.ghc96; - in - { - packages = { - default = haskell.callCabal2nix "zureg" ./. {}; - }; - devShells = { - default = pkgs.mkShell { - buildInputs = [ - pkgs.zlib.dev - ]; - packages = [ - pkgs.cabal-install - pkgs.entr - pkgs.jq - pkgs.postgresql - pkgs.awscli2 - haskell.stylish-haskell - (haskell.ghc.withPackages (p: - inputs.self.packages.${system}.default.buildInputs ++ - [p.postgresql-simple])) - ]; - }; - }; - }); + let + pkgs = inputs.nixpkgs.legacyPackages.${system}; + haskell = pkgs.haskell.packages.ghc96; + in { + packages = { default = haskell.callCabal2nix "zureg" ./. { }; }; + devShells = { + default = let + postgres = { + db = "zureg"; + password = "hunter2"; + port = "5433"; + }; + in pkgs.mkShell { + buildInputs = [ pkgs.zlib.dev ]; + packages = [ + pkgs.cabal-install + pkgs.entr + pkgs.jq + pkgs.docker + pkgs.postgresql + pkgs.awscli2 + haskell.stylish-haskell + (haskell.ghc.withPackages (p: + inputs.self.packages.${system}.default.buildInputs + ++ [ p.postgresql-simple ])) + ]; + + shellHook = '' + docker container port ${postgres.db}-postgres || docker run \ + --rm \ + --name ${postgres.db}-postgres \ + -e POSTGRES_DB=${postgres.db} \ + -e POSTGRES_PASSWORD=${postgres.password} \ + -p ${postgres.port}:5432 \ + -d postgres + ''; + + ZUREG_DB = + "postgresql://postgres:${postgres.password}@localhost:${postgres.port}/${postgres.db}"; + }; + }; + formatter = pkgs.nixfmt; + }); } diff --git a/lib/Zureg/Main/Migrations.hs b/lib/Zureg/Database/Migrations.hs similarity index 84% rename from lib/Zureg/Main/Migrations.hs rename to lib/Zureg/Database/Migrations.hs index 63896f7..630a3de 100644 --- a/lib/Zureg/Main/Migrations.hs +++ b/lib/Zureg/Database/Migrations.hs @@ -1,8 +1,11 @@ {-# LANGUAGE OverloadedStrings #-} -module Zureg.Main.Migrations where +module Zureg.Database.Migrations + ( migrate + ) where import qualified Data.ByteString.Char8 as BS8 import Data.Char (isDigit) +import Data.Foldable (for_) import Data.List (sortOn) import Data.String (fromString) import Data.Traversable (for) @@ -24,18 +27,18 @@ listMigrations = sortOn fst <$> do where dir = "lib/Zureg/Database/Migrations" -main :: IO () -main = do +migrate :: IO () +migrate = do pgstring <- lookupEnv "ZUREG_DB" >>= maybe (fail "ZUREG_DB not set") pure conn <- Pg.connectPostgreSQL $ BS8.pack pgstring - Pg.execute_ conn "\ + _ <- Pg.execute_ conn "\ \CREATE TABLE IF NOT EXISTS migrations (\n\ \ version INT NOT NULL PRIMARY KEY,\n\ \ path TEXT NOT NULL\n\ \)" migrations <- listMigrations - for migrations $ \(version, path) -> Pg.withTransaction conn $ do + for_ migrations $ \(version, path) -> Pg.withTransaction conn $ do rows <- Pg.query conn "SELECT version FROM migrations WHERE version = ?" (Pg.Only version) :: IO [Pg.Only Int] @@ -45,8 +48,8 @@ main = do [] -> do IO.hPutStrLn IO.stderr $ "Running migration: " ++ path contents <- readFile path - Pg.execute_ conn $ fromString contents - Pg.execute conn + _ <- Pg.execute_ conn $ fromString contents + _ <- Pg.execute conn "INSERT INTO migrations (version, path) VALUES (?, ?)" (version, path) pure () diff --git a/lib/Zureg/Main/Migrate.hs b/lib/Zureg/Main/Migrate.hs new file mode 100644 index 0000000..8824176 --- /dev/null +++ b/lib/Zureg/Main/Migrate.hs @@ -0,0 +1,7 @@ +{-# LANGUAGE OverloadedStrings #-} +module Zureg.Main.Migrate (main) where + +import Zureg.Database.Migrations (migrate) + +main :: IO () +main = migrate diff --git a/zureg.cabal b/zureg.cabal index 579f494..c7581e2 100644 --- a/zureg.cabal +++ b/zureg.cabal @@ -31,6 +31,7 @@ Library Exposed-modules: Zureg.Database + Zureg.Database.Migrations Zureg.Form Zureg.Hackathon Zureg.Hackathon.Interface @@ -64,6 +65,7 @@ Library Zureg.Main.Export Zureg.Main.Janitor Zureg.Main.PopWaitlist + Zureg.Main.Migrate Zureg.Main.Web Zureg.Model Zureg.Model.Csv