Skip to content

secondsky/claude-skills / plugins/cloudflare-d1/skills/cloudflare-d1

cloudflare-d1

Verified

Cloudflare D1 serverless SQLite on edge. Use for databases, migrations, bindings, or encountering D1_ERROR, statement too long, too many requests queued errors.

templates/d1-setup-migration.sh

View on GitHub
#!/bin/bash
#
# Cloudflare D1 Setup and Migration Workflow
#
# This script demonstrates the complete D1 workflow:
# 1. Create a D1 database
# 2. Configure bindings
# 3. Create and apply migrations
# 4. Query the database
#
# Usage:
#   chmod +x d1-setup-migration.sh
#   ./d1-setup-migration.sh my-app-database
#

set -e  # Exit on error

DATABASE_NAME="${1:-my-database}"

echo "========================================="
echo "Cloudflare D1 Setup and Migration"
echo "========================================="
echo ""

# Step 1: Create D1 Database
echo "πŸ“¦ Step 1: Creating D1 database '$DATABASE_NAME'..."
echo ""

npx wrangler d1 create "$DATABASE_NAME"

echo ""
echo "βœ… Database created!"
echo ""
echo "πŸ“ IMPORTANT: Copy the output above and add to your wrangler.jsonc:"
echo ""
echo '  {
    "d1_databases": [
      {
        "binding": "DB",
        "database_name": "'"$DATABASE_NAME"'",
        "database_id": "<UUID_FROM_OUTPUT_ABOVE>",
        "preview_database_id": "local-dev-db"
      }
    ]
  }'
echo ""
read -p "Press ENTER when you've added the binding to wrangler.jsonc..."

# Step 2: Create Migrations Directory
echo ""
echo "πŸ“ Step 2: Setting up migrations directory..."
mkdir -p migrations

# Step 3: Create Initial Migration
echo ""
echo "πŸ”¨ Step 3: Creating initial migration..."
echo ""

npx wrangler d1 migrations create "$DATABASE_NAME" create_initial_schema

# Find the created migration file (most recent .sql file in migrations/)
MIGRATION_FILE=$(ls -t migrations/*.sql | head -n1)

echo ""
echo "βœ… Migration file created: $MIGRATION_FILE"
echo ""
echo "πŸ“ Add your schema to this file. Example:"
echo ""
echo "  DROP TABLE IF EXISTS users;
  CREATE TABLE IF NOT EXISTS users (
    user_id INTEGER PRIMARY KEY AUTOINCREMENT,
    email TEXT NOT NULL UNIQUE,
    username TEXT NOT NULL,
    created_at INTEGER NOT NULL,
    updated_at INTEGER
  );

  CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);

  PRAGMA optimize;"
echo ""
read -p "Press ENTER when you've edited the migration file..."

# Step 4: Apply Migration Locally
echo ""
echo "πŸ”§ Step 4: Applying migration to LOCAL database..."
echo ""

npx wrangler d1 migrations apply "$DATABASE_NAME" --local

echo ""
echo "βœ… Local migration applied!"

# Step 5: Verify Local Database
echo ""
echo "πŸ” Step 5: Verifying local database..."
echo ""

npx wrangler d1 execute "$DATABASE_NAME" --local --command "SELECT name FROM sqlite_master WHERE type='table'"

# Step 6: Seed Local Database (Optional)
echo ""
echo "🌱 Step 6: Would you like to seed the local database with test data?"
read -p "Seed database? (y/n): " -n 1 -r
echo ""

if [[ $REPLY =~ ^[Yy]$ ]]; then
  echo "Creating seed data..."

  cat > seed.sql << 'EOF'
-- Seed data for testing
INSERT INTO users (email, username, created_at) VALUES
  ('alice@example.com', 'alice', unixepoch()),
  ('bob@example.com', 'bob', unixepoch()),
  ('charlie@example.com', 'charlie', unixepoch());
EOF

  npx wrangler d1 execute "$DATABASE_NAME" --local --file=seed.sql

  echo ""
  echo "βœ… Seed data inserted!"
  echo ""
  echo "πŸ” Verifying data..."
  npx wrangler d1 execute "$DATABASE_NAME" --local --command "SELECT * FROM users"
fi

# Step 7: Apply to Production (Optional)
echo ""
echo "πŸš€ Step 7: Ready to apply migration to PRODUCTION?"
echo ""
echo "⚠️  WARNING: This will modify your production database!"
read -p "Apply to production? (y/n): " -n 1 -r
echo ""

if [[ $REPLY =~ ^[Yy]$ ]]; then
  echo "Applying migration to production..."
  npx wrangler d1 migrations apply "$DATABASE_NAME" --remote

  echo ""
  echo "βœ… Production migration applied!"
else
  echo "Skipping production migration."
  echo ""
  echo "To apply later, run:"
  echo "  npx wrangler d1 migrations apply $DATABASE_NAME --remote"
fi

# Summary
echo ""
echo "========================================="
echo "βœ… D1 Setup Complete!"
echo "========================================="
echo ""
echo "Database: $DATABASE_NAME"
echo "Local database: βœ…"
echo "Migrations: βœ…"
echo ""
echo "πŸ“š Next steps:"
echo ""
echo "1. Start dev server:"
echo "   npm run dev"
echo ""
echo "2. Query from your Worker:"
echo '   const user = await env.DB.prepare("SELECT * FROM users WHERE email = ?")
     .bind("alice@example.com")
     .first();'
echo ""
echo "3. Create more migrations as needed:"
echo "   npx wrangler d1 migrations create $DATABASE_NAME <migration_name>"
echo ""
echo "4. View all tables:"
echo "   npx wrangler d1 execute $DATABASE_NAME --local --command \"SELECT name FROM sqlite_master WHERE type='table'\""
echo ""
echo "========================================="
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall