Code Monkey home page Code Monkey logo

webapp-db's Introduction

๐Ÿ—„๏ธ Database Migrations

Flyway is a database migration tool. We will use the flyway migration scripts that will migrate our database from our local workstation to a container running an init container, which will run the migration scripts before starting our REST API.

We will work with PostgreSQL ^14 as our primary database, so we would need to setup our local environment accordingly.

โฌ Local PostgreSQL setup

NOTE: The steps mentioned below are only for MacOS. For other distros, please refer to the official Postgresql documentation.

  • Install postgres locally:
brew install postgresql@15
# validate installation
psql --version
  • After postgres is installed, we need the postgres service up and running:
brew service start postgres@15
  • Let's start by connecting to the default postgres database:
psql postgres
  • Create a role in postgres and assign permissions:
CREATE ROLE <user> WITH LOGIN PASSWORD <user_password>;
ALTER ROLE <user> CREATEDB;
# if required, add other permissions as well: REPLICATION, CREATEROLE, etc
\du # to list all users and permissions
\q # quit postgres cli
  • Connect to postgres db using new role created, create and connect to a new database:
# connect to db using new user
psql -d postgres -U <user>
# create a database
CREATE DATABASE <db_name>;
\c <db_name> # connect to new db

๐Ÿšš Working with Flyway

NOTE: The steps mentioned below are only for MacOS. For other distros, please refer to the official Flyway documentation.

  • Install flyway locally:

    brew install flyway
    # verify installation
    flyway version
  • Creating migration scripts: Migration scripts ending with .sql are the scripts used to migrate/setup our database using flyway for SQL based migrations. These should be placed in a folder called sql, with proper naming conventions as mentioned in the documentation.

  • Create a flyway.conf migration configuration file: This file contains the database connection URL, which connects to our database instance/service. It also contains the username and password for the database we connect to and run the migrations on. For more details on the configuration file, refer this documentation. Alternatively, you can also follow the example.flyway.conf file for configuration options.

  • Using environment variables: Since we will use the kubernetes Secret resource to configure our flyway environment variables, we just need to follow the naming convention for the variables in the flyway.conf configuration file:

    flyway.url=${DB_FLYWAY_CONNECTION_URL}
    flyway.user=${DB_FLYWAY_USERNAME}
    flyway.password=${DB_FLYWAY_PASSWORD}
  • To run migrations locally on a postgres service, we can configure the migration options in the flyway.conf file and run the migrate command.

    flyway migrate

NOTE: If the migrations are already performed, or there are no migrations to be performed, flyway returns a no-op.

๐Ÿณ Containerize migrations

Here, we containerize the migration scripts and configuration files into a docker image. Since this image will run within an initContainer in a k8s deployment, this docker image does not need to run any CMD to run the migrations (since this will be done within the initContainer). A .dockerignore file is also needed to prevent copying files and folders that are not required in the final build image.

webapp-db's People

Contributors

karanwadhwa avatar rishabneu avatar semantic-release-bot avatar sydrawat01 avatar

webapp-db's Issues

๐Ÿ“ Update docs

Update the README.md file to include instructions on how to install and work with flyway as a database migration tool.

๐Ÿž Flyway db migration location fails

The db migration location fails to install the schema and create a table within that schema because the default flyway.locations is not assumed by default in Flyway v10.0.1-alpine.

We need to add the flyway.locations manually and set it to the default setting, pointing it to the install directory.

flyway.filesystem:<<INSTALL-DIR>>/sql

๐Ÿชฐ Fix Dockerfile, Update flyway.conf

  • In Dockerfile.dev, the COPY command fails to copy all files in the sql/ directory into a new sql/ directory in the Docker image. Needs to be fixed to copy all contents of sql/ into a ${WORKDIR}/sql.
  • The flyway.conf file contains hard-coded values that configure and connect to our local postgres service.
    These values are to be read via the initContainer environment variables.
  • Update the migration scripts to include DDL SQL statements that CREATE, ALTER, or DROP database objects such as Tables, Views, Indexes, etc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.