From 0cdfc23c082a820e5bf078dd9b31832344490f65 Mon Sep 17 00:00:00 2001 From: shreycodes Date: Mon, 23 Mar 2026 00:19:01 +0530 Subject: [PATCH 1/3] env for contributors + auto update DB_ID in wrangler.toml --- setup.sh | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ wrangler.toml | 6 ++++++ 2 files changed, 62 insertions(+) create mode 100644 setup.sh diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000..0c990d5 --- /dev/null +++ b/setup.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +echo "Setting up project..." + +# Step 1: Check Node & Wrangler +if ! command -v node &> /dev/null +then + echo "Node.js not found. Please install Node.js." + exit 1 +fi + +if ! command -v wrangler &> /dev/null +then + echo "Wrangler not found. Install using: npm install -g wrangler" + exit 1 +fi + +# Step 2: Create D1 Database +echo "Checking/Creating D1 database..." + +OUTPUT=$(wrangler d1 create education_db 2>&1) + +if echo "$OUTPUT" | grep -q "already exists"; then + echo "Database already exists. Fetching existing ID..." + + OUTPUT=$(wrangler d1 list) +fi + +DB_ID=$(echo "$OUTPUT" | grep -oE '[a-f0-9-]{36}' | head -n 1) + +# Extract database_id +DB_ID=$(echo "$OUTPUT" | grep -oE '[a-f0-9-]{36}' | head -n 1) + +if [ -z "$DB_ID" ]; then + echo "Failed to extract database_id" + echo "$OUTPUT" + exit 1 +fi + +echo "Database created with ID: $DB_ID" + +# Step 3: Update wrangler.toml +echo "Updating wrangler.toml..." + +sed -i "0,/ADD_YOUR_LOCAL_DATABASE_ID_HERE/s//${DB_ID}/" wrangler.toml + +echo "wrangler.toml updated" + +# Step 4: Apply schema +echo "Applying schema..." +wrangler d1 execute education_db --file=schema.sql + +echo "Schema applied" + +echo "Setup complete!" +echo "Run: wrangler dev" \ No newline at end of file diff --git a/wrangler.toml b/wrangler.toml index c281b20..1db31bb 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -17,6 +17,12 @@ binding = "DB" database_name = "education_db" database_id = "a0021f2e-a8cc-4e20-8910-3c7290ba47a6" +[env.dev] +name = "learn-dev" +[[env.dev.d1_databases]] +binding = "DB" +database_name = "education_db" +database_id = "ADD_YOUR_LOCAL_DATABASE_ID_HERE" [observability] enabled = false From 5b4800330a632db375047f9140409a74685096e2 Mon Sep 17 00:00:00 2001 From: shreycodes Date: Tue, 24 Mar 2026 00:02:17 +0530 Subject: [PATCH 2/3] Resolved conversion --- setup.sh | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/setup.sh b/setup.sh index 0c990d5..864b00b 100644 --- a/setup.sh +++ b/setup.sh @@ -26,8 +26,6 @@ if echo "$OUTPUT" | grep -q "already exists"; then OUTPUT=$(wrangler d1 list) fi -DB_ID=$(echo "$OUTPUT" | grep -oE '[a-f0-9-]{36}' | head -n 1) - # Extract database_id DB_ID=$(echo "$OUTPUT" | grep -oE '[a-f0-9-]{36}' | head -n 1) @@ -37,20 +35,31 @@ if [ -z "$DB_ID" ]; then exit 1 fi -echo "Database created with ID: $DB_ID" +echo "Using Database ID: $DB_ID" # Step 3: Update wrangler.toml echo "Updating wrangler.toml..." -sed -i "0,/ADD_YOUR_LOCAL_DATABASE_ID_HERE/s//${DB_ID}/" wrangler.toml +if [[ "$OSTYPE" == "darwin"* ]]; then + # macOS BSD sed + sed -i '' "s/ADD_YOUR_LOCAL_DATABASE_ID_HERE/${DB_ID}/" wrangler.toml +else + # GNU sed (Linux) + sed -i "0,/ADD_YOUR_LOCAL_DATABASE_ID_HERE/s//${DB_ID}/" wrangler.toml +fi echo "wrangler.toml updated" # Step 4: Apply schema echo "Applying schema..." -wrangler d1 execute education_db --file=schema.sql +wrangler d1 execute education_db --file=schema.sql --env dev + +if [ $? -ne 0 ]; then + echo "Failed to apply schema." + exit 1 +fi echo "Schema applied" echo "Setup complete!" -echo "Run: wrangler dev" \ No newline at end of file +echo "Run: wrangler dev --env dev"" \ No newline at end of file From fbd35a9674731ab28527eb4a4810758152b3fa6e Mon Sep 17 00:00:00 2001 From: shreycodes Date: Tue, 24 Mar 2026 00:20:54 +0530 Subject: [PATCH 3/3] resolved conversation 2 --- setup.sh | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/setup.sh b/setup.sh index 864b00b..f1f7bca 100644 --- a/setup.sh +++ b/setup.sh @@ -23,11 +23,15 @@ OUTPUT=$(wrangler d1 create education_db 2>&1) if echo "$OUTPUT" | grep -q "already exists"; then echo "Database already exists. Fetching existing ID..." - OUTPUT=$(wrangler d1 list) + OUTPUT=$(wrangler d1 list --json) fi -# Extract database_id -DB_ID=$(echo "$OUTPUT" | grep -oE '[a-f0-9-]{36}' | head -n 1) +# Extract database_id using name-based selection +DB_ID=$(echo "$OUTPUT" | node -e ' + const data = JSON.parse(require("fs").readFileSync(0, "utf8")); + const db = data.find(d => d.name === "education_db"); + if (db) process.stdout.write(db.uuid || ""); +') if [ -z "$DB_ID" ]; then echo "Failed to extract database_id" @@ -35,31 +39,20 @@ if [ -z "$DB_ID" ]; then exit 1 fi -echo "Using Database ID: $DB_ID" +echo "Database ID: $DB_ID" # Step 3: Update wrangler.toml echo "Updating wrangler.toml..." -if [[ "$OSTYPE" == "darwin"* ]]; then - # macOS BSD sed - sed -i '' "s/ADD_YOUR_LOCAL_DATABASE_ID_HERE/${DB_ID}/" wrangler.toml -else - # GNU sed (Linux) - sed -i "0,/ADD_YOUR_LOCAL_DATABASE_ID_HERE/s//${DB_ID}/" wrangler.toml -fi +sed -i "0,/ADD_YOUR_LOCAL_DATABASE_ID_HERE/s//${DB_ID}/" wrangler.toml echo "wrangler.toml updated" # Step 4: Apply schema echo "Applying schema..." -wrangler d1 execute education_db --file=schema.sql --env dev - -if [ $? -ne 0 ]; then - echo "Failed to apply schema." - exit 1 -fi +wrangler d1 execute education_db --file=schema.sql echo "Schema applied" echo "Setup complete!" -echo "Run: wrangler dev --env dev"" \ No newline at end of file +echo "Run: wrangler dev" \ No newline at end of file