Skip to content

Commit

Permalink
add make command to generate db schemas (#1486)
Browse files Browse the repository at this point in the history
* add make command to generate db schemas
  • Loading branch information
SimoneDutto authored Dec 13, 2024
1 parent 3b226f0 commit b5144d3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ qa-lxd
/cloudinit.temp.yaml

local/traefik/certs/ca.srl

db_schemas
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions local/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
45 changes: 45 additions & 0 deletions local/jimm/generate_db_schemas.sh
Original file line number Diff line number Diff line change
@@ -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."

0 comments on commit b5144d3

Please sign in to comment.