-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add --db-image
argument to wasp start db
command
#3182
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
Changes from all commits
98e6366
02fbe50
f3e029f
071dba6
eef03ef
85edcbb
2644208
dcdd083
b08f5b3
008b004
4c6ddc2
f96a943
7e70d20
9189200
0be6c33
f01ae55
3786a5c
7b8172e
3ad3ea1
4c4283c
de04937
8326fae
c37184d
0ae96d4
9a77d0f
11f79b3
579006c
f882fda
658f0dc
45855d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ main = withUtf8 . (`E.catch` handleInternalErrors) $ do | |
("new" : newArgs) -> Command.Call.New newArgs | ||
("new:ai" : newAiArgs) -> Command.Call.NewAi newAiArgs | ||
["start"] -> Command.Call.Start | ||
["start", "db"] -> Command.Call.StartDb | ||
("start" : "db" : startDbArgs) -> Command.Call.StartDb startDbArgs | ||
["clean"] -> Command.Call.Clean | ||
["ts-setup"] -> Command.Call.TsSetup | ||
["compile"] -> Command.Call.Compile | ||
|
@@ -104,7 +104,7 @@ main = withUtf8 . (`E.catch` handleInternalErrors) $ do | |
projectConfigJson | ||
_unknownCommand -> printWaspNewAiUsage >> exitFailure | ||
Command.Call.Start -> runCommand start | ||
Command.Call.StartDb -> runCommand Command.Start.Db.start | ||
Command.Call.StartDb startDbArgs -> runCommand $ Command.Start.Db.start startDbArgs | ||
Command.Call.Clean -> runCommand clean | ||
Command.Call.TsSetup -> runCommand tsConfigSetup | ||
Command.Call.Compile -> runCommand compile | ||
|
@@ -176,7 +176,9 @@ printUsage = | |
cmd " uninstall Removes Wasp from your system.", | ||
title " IN PROJECT", | ||
cmd " start Runs Wasp app in development mode, watching for file changes.", | ||
cmd " start db Starts managed development database for you.", | ||
cmd " start db [--db-image <image>]", | ||
" Starts managed development database for you.", | ||
" Optionally specify a custom Docker image.", | ||
cmd " db <db-cmd> [args] Executes a database command. Run 'wasp db' for more info.", | ||
cmd " clean Deletes all generated code, all cached artifacts, and the node_modules dir.", | ||
" Wasp equivalent of 'have you tried closing and opening it again?'.", | ||
|
@@ -220,7 +222,7 @@ printVersion = do | |
dbCli :: [String] -> IO () | ||
dbCli args = case args of | ||
-- These commands don't require an existing and running database. | ||
["start"] -> runCommand Command.Start.Db.start | ||
"start" : optionalStartArgs -> runCommand $ Command.Start.Db.start optionalStartArgs | ||
-- These commands require an existing and running database. | ||
["reset"] -> runCommandThatRequiresDbRunning Command.Db.Reset.reset | ||
"migrate-dev" : optionalMigrateArgs -> runCommandThatRequiresDbRunning $ Command.Db.Migrate.migrateDev optionalMigrateArgs | ||
|
@@ -238,21 +240,25 @@ printDbUsage = | |
" wasp db <command> [command-args]", | ||
"", | ||
title "COMMANDS", | ||
cmd " start Alias for `wasp start db`.", | ||
cmd " reset Drops all data and tables from development database and re-applies all migrations.", | ||
cmd " seed [name] Executes a db seed function (specified via app.db.seeds).", | ||
" If there are multiple seeds, you can specify a seed to execute by providing its name,", | ||
" or if not then you will be asked to provide the name interactively.", | ||
cmd $ intercalate "\n" [ | ||
" migrate-dev Ensures dev database corresponds to the current state of schema(entities):", | ||
" - Generates a new migration if there are changes in the schema.", | ||
" - Applies any pending migrations to the database either using the", | ||
" supplied migration name or asking for one.", | ||
" start [--db-image <image>] Alias for `wasp start db`.", | ||
" Starts managed development database for you.", | ||
" Optionally specify a custom Docker image." | ||
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. Not the same as for 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 don't get this comment, can you clarify? I do handle the argument in both cases. 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. Aha sorry, I should have been more verbose! So CLI docs for 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. ahh understood, my eyes were skipping over that haha |
||
], | ||
cmd " reset Drops all data and tables from development database and re-applies all migrations.", | ||
cmd " seed [name] Executes a db seed function (specified via app.db.seeds).", | ||
" If there are multiple seeds, you can specify a seed to execute by providing its name,", | ||
" or if not then you will be asked to provide the name interactively.", | ||
cmd $ intercalate "\n" [ | ||
" migrate-dev Ensures dev database corresponds to the current state of schema(entities):", | ||
" - Generates a new migration if there are changes in the schema.", | ||
" - Applies any pending migrations to the database either using the", | ||
" supplied migration name or asking for one.", | ||
" OPTIONS:", | ||
" --name [migration-name]", | ||
" --create-only" | ||
], | ||
cmd " studio GUI for inspecting your database.", | ||
cmd " studio GUI for inspecting your database.", | ||
"", | ||
title "EXAMPLES", | ||
" wasp db migrate-dev", | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,7 +7,9 @@ where | |||||||||||
import Control.Monad (when) | ||||||||||||
import qualified Control.Monad.Except as E | ||||||||||||
import Control.Monad.IO.Class (liftIO) | ||||||||||||
import Data.Function ((&)) | ||||||||||||
import Data.Maybe (isJust) | ||||||||||||
import qualified Options.Applicative as Opt | ||||||||||||
import StrongPath (Abs, Dir, File', Path', Rel, fromRelFile) | ||||||||||||
import System.Environment (lookupEnv) | ||||||||||||
import System.Process (callCommand) | ||||||||||||
|
@@ -16,38 +18,62 @@ import qualified Wasp.AppSpec as AS | |||||||||||
import qualified Wasp.AppSpec.App.Db as AS.App.Db | ||||||||||||
import qualified Wasp.AppSpec.Valid as ASV | ||||||||||||
import Wasp.Cli.Command (Command, CommandError (CommandError)) | ||||||||||||
import Wasp.Cli.Command.Call (Arguments) | ||||||||||||
import Wasp.Cli.Command.Common (throwIfExeIsNotAvailable) | ||||||||||||
import Wasp.Cli.Command.Compile (analyze) | ||||||||||||
import Wasp.Cli.Command.Message (cliSendMessageC) | ||||||||||||
import Wasp.Cli.Command.Require (InWaspProject (InWaspProject), require) | ||||||||||||
import Wasp.Cli.Util.Parser (parseArguments) | ||||||||||||
import Wasp.Db.Postgres (defaultDockerImageForPostgres) | ||||||||||||
import qualified Wasp.Message as Msg | ||||||||||||
import Wasp.Project.Common (WaspProjectDir, makeAppUniqueId) | ||||||||||||
import Wasp.Project.Db (databaseUrlEnvVarName) | ||||||||||||
import qualified Wasp.Project.Db.Dev.Postgres as Dev.Postgres | ||||||||||||
import Wasp.Project.Env (dotEnvServer) | ||||||||||||
import Wasp.Util (whenM) | ||||||||||||
import Wasp.Util.Docker (DockerImageName) | ||||||||||||
import qualified Wasp.Util.Network.Socket as Socket | ||||||||||||
|
||||||||||||
-- | Starts a "managed" dev database, where "managed" means that | ||||||||||||
-- Wasp creates it and connects the Wasp app with it. | ||||||||||||
-- Wasp is smart while doing this so it checks which database is specified | ||||||||||||
-- in Wasp configuration and spins up a database of appropriate type. | ||||||||||||
start :: Command () | ||||||||||||
start = do | ||||||||||||
start :: Arguments -> Command () | ||||||||||||
start args = do | ||||||||||||
startDbArgs <- | ||||||||||||
parseArguments "wasp start db" startDbArgsParser args | ||||||||||||
& either (E.throwError . CommandError "Invalid arguments") return | ||||||||||||
Comment on lines
+42
to
+45
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. No need to change anything here, just FYI. I rarely use Here's how I would probably write this: start args = do
startDbArgs <-
either (E.throwError . CommandError "Invalid arguments") return $
parseArguments "wasp start db" startDbArgsParser args Or even like this: start args = do
startDbArgs <- case parseArguments "wasp start db" startDbArgsParser args of
Left err -> E.throwError $ CommandError "Invalid arguments" err
Right parsedArgs -> return parsedArgs You can pick whatever you like the most, all options are perfectly valid. 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. Opposite opinion: I think the current code with the While In Haskell, you anyway read both from left to right and right to left, it depends on the situation, so there is no golden rule here. It does make sense to stick with how people normally write Haskell, so I agree one shoudln't go crazy with 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 this case it's written like that for consistency, like in here: wasp/waspc/cli/src/Wasp/Cli/Command/CreateNewProject.hs Lines 32 to 36 in 1129a07
I think a good idea in general for this would be to start using Options.Applicative for all of our CLI argument parsing, not just occasionally. |
||||||||||||
|
||||||||||||
InWaspProject waspProjectDir <- require | ||||||||||||
appSpec <- analyze waspProjectDir | ||||||||||||
|
||||||||||||
throwIfCustomDbAlreadyInUse appSpec | ||||||||||||
|
||||||||||||
let (appName, _) = ASV.getApp appSpec | ||||||||||||
|
||||||||||||
case ASV.getValidDbSystem appSpec of | ||||||||||||
AS.App.Db.SQLite -> noteSQLiteDoesntNeedStart | ||||||||||||
AS.App.Db.PostgreSQL -> startPostgreDevDb waspProjectDir appName | ||||||||||||
AS.App.Db.PostgreSQL -> startPostgresDevDb waspProjectDir appName (dbImage startDbArgs) | ||||||||||||
where | ||||||||||||
noteSQLiteDoesntNeedStart = | ||||||||||||
cliSendMessageC . Msg.Info $ | ||||||||||||
"Nothing to do! You are all good, you are using SQLite which doesn't need to be started." | ||||||||||||
|
||||||||||||
startDbArgsParser :: Opt.Parser StartDbArgs | ||||||||||||
startDbArgsParser = | ||||||||||||
StartDbArgs | ||||||||||||
<$> Opt.strOption | ||||||||||||
( Opt.long "db-image" | ||||||||||||
<> Opt.metavar "IMAGE" | ||||||||||||
<> Opt.help "Docker image to use for the database" | ||||||||||||
<> Opt.showDefault | ||||||||||||
<> Opt.value defaultDockerImageForPostgres | ||||||||||||
) | ||||||||||||
|
||||||||||||
data StartDbArgs = StartDbArgs | ||||||||||||
{ dbImage :: DockerImageName | ||||||||||||
} | ||||||||||||
|
||||||||||||
throwIfCustomDbAlreadyInUse :: AS.AppSpec -> Command () | ||||||||||||
throwIfCustomDbAlreadyInUse spec = do | ||||||||||||
throwIfDbUrlInEnv | ||||||||||||
|
@@ -82,8 +108,8 @@ throwIfCustomDbAlreadyInUse spec = do | |||||||||||
throwCustomDbAlreadyInUseError msg = | ||||||||||||
E.throwError $ CommandError "You are using custom database already" msg | ||||||||||||
|
||||||||||||
startPostgreDevDb :: Path' Abs (Dir WaspProjectDir) -> String -> Command () | ||||||||||||
startPostgreDevDb waspProjectDir appName = do | ||||||||||||
startPostgresDevDb :: Path' Abs (Dir WaspProjectDir) -> String -> String -> Command () | ||||||||||||
startPostgresDevDb waspProjectDir appName dbDockerImage = do | ||||||||||||
throwIfExeIsNotAvailable | ||||||||||||
"docker" | ||||||||||||
"To run PostgreSQL dev database, Wasp needs `docker` installed and in PATH." | ||||||||||||
|
@@ -94,6 +120,7 @@ startPostgreDevDb waspProjectDir appName = do | |||||||||||
[ "✨ Starting a PostgreSQL dev database (based on your Wasp config) ✨", | ||||||||||||
"", | ||||||||||||
"Additional info:", | ||||||||||||
" ℹ Using Docker image: " <> dbDockerImage, | ||||||||||||
" ℹ Connection URL, in case you might want to connect with external tools:", | ||||||||||||
" " <> connectionUrl, | ||||||||||||
" ℹ Database data is persisted in a docker volume with the following name" | ||||||||||||
|
@@ -117,7 +144,7 @@ startPostgreDevDb waspProjectDir appName = do | |||||||||||
"--env POSTGRES_PASSWORD=%s", | ||||||||||||
"--env POSTGRES_USER=%s", | ||||||||||||
"--env POSTGRES_DB=%s", | ||||||||||||
"postgres" | ||||||||||||
"%s" | ||||||||||||
] | ||||||||||||
) | ||||||||||||
dockerContainerName | ||||||||||||
|
@@ -126,6 +153,7 @@ startPostgreDevDb waspProjectDir appName = do | |||||||||||
Dev.Postgres.defaultDevPass | ||||||||||||
Dev.Postgres.defaultDevUser | ||||||||||||
dbName | ||||||||||||
dbDockerImage | ||||||||||||
liftIO $ callCommand command | ||||||||||||
where | ||||||||||||
dockerVolumeName = makeWaspDevDbDockerVolumeName waspProjectDir appName | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Wasp.Util.Docker (DockerImageName) where | ||
|
||
type DockerImageName = String |
Uh oh!
There was an error while loading. Please reload this page.