Flyway

1. Running without using docker compose.

This is flyway tool allowing you to populate the data in dev environment and production environment with just minimized commands using your sql migrations.

I try to make the workflow of migrating our database looks like the same, regardless of whatever envinronment you are in. The flow will be as following for any environment:

Step 1: Prepare your environment variable file: this file contains required parameter to access a database you want. Below is the sample for environment file using for accessing LANGCHAT database:

LANGCHAT_DB=langchat
LANGCHAT_URL=jdbc:postgresql://${HOST}:${PORT}
POSTGRES_USER=${USER}
POSTGRES_PASSWORD=${USER_PASSWORD}

Step 2: Build the image of flyway using for the database that you want to access: First access to subdirectory that has the name of database you want to use from root directory, then builing image as below:

user@root:~$  cd ./{subdir}
user@root:~$ docker build -t {image_name}/{your_tag} .

If you dont want to do so, you can directly pull and use my image from remote which has the name syntax narutosimaha/flyway-{dbname}, for instance I want to use langchat db:

user@root:~$  docker pull narutosimaha/flyway-langchat

Step 3: Populate your database: In this step, first ensure that your database is active that is it is hosted on production, in case you want to populate data on production, or running as a container in a particular network (using this network enabling your flyway to find your db container), in case you want to populate data on development. The development case is illustrated as follow:

user@root:~$  docker network create {network_name}

user@root:~$: docker run --network={network_name} --name langdb -e POSTGRES_DB=langchat -e POSTGRES_USER=langteam -e POSTGRES_PASSWORD=Aa1234 -d postgres

then populate your newly created database using:

user@root:~$ docker run --network={network_name} --env-file={path_to_env_file} {image_name}

2. Running using docker compose.

The docker compose definition as following is in case that I want to use database langchat on development environment. Make sure that you provide the environment variables as the guilding above.

version: '3'
services:
  flyway:
    image: narutosimaha/flyway-langchat
    environment:
      - DATABASE_DB=langchat
      - DATABASE_URL=jdbc:postgresql://langdb:5432
      - POSTGRES_USER=langteam
      - POSTGRES_PASSWORD=Aa1234
    depends_on:
      - langdb

  langdb:
    image: postgres
    environment:
      - POSTGRES_DB=langchat
      - POSTGRES_USER=langteam
      - POSTGRES_PASSWORD=Aa1234
    ports:
      - 5432:5432
    volumes:
      - langchat:/var/lib/postgresql/data
    restart: always

volumes:
  langchat:

Until now, you can access the datbase through port 5432 at localhost. You can custome the docker compose file for using multiple databases as you want.

3. Considerations

In practice, you should create docker compose that runs your app and it's dependent database. It makes sure that on the production, your data is applied before your app using it.