Skip to content

Commit

Permalink
Bump
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Jan 8, 2025
1 parent 4c6267f commit ec452f9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 34 deletions.
71 changes: 44 additions & 27 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
17 changes: 10 additions & 7 deletions lib/Zureg/Main/Migrations.hs → lib/Zureg/Database/Migrations.hs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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]
Expand All @@ -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 ()
Expand Down
7 changes: 7 additions & 0 deletions lib/Zureg/Main/Migrate.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}
module Zureg.Main.Migrate (main) where

import Zureg.Database.Migrations (migrate)

main :: IO ()
main = migrate
2 changes: 2 additions & 0 deletions zureg.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Library

Exposed-modules:
Zureg.Database
Zureg.Database.Migrations
Zureg.Form
Zureg.Hackathon
Zureg.Hackathon.Interface
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ec452f9

Please sign in to comment.