Skip to content

Instantly share code, notes, and snippets.

@amitavaghosh1
Forked from vfarcic/093-schemahero.sh
Created September 1, 2022 08:22
Show Gist options
  • Save amitavaghosh1/d1f8b1cc0f73b3f66f0876d5dae847aa to your computer and use it in GitHub Desktop.
Save amitavaghosh1/d1f8b1cc0f73b3f66f0876d5dae847aa to your computer and use it in GitHub Desktop.

Revisions

  1. @vfarcic vfarcic revised this gist Jun 23, 2021. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions 093-schemahero.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Source: https://gist.github.com/17f57ba2a076bcd5335424575f55d6bd

    ##########################################################
    # How to do database schema migration inside Kubernetes? #
    # https://youtu.be/SofQxb4CDQQ #
    @@ -42,9 +44,9 @@ cat db-orig.yaml \

    kubectl krew install schemahero

    ####################
    # Setup SchemaHero #
    ####################
    #########################
    # Installing SchemaHero #
    #########################

    kubectl schemahero install

  2. @vfarcic vfarcic created this gist Jun 23, 2021.
    163 changes: 163 additions & 0 deletions 093-schemahero.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,163 @@
    ##########################################################
    # How to do database schema migration inside Kubernetes? #
    # https://youtu.be/SofQxb4CDQQ #
    ##########################################################

    # Referenced videos:
    # - K3d - How to run Kubernetes cluster locally using Rancher k3s: https://youtu.be/mCesuGk-Fks
    # - Argo CD - Applying GitOps Principles To Manage Production Environment In Kubernetes: https://youtu.be/vpWQeoaiRM4
    # - Flux CD v2 With GitOps Toolkit - Kubernetes Deployment And Sync Mechanism (Second Review): https://youtu.be/R6OeIgb7lUI
    # - Bitnami Sealed Secrets - How To Store Kubernetes Secrets In Git Repositories: https://youtu.be/xd2QoV6GJlc

    #########
    # Setup #
    #########

    git clone https://github.com/vfarcic/schemahero-demo

    cd schemahero-demo

    # Please watch https://youtu.be/mCesuGk-Fks if you are not familiar with k3d.
    # It could be any other Kubernetes cluster. It does not have to be k3d.
    k3d cluster create --config k3d.yaml

    helm repo add bitnami \
    https://charts.bitnami.com/bitnami

    helm upgrade --install \
    postgresql bitnami/postgresql \
    --namespace postgresql \
    --create-namespace \
    --wait

    export PGPASSWORD=$(\
    kubectl --namespace postgresql \
    get secret postgresql \
    --output jsonpath="{.data.postgresql-password}" \
    | base64 --decode)

    cat db-orig.yaml \
    | sed -e "s@password@$PGPASSWORD@g" \
    | tee db.yaml

    kubectl krew install schemahero

    ####################
    # Setup SchemaHero #
    ####################

    kubectl schemahero install

    kubectl --namespace postgresql \
    run postgresql-client \
    --rm -ti --restart='Never' \
    --image docker.io/bitnami/postgresql:11.12.0-debian-10-r23 \
    --env PGPASSWORD=$PGPASSWORD \
    --command -- psql --host postgresql -U postgres -d postgres -p 5432

    \l

    CREATE DATABASE devopstoolkit;

    \l

    exit

    ###################################
    # Configuring database connection #
    ###################################

    # Open https://schemahero.io/databases/

    cat db.yaml

    kubectl --namespace postgresql \
    apply --filename db.yaml

    kubectl --namespace postgresql get databases

    ###################
    # Creating tables #
    ###################

    cat table.yaml

    kubectl --namespace postgresql \
    apply --filename table.yaml

    kubectl --namespace postgresql get tables

    kubectl schemahero \
    --namespace postgresql \
    get migrations

    # Replace `[...]` with the migration ID
    kubectl schemahero \
    --namespace postgresql \
    describe migration [...]

    kubectl --namespace postgresql \
    run postgresql-client \
    --rm -ti --restart='Never' \
    --image docker.io/bitnami/postgresql:11.12.0-debian-10-r23 \
    --env PGPASSWORD=$PGPASSWORD \
    --command -- psql --host postgresql -U postgres -d devopstoolkit -p 5432

    \d videos

    exit

    ###################
    # Altering tables #
    ###################

    cat table2.yaml

    diff table.yaml table2.yaml

    kubectl --namespace postgresql \
    apply --filename table2.yaml

    kubectl schemahero \
    --namespace postgresql \
    get migrations

    # Replace `[...]` with the migration ID
    kubectl schemahero \
    --namespace postgresql \
    describe migration [...]

    kubectl --namespace postgresql \
    run postgresql-client \
    --rm -ti --restart='Never' \
    --image docker.io/bitnami/postgresql:11.12.0-debian-10-r23 \
    --env PGPASSWORD=$PGPASSWORD \
    --command -- psql --host postgresql -U postgres -d devopstoolkit -p 5432

    \d videos

    exit

    ##############################
    # Rolling back table changes #
    ##############################

    kubectl --namespace postgresql \
    apply --filename table.yaml

    kubectl --namespace postgresql \
    run postgresql-client \
    --rm -ti --restart='Never' \
    --image docker.io/bitnami/postgresql:11.12.0-debian-10-r23 \
    --env PGPASSWORD=$PGPASSWORD \
    --command -- psql --host postgresql -U postgres -d devopstoolkit -p 5432

    \d videos

    exit

    ###########
    # Destroy #
    ###########

    k3d cluster delete devops-toolkit