fix: change wrangler d1 usage
This commit is contained in:
parent
3401cfe524
commit
00f714e60d
2 changed files with 47 additions and 19 deletions
36
.github/workflows/backup-d1.yaml
vendored
36
.github/workflows/backup-d1.yaml
vendored
|
|
@ -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
|
||||
|
||||
|
|
|
|||
30
README.md
30
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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue