diff --git a/.github/scripts/create-sample-db.sh b/.github/scripts/create-sample-db.sh new file mode 100755 index 0000000..75f2428 --- /dev/null +++ b/.github/scripts/create-sample-db.sh @@ -0,0 +1,39 @@ +#!/bin/bash +# Create a sample SQLite database from the hadiscover models for schema diagram generation + +set -e + +echo "Creating sample database from models..." + +# Get the script directory and project root +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" +BACKEND_DIR="$PROJECT_ROOT/backend" + +# Install backend dependencies using the shared script +echo "Installing backend dependencies..." +cd "$PROJECT_ROOT" +.github/scripts/install-backend-deps.sh + +# Create a temporary Python script to generate the database +cat > /tmp/create_db.py << 'EOF' +"""Generate sample SQLite database from SQLAlchemy models.""" +from app.models.database import Base +from sqlalchemy import create_engine + +# Create engine and database +engine = create_engine('sqlite:///sample_schema.db') +Base.metadata.create_all(engine) + +print("Sample database created successfully at backend/sample_schema.db") +EOF + +# Set PYTHONPATH and run the script +cd "$BACKEND_DIR" +export PYTHONPATH="$BACKEND_DIR:$PYTHONPATH" +python /tmp/create_db.py + +# Clean up +rm /tmp/create_db.py + +echo "Database creation complete." diff --git a/.github/workflows/schema-diagram.yml b/.github/workflows/schema-diagram.yml new file mode 100644 index 0000000..cc2dee2 --- /dev/null +++ b/.github/workflows/schema-diagram.yml @@ -0,0 +1,61 @@ +--- +name: Generate Database Schema Diagram + +# yamllint disable-line rule:truthy +on: + workflow_dispatch: null + push: + branches: + - main + paths: + - "backend/app/models/**" + - ".github/workflows/schema-diagram.yml" + - ".github/scripts/create-sample-db.sh" + +permissions: + contents: read + +jobs: + generate-schema-diagram: + name: Generate Schema Diagram + runs-on: ubuntu-latest + + steps: + - name: Checkout code + # v6 + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd + with: + persist-credentials: false + + - name: Setup Python + # v6 + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 + with: + python-version: "3.14" + cache: "pip" + cache-dependency-path: backend/requirements.txt + + - name: Create sample database + run: .github/scripts/create-sample-db.sh + + - name: Generate schema diagram + # v17.6.0 + uses: schemacrawler/SchemaCrawler-Action@v17.6.0 + with: + entrypoint: /schemacrawler.sh + args: >- + --server=sqlite + --database=backend/sample_schema.db + --info-level=maximum + --command=schema + --output-format=png + --output-file=schema-diagram.png + + - name: Upload schema diagram + # v4 + uses: >- + actions/upload-artifact@c0d66db0b43eb92e5f6e4fc5f8f67c6d8c1e2d10 + with: + name: database-schema-diagram + path: schema-diagram.png + retention-days: 90 diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 9bdc9ea..2daa4ad 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -79,6 +79,8 @@ SQLite Database ←→ GitHub API ## Database Schema +A visual schema diagram is automatically generated on each push to main when models change. Download it from the workflow artifacts at [Actions > Generate Database Schema Diagram](../../actions/workflows/schema-diagram.yml). + ### repositories ```sql