Skip to content

Instantly share code, notes, and snippets.

@vfarcic
Created December 7, 2022 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfarcic/54b5994500501789ceac715486977901 to your computer and use it in GitHub Desktop.
Save vfarcic/54b5994500501789ceac715486977901 to your computer and use it in GitHub Desktop.
# Source: https://gist.github.com/54b5994500501789ceac715486977901
#######################################################
# How To Inspect, Plan, Migrate DB Schemas With Atlas #
# https://youtu.be/JLvHpXJ1hHk #
#######################################################
# Additional Info:
# - Atlas: https://atlasgo.io
# - SchemaHero - Database Schema Migrations Inside Kubernetes: https://youtu.be/SofQxb4CDQQ
#########
# Setup #
#########
git clone https://github.com/vfarcic/atlas-demo
cd atlas-demo
# You can skip the command that follows if you already have a
# PostgreSQL DB running.
# If you do skip it, you might need to change the addresses in
# the rest of the commands.
# Make sure that Docker is running
docker container run --name my-db \
--publish 5432:5432 --env POSTGRES_PASSWORD=admin \
--detach postgres
docker exec -it my-db psql -U postgres
CREATE DATABASE demo;
\connect demo
CREATE TABLE videos (
id text PRIMARY KEY,
title text NOT NULL
);
exit
# Install `atlas` CLI from https://atlasgo.io/getting-started/
##############################################
# Declarative Database Migrations With Atlas #
##############################################
atlas schema inspect \
--url "postgres://postgres:admin@localhost:5432/demo?sslmode=disable" \
| tee schema.hcl
diff schema.hcl schema-comments.hcl
atlas schema apply \
--url "postgres://postgres:admin@localhost:5432/demo?sslmode=disable" \
--file schema-comments.hcl --dry-run
atlas schema apply \
--url "postgres://postgres:admin@localhost:5432/demo?sslmode=disable" \
--file schema-comments.hcl --auto-approve
# Comment a column
atlas schema apply \
--url "postgres://postgres:admin@localhost:5432/demo?sslmode=disable" \
--file schema-comments.hcl
# Comment the column
atlas schema apply \
--url "postgres://postgres:admin@localhost:5432/demo?sslmode=disable" \
--file schema-comments.hcl --auto-approve
###########
# Destroy #
###########
docker container rm my-db --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment