-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add make command to generate db schemas (#1486)
* add make command to generate db schemas
- Loading branch information
1 parent
3b226f0
commit b5144d3
Showing
4 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ qa-lxd | |
/cloudinit.temp.yaml | ||
|
||
local/traefik/certs/ca.srl | ||
|
||
db_schemas |
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
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
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
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." |