From 00f714e60d8ed7ad2db20561af9196fbb2a1866c Mon Sep 17 00:00:00 2001 From: qaz741wsd856 Date: Sat, 29 Nov 2025 05:49:28 +0000 Subject: [PATCH] fix: change wrangler d1 usage --- .github/workflows/backup-d1.yaml | 36 ++++++++++++++++++++++++++++---- README.md | 30 +++++++++++++------------- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/.github/workflows/backup-d1.yaml b/.github/workflows/backup-d1.yaml index f8c8c8f..5594b43 100644 --- a/.github/workflows/backup-d1.yaml +++ b/.github/workflows/backup-d1.yaml @@ -32,13 +32,27 @@ jobs: - name: Install wrangler run: npm install -g wrangler + - name: Get D1 Database Name + id: db_name + env: + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + run: | + DB_NAME=$(npx wrangler d1 list --json | jq -r '.[] | select(.uuid == "${{ secrets.D1_DATABASE_ID }}") | .name') + if [ -z "$DB_NAME" ]; then + echo "❌ Error: Could not find database with D1_DATABASE_ID secret" + exit 1 + fi + echo "Found database name: $DB_NAME" + echo "db_name=$DB_NAME" >> $GITHUB_OUTPUT + - name: Export D1 Database env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} run: | - echo "Exporting D1 database..." - npx wrangler d1 export ${{ secrets.D1_DATABASE_ID }} \ + echo "Exporting D1 database: ${{ steps.db_name.outputs.db_name }}..." + npx wrangler d1 export "${{ steps.db_name.outputs.db_name }}" \ --remote \ --output=backup.sql @@ -133,13 +147,27 @@ jobs: - name: Install wrangler run: npm install -g wrangler + - name: Get D1 Database Name (Dev) + id: db_name + env: + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + run: | + DB_NAME=$(npx wrangler d1 list --json | jq -r '.[] | select(.uuid == "${{ secrets.D1_DATABASE_ID_DEV }}") | .name') + if [ -z "$DB_NAME" ]; then + echo "❌ Error: Could not find database with D1_DATABASE_ID_DEV secret + exit 1 + fi + echo "Found database name: $DB_NAME" + echo "db_name=$DB_NAME" >> $GITHUB_OUTPUT + - name: Export D1 Database (Dev) env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} run: | - echo "Exporting D1 dev database..." - npx wrangler d1 export ${{ secrets.D1_DATABASE_ID_DEV }} \ + echo "Exporting D1 dev database: ${{ steps.db_name.outputs.db_name }}..." + npx wrangler d1 export "${{ steps.db_name.outputs.db_name }}" \ --remote \ --output=backup.sql diff --git a/README.md b/README.md index 4bd62ce..bb922a0 100644 --- a/README.md +++ b/README.md @@ -223,27 +223,27 @@ To restore your D1 database from a backup: 4. **Restore to Cloudflare D1:** + First, find your database name using wrangler: + ```bash + wrangler d1 list + ``` + + This will show a table with your databases. Look for the `name` column (e.g., `warden-db` for production or `warden-dev` for dev). + + Then restore the backup: + + ```bash + # Replace DATABASE_NAME with your actual database name (e.g., warden-db) + # First, you may want to clear the existing database (optional, use with caution!) - # wrangler d1 execute vault1 --remote --command "DELETE FROM ciphers; DELETE FROM folders; DELETE FROM users;" + # wrangler d1 execute DATABASE_NAME --remote --command "DELETE FROM ciphers; DELETE FROM folders; DELETE FROM users;" # Import the backup - wrangler d1 execute vault1 --remote --file=backup.sql + wrangler d1 execute DATABASE_NAME --remote --file=backup.sql ``` - > **Note:** The `--remote` flag is required to execute against your production D1 database. Without it, the command will run against the local development database. - - **Alternative: Using Database ID directly** - - If you don't have `vault1` binding configured in your local `wrangler.toml`, you can use the database ID (UUID) directly instead of the binding name: - - ```bash - # Replace YOUR_DATABASE_ID with your actual D1 database UUID - # wrangler d1 execute YOUR_DATABASE_ID --remote --command "DELETE FROM ciphers; DELETE FROM folders; DELETE FROM users;" - wrangler d1 execute YOUR_DATABASE_ID --remote --file=backup.sql - ``` - - You can find your database ID in the Cloudflare dashboard under Storage & databases > D1 SQL database, or by running `wrangler d1 list`. + > **Note:** The `--remote` flag is required to execute against your production D1 database. Without it, the command will run against the local development database. ### Local Development with D1