Developer Docs

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

  1. In your git repository, pull any upstream changes

     git pull
    
  2. 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

  1. In your git repository, pull any upstream changes

     git pull
    
  2. Start your application if it is not already running

     docker-compose up -d
    
  3. Stop the .Net application running on the server container. (The migration will fail to build otherwise)

     docker-compose exec server pkill dotnet
    
  4. 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"
    
  5. 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
    
  2. Stop the entire application

     docker-compose down
    
  3. 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)"
    
  4. Start the application again

     docker-compose up -d
    
  5. 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?

Thanks for your feedback!

If you would like to tell us more, please click on the link below to send us a message with more details.

Tool:

Generate

Iterate

Bot:

C#Bot

SpringBot

On this page

New to Codebots?

We know our software can be complicated, so we are always happy to have a chat if you have any questions.