Back to book

Database updates and migrations using docker

In this article, you will learn how to create and apply database migrations, as well as dropping and re-creating the database.

Note, this article assumes you are using docker to run your application as per the How do I view my app? article.


As you build your application and your entity diagram grows, the underlying database will also need to grow and change.

In the case of SpringBot, your application will do its best to keep the underlying database up to date automatically. (For example, adding a new entity to your entity diagram should cause SpringBot to automatically update the database next time it restarts.)

In the case of CSharpBot, it's a bit more manual, and therefore up to you as a developer to define when the database should be updated. (For example, adding a new entity to your entity diagram will not automatically update your CSharpBot database next time it restarts - there's a few steps to perform)

Performing a database migration in SpringBot

  1. In your git repository, pull any upstream changes
git pull
  1. As mentioned above, SpringBot will automatically apply database migrations when it is started. So simply start (or re-start if already running) your SpringBot application.
# To start the application
docker-compose up -d

# To restart the server
docker-compose restart server

Performing a database migration in CSharpBot

  1. In your git repository, pull any upstream changes
git pull
  1. Start your application if it is not already running
docker-compose up -d
  1. Stop the .Net application running on the server container. (The migration will fail to build otherwise)
docker-compose exec server pkill dotnet
  1. Create the new migration. (Consider a migration a "line in the sand." DotNet Core will calculate the differences between the previous migration and now, and automatically determine how your database needs to change)
docker-compose exec server dotnet ef migrations add "put a useful description of what has changed here"
  1. Restart the server container
docker-compose restart server

Starting fresh (AKA dropping the database and creating it again)

Sometimes the automatic migrations don't quite go to plan, so you may find yourself wanting to just start fresh.

There are a few ways to go about doing this, but the easiest is to delete the PostgreSQL data volume which contains the database.

  1. In your git repository, pull any upstream changes
git pull
  1. Stop the entire application
docker-compose down
  1. Run the following command to delete the docker volume that contains the PostgreSQL database
# For Windows (PowerShell)
docker volume rm ((get-location | split-path -leaf) + "_" + (docker-compose config --volumes | select-string -pattern "postgres").ToString().trim())

# For Bash (Mac/Linux)
docker volume rm "$(basename $(pwd))_$(docker-compose config --volumes | grep postgres)"
  1. Start the application again
docker-compose up -d
  1. As your application starts, it should automatically initialise the new database.
    (Note that if you're using CSharpBot, you may still need to create and apply a new migration as per above instructions if there has been any changes to the database since the last migration)