Skip to content
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

feat: add postgres setup and migration to ftl serve #518

Merged
merged 2 commits into from
Oct 25, 2023

Conversation

wesbillman
Copy link
Collaborator

@wesbillman wesbillman commented Oct 24, 2023

Fixes #499

When docker isn't running

ftl serve

info: Checking for FTL database
error: Cannot connect to the Docker daemon at unix:///Users/wesbillman/.orbstack/run/docker.sock. Is the docker daemon running?
: exit status 1
ftl: error: exit status 1

When the database hasn't been created yet

ftl serve

info: Checking for FTL database
info: Creating FTL database
info: 7f1fc8b7e07c983efdba2ef7e207ac0d7e82eb704bcd7e990ff5573839dafa8e
info: Waiting for ftl-db to be healthy
info: Initializing FTL schema
info: Applying: 001_init.sql
info: Starting 1 controller(s) and 10 runner(s)

When the database already exists

ftl serve
info: Checking for FTL database
info: Starting 1 controller(s) and 10 runner(s)

select {
case <-pollCtx.Done():
return errors.New("timed out waiting for container to be healthy")
default:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use case <-time.After(100 * time.Millisecond): instead of this and the sleep below.

@alecthomas
Copy link
Collaborator

LGTM nice!

@wesbillman wesbillman force-pushed the ftl-serve-init-postgres branch 2 times, most recently from dc7e49e to 1de7367 Compare October 24, 2023 22:45
func setupDB(ctx context.Context) (*string, error) {
logger := log.FromContext(ctx)

dsn := fmt.Sprintf("postgres://postgres:secret@localhost:5433/%s?sslmode=disable", ftlDBName)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be a better idea to dynamically allocate the port, then retrieve it, rather than hard-coding it. Either that, or add a flag for controlling it.

"--user", "postgres",
"--restart", "always",
"-e", "POSTGRES_PASSWORD=secret",
"-p", "5433:5432",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re. the above comment, changing this to -p 5432 will dynamically allocate a port. I think you could then pull the real port out of the docker inspect below.

@@ -105,6 +118,66 @@ func (s *serveCmd) Run(ctx context.Context) error {
return nil
}

func setupDB(ctx context.Context) (*string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be a pointer to a string, just return a string.

@wesbillman wesbillman force-pushed the ftl-serve-init-postgres branch from fd34cf6 to bdf6d28 Compare October 25, 2023 15:49
@wesbillman
Copy link
Collaborator Author

@alecthomas added some updates. Is this closer to what you were thinking? Thanks for the suggestions!!

Copy link
Collaborator

@alecthomas alecthomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naisu!

return "", errors.WithStack(err)
}

dsn := fmt.Sprintf("postgres://postgres:secret@localhost:%s/%s?sslmode=disable", strings.TrimSpace(string(port)), ftlContainerName)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

@wesbillman wesbillman merged commit 4485074 into main Oct 25, 2023
8 checks passed
@wesbillman wesbillman deleted the ftl-serve-init-postgres branch October 25, 2023 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Docker/Postgres setup for ftl serve
2 participants