Database updates and migrations using docker
Note, this article assumes you are using docker to run your application as per the How do I run my app? article.
Introduction
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 C#Bot, 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 C#Bot database next time it restarts - there’s a few steps to perform)
Performing a database migration in SpringBot
-
In your git repository, pull any upstream changes
git pull
-
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 C#Bot
-
In your git repository, pull any upstream changes
git pull
-
Start your application if it is not already running
docker-compose up -d
-
Stop the .Net application running on the server container. (The migration will fail to build otherwise)
docker-compose exec server pkill dotnet
-
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"
-
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.
-
In your git repository, pull any upstream changes
git pull
-
Stop the entire application
docker-compose down
-
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)"
-
Start the application again
docker-compose up -d
-
As your application starts, it should automatically initialise the new database. (Note that if you’re using C#Bot, 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)
Was this article helpful?