Update DB #30
This file contains hidden or 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
| name: Update DB | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| sql_path: | |
| description: "Path to SQL file committed in repo" | |
| required: true | |
| permissions: | |
| contents: write | |
| jobs: | |
| update: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repo | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 | |
| - name: Install sqlite3 + gzip | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y sqlite3 gzip | |
| - name: Validate sql_path | |
| run: | | |
| set -euo pipefail | |
| SQL_PATH="${{ github.event.inputs.sql_path }}" | |
| case "$SQL_PATH" in | |
| pending-sql/*.sql) ;; | |
| *) echo "Invalid sql_path: $SQL_PATH"; exit 1 ;; | |
| esac | |
| test -f "$SQL_PATH" | |
| echo "SQL_PATH=$SQL_PATH" >> $GITHUB_ENV | |
| - name: Prepare DB (accept gz or plain, always output plain sqlite) | |
| run: | | |
| set -euo pipefail | |
| test -f ./public/CollecTF.db.gz | |
| # Queremos un archivo sqlite plano en ./public/CollecTF.db | |
| if gzip -t ./public/CollecTF.db.gz 2>/dev/null; then | |
| echo "DB is valid gzip, unzipping to plain sqlite..." | |
| gunzip -c ./public/CollecTF.db.gz > ./public/CollecTF.db | |
| else | |
| echo "DB is NOT gzip. Treating ./public/CollecTF.db.gz as plain sqlite..." | |
| cp ./public/CollecTF.db.gz ./public/CollecTF.db | |
| fi | |
| # (Opcional) sanity check: sqlite header | |
| head -c 16 ./public/CollecTF.db | strings || true | |
| - name: Run SQL from file | |
| run: | | |
| set -euo pipefail | |
| echo "Using SQL file: $SQL_PATH" | |
| sqlite3 ./public/CollecTF.db ".read $SQL_PATH" | |
| - name: Store updated DB as .gz filename (but keep it plain sqlite) | |
| run: | | |
| set -euo pipefail | |
| # Tu front espera CollecTF.db.gz, pero lo quiere como sqlite directo. | |
| # Por eso NO comprimimos: renombramos el sqlite plano al nombre .gz. | |
| mv -f ./public/CollecTF.db ./public/CollecTF.db.gz | |
| - name: Delete SQL file after use | |
| run: | | |
| set -euo pipefail | |
| rm -f "$SQL_PATH" | |
| - name: Commit updated database + cleanup sql file | |
| env: | |
| TOKEN: ${{ secrets.BOT_TOKEN }} | |
| run: | | |
| set -euo pipefail | |
| git config user.name "github-actions" | |
| git config user.email "github-actions@github.com" | |
| git add ./public/CollecTF.db.gz | |
| git add -u | |
| if git diff --cached --quiet; then | |
| echo "No changes to commit." | |
| exit 0 | |
| fi | |
| git commit -m "Update database via workflow_dispatch" | |
| git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }} | |
| git push origin main |