Skip to content

Commit 62aa2b7

Browse files
authored
Adjust the DB check error message (#1558)
1 parent 96ea796 commit 62aa2b7

File tree

8 files changed

+81
-13
lines changed

8 files changed

+81
-13
lines changed

waspc/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ app todoApp {
2020
### 🐞 Bug fixes / 🔧 small improvements
2121
- Changed the minimum number of machines that a server app is using when deployed to Fly.io from 0 to 1. This prevents the server app from shutting down when there are no requests to it. There might be some other work that the server is doing e.g. running periodic Jobs or sending e-mails, so we want to make sure that the server is always running.
2222
- Fixes a bug where copying of migrations dir failed due to a missing `migrations` dir.
23+
- Fixes a regression where a missing DB on the DB server would prevent project from running. Now, Wasp will tolerate the missing DB error and rely on Prisma to create the DB for you (like before).
2324

2425

2526
## 0.11.7

waspc/cli/src/Wasp/Cli/Command/Require.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import qualified System.FilePath as FP
3939
import Wasp.Cli.Command (CommandError (CommandError), Requirable (checkRequirement), require)
4040
import Wasp.Cli.Common (WaspProjectDir)
4141
import qualified Wasp.Cli.Common as Cli.Common
42-
import Wasp.Generator.DbGenerator.Operations (isDbRunning)
42+
import Wasp.Generator.DbGenerator.Operations (isDbConnectionPossible, testDbConnection)
4343

4444
data DbConnectionEstablished = DbConnectionEstablished deriving (Typeable)
4545

@@ -49,7 +49,8 @@ instance Requirable DbConnectionEstablished where
4949
-- call to 'require' will not result in an infinite loop.
5050
InWaspProject waspProjectDir <- require
5151
let outDir = waspProjectDir SP.</> Cli.Common.dotWaspDirInWaspProjectDir SP.</> Cli.Common.generatedCodeDirInDotWaspDir
52-
dbIsRunning <- liftIO $ isDbRunning outDir
52+
dbIsRunning <- liftIO $ isDbConnectionPossible <$> testDbConnection outDir
53+
5354
if dbIsRunning
5455
then return DbConnectionEstablished
5556
else throwError noDbError

waspc/examples/crud-testing/migrations/20230816101428_add_address/migration.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

waspc/examples/crud-testing/migrations/20230821110043_inital/migration.sql renamed to waspc/examples/crud-testing/migrations/20231106110220_initial/migration.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CREATE TABLE "User" (
33
"id" SERIAL NOT NULL,
44
"username" TEXT NOT NULL,
55
"password" TEXT NOT NULL,
6+
"address" TEXT,
67

78
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
89
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from "vite";
2+
3+
export default defineConfig({
4+
server: {
5+
open: false
6+
}
7+
})

waspc/src/Wasp/Generator/DbGenerator/Operations.hs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ module Wasp.Generator.DbGenerator.Operations
66
areAllMigrationsAppliedToDb,
77
dbReset,
88
dbSeed,
9-
isDbRunning,
9+
testDbConnection,
10+
isDbConnectionPossible,
11+
prismaErrorContainsDbNotCreatedError,
1012
)
1113
where
1214

@@ -17,10 +19,12 @@ import Control.Monad (when)
1719
import Control.Monad.Catch (catch)
1820
import Control.Monad.Extra (whenM)
1921
import Data.Either (isRight)
22+
import qualified Data.Text as T
2023
import qualified Path as P
2124
import StrongPath (Abs, Dir, File, Path', Rel, (</>))
2225
import qualified StrongPath as SP
2326
import System.Exit (ExitCode (..))
27+
import qualified Text.Regex.TDFA as TR
2428
import Wasp.Generator.Common (ProjectRootDir)
2529
import Wasp.Generator.DbGenerator.Common
2630
( DbSchemaChecksumFile,
@@ -38,13 +42,23 @@ import Wasp.Generator.DbGenerator.Common
3842
import qualified Wasp.Generator.DbGenerator.Jobs as DbJobs
3943
import Wasp.Generator.FileDraft.WriteableMonad (WriteableMonad (copyDirectoryRecursive, doesDirectoryExist))
4044
import qualified Wasp.Generator.Job as J
41-
import Wasp.Generator.Job.IO (printJobMsgsUntilExitReceived, readJobMessagesAndPrintThemPrefixed)
45+
import Wasp.Generator.Job.IO
46+
( collectJobTextOutputUntilExitReceived,
47+
printJobMsgsUntilExitReceived,
48+
readJobMessagesAndPrintThemPrefixed,
49+
)
4250
import qualified Wasp.Generator.WriteFileDrafts as Generator.WriteFileDrafts
4351
import Wasp.Project.Db.Migrations (DbMigrationsDir)
4452
import Wasp.Util (checksumFromFilePath, hexToString)
4553
import Wasp.Util.IO (deleteFileIfExists, doesFileExist)
4654
import qualified Wasp.Util.IO as IOUtil
4755

56+
data DbConnectionTestResult
57+
= DbConnectionSuccess
58+
| DbNotCreated
59+
| DbConnectionFailure
60+
deriving (Eq)
61+
4862
-- | Migrates in the generated project context and then copies the migrations dir back
4963
-- up to the wasp project dir to ensure they remain in sync.
5064
migrateDevAndCopyToSource :: Path' Abs (Dir DbMigrationsDir) -> Path' Abs (Dir ProjectRootDir) -> MigrateArgs -> IO (Either String ())
@@ -143,15 +157,32 @@ dbSeed genProjectDir seedName = do
143157
ExitSuccess -> Right ()
144158
ExitFailure c -> Left $ "Failed with exit code " <> show c
145159

146-
isDbRunning ::
160+
testDbConnection ::
147161
Path' Abs (Dir ProjectRootDir) ->
148-
IO Bool
149-
isDbRunning genProjectDir = do
162+
IO DbConnectionTestResult
163+
testDbConnection genProjectDir = do
150164
chan <- newChan
151165
exitCode <- DbJobs.dbExecuteTest genProjectDir chan
152-
-- NOTE: We only care if the command succeeds or fails, so we don't look at
153-
-- the exit code or stdout/stderr for the process.
154-
return $ exitCode == ExitSuccess
166+
167+
case exitCode of
168+
ExitSuccess -> return DbConnectionSuccess
169+
ExitFailure _ -> do
170+
outputLines <- collectJobTextOutputUntilExitReceived chan
171+
let databaseNotCreated = any prismaErrorContainsDbNotCreatedError outputLines
172+
173+
return $
174+
if databaseNotCreated
175+
then DbNotCreated
176+
else DbConnectionFailure
177+
178+
-- Prisma error code for "Database not created" is P1003.
179+
prismaErrorContainsDbNotCreatedError :: T.Text -> Bool
180+
prismaErrorContainsDbNotCreatedError text = text TR.=~ ("\\bP1003\\b" :: String)
181+
182+
isDbConnectionPossible :: DbConnectionTestResult -> Bool
183+
isDbConnectionPossible DbConnectionSuccess = True
184+
isDbConnectionPossible DbNotCreated = True
185+
isDbConnectionPossible _ = False
155186

156187
generatePrismaClients :: Path' Abs (Dir ProjectRootDir) -> IO (Either String ())
157188
generatePrismaClients projectRootDir = do

waspc/src/Wasp/Generator/Job/IO.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ module Wasp.Generator.Job.IO
22
( readJobMessagesAndPrintThemPrefixed,
33
printJobMessage,
44
printJobMsgsUntilExitReceived,
5+
collectJobTextOutputUntilExitReceived,
56
)
67
where
78

89
import Control.Concurrent (Chan, readChan)
910
import Control.Monad.IO.Class (liftIO)
11+
import Data.Text (Text)
1012
import qualified Data.Text.IO as T.IO
1113
import System.IO (hFlush)
1214
import qualified Wasp.Generator.Job as J
@@ -29,6 +31,15 @@ readJobMessagesAndPrintThemPrefixed chan = runPrefixedWriter go
2931
J.JobOutput {} -> printJobMessagePrefixed jobMsg >> go
3032
J.JobExit {} -> return ()
3133

34+
collectJobTextOutputUntilExitReceived :: Chan J.JobMessage -> IO [Text]
35+
collectJobTextOutputUntilExitReceived = go []
36+
where
37+
go jobTextOutput chan = do
38+
jobMsg <- readChan chan
39+
case J._data jobMsg of
40+
J.JobExit {} -> return jobTextOutput
41+
J.JobOutput text _ -> go (text : jobTextOutput) chan
42+
3243
printJobMessage :: J.JobMessage -> IO ()
3344
printJobMessage jobMsg = do
3445
let outHandle = getJobMessageOutHandle jobMsg

waspc/test/Generator/DbGeneratorTest.hs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module Generator.DbGeneratorTest where
22

3-
import Test.Tasty.Hspec (Spec, it, shouldBe)
3+
import qualified Data.Text as T
4+
import Test.Tasty.Hspec (Spec, describe, it, shouldBe)
45
import Wasp.Generator.DbGenerator.Common
56
( MigrateArgs (..),
67
defaultMigrateArgs,
78
)
89
import Wasp.Generator.DbGenerator.Jobs (asPrismaCliArgs)
10+
import Wasp.Generator.DbGenerator.Operations (prismaErrorContainsDbNotCreatedError)
911

1012
spec_Jobs :: Spec
1113
spec_Jobs =
@@ -19,3 +21,19 @@ spec_Jobs =
1921
`shouldBe` ["--name", "something else longer"]
2022
asPrismaCliArgs (MigrateArgs {_migrationName = Just "something", _isCreateOnlyMigration = True})
2123
`shouldBe` ["--create-only", "--name", "something"]
24+
25+
spec_DbConnectionTest :: Spec
26+
spec_DbConnectionTest =
27+
describe "prismaErrorContainsDbNotCreatedError" $ do
28+
it "should not match DB server not available error" $ do
29+
prismaErrorContainsDbNotCreatedError
30+
(T.pack "Error: P1001\n\nCan't reach database server at `localhost`:`5432`\n\nPlease make sure your database server is running at `localhost`:`5432`.")
31+
`shouldBe` False
32+
it "should not match similar error codes" $ do
33+
prismaErrorContainsDbNotCreatedError
34+
(T.pack "Error: P10033\n\nMade up error code")
35+
`shouldBe` False
36+
it "should match the DB not created error code" $ do
37+
prismaErrorContainsDbNotCreatedError
38+
(T.pack "Error: P1003\n\nDatabase `x` does not exist on the database server at `localhost:5432`.")
39+
`shouldBe` True

0 commit comments

Comments
 (0)