Last active
September 1, 2022 08:22
-
-
Save vfarcic/17f57ba2a076bcd5335424575f55d6bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Source: https://gist.github.com/17f57ba2a076bcd5335424575f55d6bd | |
########################################################## | |
# 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 | |
######################### | |
# Installing 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment