-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 initial backend database setup #6
Open
kusalananda
wants to merge
28
commits into
main
Choose a base branch
from
5-initial-backend-database-setup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Currently only contains the location of the backend database file. It is located in the `/vol` directory in the container, which is a local volume.
Currently only adds the SQLite command line utility to the unversioned golang base container based on Alpine Linux.
Currently only contains the backend service and its volume. Does nothing when you start it because there is no real entrypoint defined for the backend service.
* Migration 0001 adds a dummy table. * Migration 0002 drops the dummy table.
This script is supposed to be run by the backend container's entrypoint when the container starts up. This is not yet hooked up.
This is a script that handles database initialisation and migration. Currently, only migrations are handled.
The Dockerfile now has two main sections: 1. The "builder" bit is based on the Alpine golang image, and builds the project. 2. The "develop" bit is based on the plain Alpine image takes the built executable and sets the container up for actual running.
"Appropriate" == There is no database at "$database" (see backend.env) and there is a database dump at "initdb.d/init.sql".
This directory, `migration.d`, will be copied into the container when it is built.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR closes #5
What this PR adds:
At the top level, it adds the project
docker-compose.yml
file. Eventually, this file will be expanded to include services relating to the frontend, but it currently only provides a short definition of the backend service. Note that this PR is only concerned about the database setup and that there is no actual API code yet, so thedocker-compose.yml
file does not enable listening on any particular ports. However, it adds a volume,backend-volume
, which the backend uses to store a persistent database.In the
backend
directory, we find theDockerfile
used to build the backend container image and theentrypoint.d
directory containing scripts for performing database restore and migration when the container starts up.There is an
initdb.d
directory into which one may optionally put an SQL file calledinit.sql
. If this file exists and the container's volume does not contain a database, the file will be used to initialize the database.The
scripts
directory contains the single scriptdumpdb
, which creates an SQL file containing the statements necessary for restoring the database to its current state (e.g., by putting a dump ininitdb.d
as previously mentioned).The
migration.d
directory contains a set of database migrations. These will be applied to the database as the container starts up. Theentypoint.d/apply-migrations
scripts will automatically skip already applied migrations.The
entrypoint.d
andmigration.d
directories are copied into the container when the container is built. Theinitdb.d
directory is bind-mounted (read-only) into the container, allowing an operator to start the service with a previously backed-up database dump.Testing this PR
To test this PR, you would want to copy the
backend.env.example
file tobackend.env
, rundocker compose build
followed bydocker compose up -d
to run the service in the background. You would expect to see the following:Note that the "server" is simulated by a simple shell script that just runs
sleep infinity
, which never terminates.Dumping the database at this point should look like this:
Stopping the service and starting it again should inform you that migrations have already been applied:
You may also try stopping the services with
docker compose down -v
, which will remove the persistent volume and the database therein. Another thing to test is to start the service (which applies the migrations inmigration.d
if needed), stop everything yet again and then modify one of the migration files (just add a space or whatever). Rebuilding and restarting the services should now look like this:The last command above shows that the service isn't running as it encountered an issue when starting up. Since migrations are applied to a copy of the database, no data will have been changed in the database.