Add user-friendly error when DATABASE_URL envar is not set (#743)

Co-authored-by: sgarcia <sgarcia@pop-os.localdomain>
This commit is contained in:
Steven Garcia 2022-10-18 04:58:21 -05:00 committed by GitHub
parent ac625d964f
commit f6da4f8899
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,10 +9,11 @@ module Wasp.Generator.DbGenerator
where
import Data.Aeson (object, (.=))
import Data.Maybe (fromMaybe, maybeToList)
import Data.Maybe (fromMaybe, isNothing, maybeToList)
import StrongPath (Abs, Dir, Path', (</>))
import qualified StrongPath as SP
import System.Directory (doesFileExist)
import System.Environment (lookupEnv)
import Wasp.AppSpec (AppSpec, getEntities)
import qualified Wasp.AppSpec as AS
import qualified Wasp.AppSpec.App as AS.App
@ -83,9 +84,19 @@ genMigrationsDir spec =
-- | This function operates on generated code, and thus assumes the file drafts were written to disk
postWriteDbGeneratorActions :: AppSpec -> Path' Abs (Dir ProjectRootDir) -> IO ([GeneratorWarning], [GeneratorError])
postWriteDbGeneratorActions spec dstDir = do
-- checkForDbEnvVars does not depend on generated code but was placed here due to convenience
dbEnvironmentError <- maybeToList <$> checkForDbEnvVars spec
dbGeneratorWarnings <- maybeToList <$> warnIfDbNeedsMigration spec dstDir
dbGeneratorErrors <- maybeToList <$> genPrismaClient spec dstDir
return (dbGeneratorWarnings, dbGeneratorErrors)
return (dbGeneratorWarnings, dbGeneratorErrors ++ dbEnvironmentError)
checkForDbEnvVars :: AppSpec -> IO (Maybe GeneratorError)
checkForDbEnvVars spec = do
let dbSystem = fromMaybe AS.Db.SQLite (AS.Db.system =<< AS.App.db (snd $ getApp spec))
dbUrlVal <- lookupEnv "DATABASE_URL"
if dbSystem == AS.Db.PostgreSQL && isNothing dbUrlVal && AS.isBuild spec == False
then return $ Just $ GenericGeneratorError "You are using PostgreSQL as your database but the DATABASE_URL environment variable is not set.\n Please refer to the instructions here: https://wasp-lang.dev/docs/language/features#postgresql ."
else return Nothing
-- | Checks if user needs to run `wasp db migrate-dev` due to changes in schema.prisma, and if so, returns a warning.
-- When doing this, it looks at schema.prisma in the generated project.