~wunderbrick/foodro

6f401af33c772cd5d322b9b99dd401ee3a94475d — wunderbrick 1 year, 8 months ago 4abdfc9 postgres
Save postgres shenanigans
1 files changed, 62 insertions(+), 6 deletions(-)

M backend/src/Main.hs
M backend/src/Main.hs => backend/src/Main.hs +62 -6
@@ 7,7 7,7 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-type-defaults #-}
{-# OPTIONS_GHC -Wno-unused-do-bind #-}
{-# OPTIONS_GHC -Wno-unused-local-binds #-}


@@ 48,7 48,9 @@ import Database.PostgreSQL.Simple (
    close,
    execute_,
    connectPostgreSQL,
    withTransaction
 )
import Data.Pool (withResource)
import GHC.Generics
import Handlers.Deliveries.Create
import Handlers.Deliveries.Update


@@ 94,15 96,69 @@ import Servant.Server.Experimental.Auth (
    AuthServerData,
    mkAuthHandler,
 )
import qualified Database.Beam.AutoMigrate as BA
import Gargoyle.PostgreSQL.Connect
import qualified Data.ByteString.Internal as BSI
import Database.Beam.AutoMigrate.Postgres (getSchema)
import Database.Beam.AutoMigrate.Diff (diff)

-- TODO: Endpoints for marking events as "canceled" and for adding other events and updating next event TVar accordingly

dbName :: String
dbName = "freemealsdistrodb"

hsSchema :: BA.Schema
hsSchema = BA.fromAnnotatedDbSettings annotatedFreeMealsDistroDb (Proxy @'[ 'BA.UserDefinedFk IngredientAndSupplyListT, 'BA.UserDefinedFk FreeMealsDistroT ])

readmeDbTransaction :: (Connection -> IO a) -> IO a
readmeDbTransaction f = withDb dbName $ \pool ->
  withResource pool $ \conn ->
    withTransaction conn $ f conn

exampleShowMigration :: IO ()
exampleShowMigration = readmeDbTransaction $ \conn ->
  runBeamPostgres conn $
    BA.printMigration $ BA.migrate conn hsSchema

exampleAutoMigration :: IO ()
exampleAutoMigration = withDb dbName $ \pool ->
  withResource pool $ \conn ->
    BA.tryRunMigrationsWithEditUpdate annotatedFreeMealsDistroDb conn

main :: IO ()
main = do
    return ()
    {-}
    conn <- open "free-meals-distro.db"
    doDevDbSetup conn
    let connInfo = BSI.packChars $ "host=localhost port=5432 user=postgres password= dbname=" <> dbName
    conn <- connect defaultConnectInfo { connectUser = "postgres"
                                       , connectDatabase = dbName
                                       , connectPassword = "postgres"
                                       } --connectPostgreSQL connInfo


    let expectedHaskellSchema = hsSchema
    actualDatabaseSchema <- getSchema conn
    --print $ diff expectedHaskellSchema actualDatabaseSchema
    BA.runMigrationWithEditUpdate Prelude.id conn expectedHaskellSchema
{-
    putStrLn "----------------------------------------------------"
    putStrLn "MIGRATION PLAN (if migration needed):"
    putStrLn "----------------------------------------------------"
    exampleShowMigration
    putStrLn "----------------------------------------------------"
    putStrLn "MIGRATE?"
    putStrLn "----------------------------------------------------"
    putStrLn "Would you like to run the migration on the database in the folder \"readme-db\" (will be created if it doesn't exist)? (y/n)"
    response <- getLine
    case response of
      "y" -> exampleAutoMigration
      "Y" -> exampleAutoMigration
      _ -> putStrLn "Exiting"
-}
    --doDevDbSetup conn
    --exampleAutoMigration

    --BA.runMigrationUnsafe conn $ BA.migrate conn hsSchema

    {-
    fmd <- seedEvent conn
    case fmd of
        (Left noEvErr) -> putStrLn "\n\nERROR: no initial event found\n\n"


@@ 287,4 343,4 @@ updateDonationServer don =
        :<|> fromEmailEditDonationHandler don
        :<|> fromEmailThankYouDonationHandler don
        :<|> fromEmailConfirmDeleteDonationHandler don
        :<|> fromEmailDeleteDonationHandler don
\ No newline at end of file
        :<|> fromEmailDeleteDonationHandler don