Skip to content

Commit

Permalink
Add support for proper database migrations (fix pwreset table) (compd…
Browse files Browse the repository at this point in the history
…emocracy#542)

* Removed empty script file.

* Reorganized files for easier database migrations. Documented manual migrations.

* Improved inline comment for db init.

* Added disclaimer about backups.
  • Loading branch information
patcon committed Aug 21, 2020
1 parent cd71abc commit 3b31ed4
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 1 deletion.
35 changes: 35 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,41 @@ We use Google to automatically translate submitted comments into the language of
[base64-encoder]: https://codepen.io/bsngr/pen/awuDh


## Database Migrations

When we need to update the Polis database, we use SQL migration files.

During initial provisioning of your Docker containers, all the migrations will
be applied in order, and you won't need to think about this.

But if we update the database schema after your initial provisioning of your
server via Docker, you'll need to manually apply each new SQL migration.

- Please note: **Backups are your responsibility.** These instructions assume
the data is disposable, and do not attempt to make backups.
- Pull requests are welcome if you'd like to see more guidance on this.
- Your database data is stored on a docker volume, which means that it will
persist even when you destroy all your docker containers. Be mindful of this.
- You can remove ALL volumes defined within a `docker-compose` file via: `docker-compose down --volumes`
- You can remove ONE volume via `docker volume ls` and `docker volume rm <name>`
- SQL migrations can be found in [`server/postgres/migrations/`][] of this
repo.
- The path to the SQL file will be relative to its location in the docker
container filesystem, not your host system.

For example, if we add the migration file
`server/postgres/migrations/000001_update_pwreset_table.sql`, you'd run on your
host system:

```
docker-compose exec postgres psql --username postgres --dbname polis-dev --file=/docker-entrypoint-initdb.d/000001_update_pwreset_table.sql
```

You'd do this for each new file.

[`server/postgres/migrations/`]: /server/postgres/migrations


## Contribution notes

Please help us out as you go in setting things up by improving the deployment code and documentation!
Expand Down
5 changes: 4 additions & 1 deletion server/Dockerfile-db
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
FROM postgres:12.4-alpine

COPY ./postgres/db_setup_draft.sql /docker-entrypoint-initdb.d/
# Used when no existing database on postgres volume, including first initialization.
# See: docs/deployment.md#database-migrations
# See: https://github.com/docker-library/docs/blob/master/postgres/README.md#initialization-scripts
COPY ./postgres/migrations/*.sql /docker-entrypoint-initdb.d/
Empty file removed server/postgres/install_schema.sh
Empty file.
File renamed without changes.
2 changes: 2 additions & 0 deletions server/postgres/migrations/000001_update_pwreset_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE password_reset_tokens RENAME COLUMN pwresettoken TO token;
ALTER TABLE password_reset_tokens RENAME TO pwreset_tokens;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 3b31ed4

Please sign in to comment.