bldg14 / eventual

A simple event calendar
https://eventual-client.fly.dev
MIT License
0 stars 0 forks source link

Setup postgres database #51

Closed kevinfalting closed 9 months ago

kevinfalting commented 10 months ago

Overview

This PR adds a docker compose target to run the postgres database locally, then updates the server to connect to it.

Related Issues / PRs

Resolves #50

Thought Process

I was trying to keep it as simple as possible, so this only adds a convenience docker compose script to run pg, otherwise you can setup postgres manually.

The https://github.com/jackc/pgx module seems to be the best postgres db driver for Go, so I pulled it in, set up a connection pool, and performed a ping.

There was a little bit of refactoring for the config; I decided to make an explicit local config and remove the local defaults from the config struct tags.

Testing Steps

You'll need to run docker, then run:

docker compose up -d

Which will download the postgres image if it hasn't already and run it with the settings we want.

Then run the server: go run ./cmd/eventual

If you don't get any errors, then it succeeded. You can try shutting down the database and running the server, which should then fail and show an error.

When you're done, you can shutdown postgres running in docker by running:

docker compose down

Risks

If the environment variable in production isn't set, then the server will fail to connect to the database and shutdown. The database is also configured to scale to zero, so it's possible that after everything has stopped after no activity, when the server goes to boot up again, it may fail a few times (I actually don't know if fly will attempt to restart it) before the database comes up. A follow up issue may be to introduce some retry logic for the connections if pgx doesn't already include some.