From b5144d3f148beb1e1e5da6e940d5628d07e13fa2 Mon Sep 17 00:00:00 2001 From: SimoneDutto Date: Fri, 13 Dec 2024 10:56:13 +0100 Subject: [PATCH] add make command to generate db schemas (#1486) * add make command to generate db schemas --- .gitignore | 2 ++ Makefile | 4 +++ local/README.md | 1 + local/jimm/generate_db_schemas.sh | 45 +++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100755 local/jimm/generate_db_schemas.sh diff --git a/.gitignore b/.gitignore index 84d8a8902..0522f2d2c 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ qa-lxd /cloudinit.temp.yaml local/traefik/certs/ca.srl + +db_schemas diff --git a/Makefile b/Makefile index d1f05b7cc..eac0577b5 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,10 @@ lint: check: version/commit.txt version/version.txt lint go test -timeout 30m $(PROJECT)/... -cover +# generates database schemas locally to inspect them. +generate-schemas: + @./local/jimm/generate_db_schemas.sh + clean: go clean $(PROJECT)/... -$(RM) version/commit.txt version/version.txt diff --git a/local/README.md b/local/README.md index ef8c22909..f130f0d3c 100644 --- a/local/README.md +++ b/local/README.md @@ -74,3 +74,4 @@ controllers that will be controlled by JIMM. - The WS API for JIMM Controller is under: `ws://localhost:17070` (http direct) and `wss://jimm.localhost` for secure. - You can verify local deployment with: `curl http://localhost:17070/debug/status` and `curl https://jimm.localhost/debug/status` - Traefik is available on `http://localhost:8089`. +- You can generate db schemas from the running deployment postgres to inspect the raw sql by using `make generate-schemas`. diff --git a/local/jimm/generate_db_schemas.sh b/local/jimm/generate_db_schemas.sh new file mode 100755 index 000000000..236948e9c --- /dev/null +++ b/local/jimm/generate_db_schemas.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +# This script generates database schemas from our running postgres deployment docker compose. +# Its purpose is to better inspect raw SQL generating our db models. + +# Configuration +DB_CONTAINER="postgres" +SCHEMA="public" +OUTPUT_DIR="./db_schemas" + +# Create output directory +mkdir -p $OUTPUT_DIR + +# Get a list of tables in the specified schema, excluding system tables +TABLES=$(docker exec -i $DB_CONTAINER sh -c " + psql -U \$POSTGRES_USER -d \$POSTGRES_DB -t -c \" + SELECT tablename + FROM pg_tables + WHERE schemaname = '$SCHEMA' AND tablename NOT LIKE 'pg_%' AND tablename != 'information_schema'; + \"" +) + +# Loop through each table and dump its schema +for TABLE in $TABLES; do + TABLE=$(echo $TABLE | xargs) # Trim whitespace + if [[ ! -z "$TABLE" ]]; then + echo "Extracting schema for table: $TABLE" + docker exec -i $DB_CONTAINER sh -c " + pg_dump -U \$POSTGRES_USER --schema-only --table=$SCHEMA.$TABLE \$POSTGRES_DB + " | sed -E ' + # Remove the SET paragraph + /SET /,/^$/d + # Remove SEQUENCE paragraph + /SEQUENCE/,/^$/d + # Remove comments (lines starting with --) + /^--/d + ' | awk ' + # Replace multiple newlines with a single newline + BEGIN {RS=""; ORS="\n\n"} + {print $0} + ' > "$OUTPUT_DIR/${TABLE}_schema.sql" + fi +done + +echo "Schemas extracted to $OUTPUT_DIR."