-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adjust the DB check error message #1558
Changes from 5 commits
f58d772
e12b49d
41ea676
2311d68
771923f
76510ce
5722c8d
ec7359e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from "vite"; | ||
|
||
export default defineConfig({ | ||
server: { | ||
open: false | ||
} | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ module Wasp.Generator.DbGenerator.Operations | |
areAllMigrationsAppliedToDb, | ||
dbReset, | ||
dbSeed, | ||
isDbRunning, | ||
testDbConnection, | ||
isDbConnectionEstablished, | ||
containsDatabaseNotCreatedError, | ||
) | ||
where | ||
|
||
|
@@ -17,10 +19,12 @@ import Control.Monad (when) | |
import Control.Monad.Catch (catch) | ||
import Control.Monad.Extra (whenM) | ||
import Data.Either (isRight) | ||
import qualified Data.Text as T | ||
import qualified Path as P | ||
import StrongPath (Abs, Dir, File, Path', Rel, (</>)) | ||
import qualified StrongPath as SP | ||
import System.Exit (ExitCode (..)) | ||
import qualified Text.Regex.TDFA as TR | ||
import Wasp.Generator.Common (ProjectRootDir) | ||
import Wasp.Generator.DbGenerator.Common | ||
( DbSchemaChecksumFile, | ||
|
@@ -38,13 +42,23 @@ import Wasp.Generator.DbGenerator.Common | |
import qualified Wasp.Generator.DbGenerator.Jobs as DbJobs | ||
import Wasp.Generator.FileDraft.WriteableMonad (WriteableMonad (copyDirectoryRecursive)) | ||
import qualified Wasp.Generator.Job as J | ||
import Wasp.Generator.Job.IO (printJobMsgsUntilExitReceived, readJobMessagesAndPrintThemPrefixed) | ||
import Wasp.Generator.Job.IO | ||
( collectJobTextOutputUntilExitReceived, | ||
printJobMsgsUntilExitReceived, | ||
readJobMessagesAndPrintThemPrefixed, | ||
) | ||
import qualified Wasp.Generator.WriteFileDrafts as Generator.WriteFileDrafts | ||
import Wasp.Project.Db.Migrations (DbMigrationsDir) | ||
import Wasp.Util (checksumFromFilePath, hexToString) | ||
import Wasp.Util.IO (deleteFileIfExists, doesFileExist) | ||
import qualified Wasp.Util.IO as IOUtil | ||
|
||
data DbConnectionTestResult | ||
= DbConnectionSuccess | ||
| DbNotCreated | ||
| DbConnectionFailure | ||
deriving (Eq) | ||
|
||
-- | Migrates in the generated project context and then copies the migrations dir back | ||
-- up to the wasp project dir to ensure they remain in sync. | ||
migrateDevAndCopyToSource :: Path' Abs (Dir DbMigrationsDir) -> Path' Abs (Dir ProjectRootDir) -> MigrateArgs -> IO (Either String ()) | ||
|
@@ -135,15 +149,32 @@ dbSeed genProjectDir seedName = do | |
ExitSuccess -> Right () | ||
ExitFailure c -> Left $ "Failed with exit code " <> show c | ||
|
||
isDbRunning :: | ||
testDbConnection :: | ||
Path' Abs (Dir ProjectRootDir) -> | ||
IO Bool | ||
isDbRunning genProjectDir = do | ||
IO DbConnectionTestResult | ||
testDbConnection genProjectDir = do | ||
chan <- newChan | ||
exitCode <- DbJobs.dbExecuteTest genProjectDir chan | ||
-- NOTE: We only care if the command succeeds or fails, so we don't look at | ||
-- the exit code or stdout/stderr for the process. | ||
return $ exitCode == ExitSuccess | ||
|
||
Martinsos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case exitCode of | ||
ExitSuccess -> return DbConnectionSuccess | ||
ExitFailure _ -> do | ||
outputLines <- collectJobTextOutputUntilExitReceived chan | ||
let databaseNotCreated = any containsDatabaseNotCreatedError outputLines | ||
|
||
return $ | ||
if databaseNotCreated | ||
then DbNotCreated | ||
else DbConnectionFailure | ||
|
||
-- Prisma error code for "Database not created" is P1003. | ||
containsDatabaseNotCreatedError :: T.Text -> Bool | ||
containsDatabaseNotCreatedError text = text TR.=~ ("\\bP1003\\b" :: String) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would probably put this in |
||
|
||
isDbConnectionEstablished :: DbConnectionTestResult -> Bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get what you do with this function, makes sense! I wonder a bit about name -> so we call that "connection established"? Even if database does not exist? It is not a bad name, but I wonder if it is precise enough. I guess the problem is I am not sure what "db connection established" really means. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've went with |
||
isDbConnectionEstablished DbConnectionSuccess = True | ||
isDbConnectionEstablished DbNotCreated = True | ||
isDbConnectionEstablished _ = False | ||
|
||
generatePrismaClients :: Path' Abs (Dir ProjectRootDir) -> IO (Either String ()) | ||
generatePrismaClients projectRootDir = do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,13 @@ module Wasp.Generator.Job.IO | |
( readJobMessagesAndPrintThemPrefixed, | ||
printJobMessage, | ||
printJobMsgsUntilExitReceived, | ||
collectJobTextOutputUntilExitReceived, | ||
) | ||
where | ||
|
||
import Control.Concurrent (Chan, readChan) | ||
import Control.Monad.IO.Class (liftIO) | ||
import Data.Text (Text) | ||
import qualified Data.Text.IO as T.IO | ||
import System.IO (hFlush) | ||
import qualified Wasp.Generator.Job as J | ||
|
@@ -29,6 +31,15 @@ readJobMessagesAndPrintThemPrefixed chan = runPrefixedWriter go | |
J.JobOutput {} -> printJobMessagePrefixed jobMsg >> go | ||
J.JobExit {} -> return () | ||
|
||
collectJobTextOutputUntilExitReceived :: Chan J.JobMessage -> IO [Text] | ||
collectJobTextOutputUntilExitReceived = collect [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In haskell, usual idiom is to go with |
||
where | ||
collect jobTextOutput chan = do | ||
jobMsg <- readChan chan | ||
case J._data jobMsg of | ||
J.JobExit {} -> return jobTextOutput | ||
J.JobOutput text _ -> collect (text : jobTextOutput) chan | ||
|
||
printJobMessage :: J.JobMessage -> IO () | ||
printJobMessage jobMsg = do | ||
let outHandle = getJobMessageOutHandle jobMsg | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be tempted to add
String
toDbConnectionFailure
that contains actual error message, is there a reason why you decided to drop that information?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we don't get an error message per se, but we get some command output that might be useless unless parsed.