Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update sql schema autogen tool #331

Merged
merged 12 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .config/dictionaries/project.dic
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ rustflags
rustfmt
rustup
sanchonet
schemaspy
sqlfluff
stdcfgs
subproject
Expand All @@ -120,4 +121,5 @@ WORKDIR
xerrors
xvfb
zstd
zstdcat
zstdcat
JDBC
15 changes: 15 additions & 0 deletions earthly/java/Earthfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
VERSION 0.8

# Base image for Java used in other targets to avoid improve caching
java-base:
FROM openjdk:21-jdk-slim

SAVE ARTIFACT /usr/local/openjdk-21 /java

COPY_DEPS:
FUNCTION
COPY +java-base/java /usr/local/openjdk-21

# Set environment variables for Java
ENV JAVA_HOME=/usr/local/openjdk-21
ENV PATH=$JAVA_HOME/bin:$PATH
50 changes: 33 additions & 17 deletions earthly/postgresql/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
VERSION 0.8

IMPORT ../rust/tools AS rust-tools
IMPORT ../../utilities/dbviz AS dbviz
IMPORT ../../utilities/scripts AS scripts
IMPORT ../java AS java

# cspell: words psycopg dbviz
# cspell: words psycopg

postgres-base:
FROM postgres:16.4-bookworm

ARG SCHEMASPY_VERSION=6.2.4
ARG POSTGRESQL_JDBC_VERSION=42.7.4

WORKDIR /root

# Install necessary packages
Expand All @@ -22,7 +25,7 @@ postgres-base:
colordiff \
findutils \
fontconfig \
fonts-liberation2 \
fonts-liberation2 \
graphviz \
libssl-dev \
mold \
Expand All @@ -34,19 +37,17 @@ postgres-base:
ripgrep \
wget \
pipx \
&& rm -rf /var/lib/apt/lists/*

&& rm -rf /var/lib/apt/lists/* \
&& wget -O /bin/schemaspy.jar https://github.com/schemaspy/schemaspy/releases/download/v${SCHEMASPY_VERSION}/schemaspy-${SCHEMASPY_VERSION}.jar \
stevenj marked this conversation as resolved.
Show resolved Hide resolved
&& wget -O /bin/postgresql.jar https://jdbc.postgresql.org/download/postgresql-${POSTGRESQL_JDBC_VERSION}.jar

# Install SQLFluff - Check
ENV PATH="/root/.local/bin:${PATH}"
RUN pipx install sqlfluff==3.1.1 && sqlfluff version

# Get refinery
COPY rust-tools+tool-refinery/refinery /bin

# Get dbviz
stevenj marked this conversation as resolved.
Show resolved Hide resolved
COPY dbviz+build/dbviz /bin
RUN dbviz --help

# Copy our set SQL files
COPY --dir sql /sql

Expand All @@ -56,7 +57,14 @@ postgres-base:
DO scripts+ADD_BASH_SCRIPTS
DO scripts+ADD_PYTHON_SCRIPTS

# Copy templates to the working directory
COPY --dir templates /templates

# Use the cached java installation from the java Earthfile
DO java+COPY_DEPS

SAVE ARTIFACT /scripts /scripts
SAVE ARTIFACT /templates /templates

# Common build setup steps.
# Arguments:
Expand All @@ -82,8 +90,13 @@ BUILDER:

# DOCS - FUNCTION to build the docs, needs to be run INSIDE the BUILDER like so:
#
# 1. Create a ./docs/diagrams.json which has the options needed to run to generate the docs to /docs
# 2. Define the following targets in your earthfile
# This function uses SchemaSpy to generate database documentation.
# SchemaSpy creates detailed, Discoverable ER diagrams and schema documentation.
#
# To use this function:
# 1. Ensure your migrations are in the ./migrations directory
# 2. Have a refinery.toml file to configure the migrations
# 3. Define the following targets in your earthfile:
#
# builder:
# DO github.com/input-output-hk/catalyst-ci/earthly/postgresql:<ver>+BUILDER --sqlfluff_cfg=./../../+repo-config/repo/.sqlfluff
Expand All @@ -93,25 +106,27 @@ BUILDER:
#
# DO github.com/input-output-hk/catalyst-ci/earthly/postgresql:<ver>+BUILD --image_name=<name>
# DO github.com/input-output-hk/catalyst-ci/earthly/postgresql:<ver>+DOCS
#
# The generated documentation will be saved in the ./docs artifact.
DOCS:
FUNCTION

ARG diagrams=./diagrams.json
ARG migrations=./migrations
ARG refinery_toml=./refinery.toml

FROM +postgres-base

stevenj marked this conversation as resolved.
Show resolved Hide resolved
USER postgres:postgres
WORKDIR /docs

COPY $diagrams ./diagrams.json
COPY --dir $migrations .
COPY --dir $refinery_toml .

RUN /scripts/std_docs.py ./diagrams.json

SAVE ARTIFACT docs /docs

RUN /scripts/std_docs.py
# Pull templates artifact from postgres-base
COPY +postgres-base/templates/schema.md ./docs/schema.md

SAVE ARTIFACT docs ./docs

# Linter checks for sql files
CHECK:
Expand Down Expand Up @@ -171,3 +186,4 @@ BUILD:
# Push the container...
SAVE IMAGE ${image_name}:latest


Loading