Skip to content

env for contributors + auto update DB_ID in wrangler.toml#10

Open
Shrey1006 wants to merge 3 commits intoalphaonelabs:mainfrom
Shrey1006:dbIDnew
Open

env for contributors + auto update DB_ID in wrangler.toml#10
Shrey1006 wants to merge 3 commits intoalphaonelabs:mainfrom
Shrey1006:dbIDnew

Conversation

@Shrey1006
Copy link
Contributor

@Shrey1006 Shrey1006 commented Mar 23, 2026

This PR does the following things

  1. Added a separate dev environment for local development
  2. Contributors now run wrangler dev --env dev
  3. This ensures they use their own D1 database ID, not the production DB
  4. Production database_id remains unchanged and safe
  5. Maintainer can still run wrangler dev or wrangler dev --env dev normally
  6. For the maintainer, local and production DB IDs remain the same
  7. Prevents contributors from accidentally using or modifying production data
  8. Makes local development safer and cleaner for everyone
Env.for.contributors.mp4

Summary

This PR adds a dedicated local "dev" environment and an automated setup script so contributors use their own D1 database IDs instead of the production DB, reducing risk of accidental production access.

Changes

  • setup.sh (new, 57 lines)

    • Executable bootstrap script for local development.
    • Verifies node and wrangler are in PATH.
    • Runs wrangler d1 create education_db (or wrangler d1 list --json if it already exists) and extracts the database UUID for the entry named "education_db" using a Node JSON parser.
    • Replaces the first occurrence of the placeholder ADD_YOUR_LOCAL_DATABASE_ID_HERE in wrangler.toml with the extracted ID via sed -i "0,/.../s//${DB_ID}/".
    • Executes wrangler d1 execute education_db --file=schema.sql to apply the schema.
    • Prints a completion message and a hint to run wrangler dev.
  • wrangler.toml (+[env.dev])

    • Added [env.dev] named "learn-dev".
    • Added [[env.dev.d1_databases]] with binding = "DB", database_name = "education_db", database_id = "ADD_YOUR_LOCAL_DATABASE_ID_HERE".
    • Top-level D1 configuration remains unchanged and contains the production database_id (a0021f2e-a8cc-4e20-8910-3c7290ba47a6).

Impact

  • Contributors: Intended workflow is to run wrangler dev --env dev and use their own D1 instance; setup.sh automates inserting their local DB ID into the env.dev config to make local development safer.
  • Maintainers: Existing workflows unchanged; production DB ID remains protected in the top-level config.
  • Risk reduction: Prevents accidental use or modification of production data by introducing a separate dev environment and per-contributor DB binding.

Notes / Potential issues

  • setup.sh uses sed -i without macOS-compatible backup suffix; the in-place edit may fail on macOS as-is.
  • setup.sh prints a hint to run wrangler dev but contributors should use wrangler dev --env dev to target the dev environment added by this PR.

@coderabbitai
Copy link

coderabbitai bot commented Mar 23, 2026

Note

.coderabbit.yaml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

⚠️ Parsing warnings (1)
Validation error: Unrecognized key(s) in object: 'tools'
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Walkthrough

Adds a new end-to-end local bootstrap script setup.sh that verifies prerequisites, creates or finds a D1 database named education_db, extracts its UUID into wrangler.toml, and applies the schema; adds an [env.dev] section in wrangler.toml with a D1 binding placeholder.

Changes

Cohort / File(s) Summary
Setup Automation
setup.sh
New executable Bash script that checks for node and wrangler, attempts wrangler d1 create education_db (falls back to listing if it already exists), extracts the database uuid via a Node JSON one-liner, exits with an error if no UUID is found, replaces the first ADD_YOUR_LOCAL_DATABASE_ID_HERE in wrangler.toml using sed -i, and runs wrangler d1 execute education_db --file=schema.sql.
Wrangler Configuration
wrangler.toml
Added [env.dev] with name = "learn-dev" and an environment-scoped D1 binding [[env.dev.d1_databases]] for binding = "DB", database_name = "education_db", and placeholder database_id = "ADD_YOUR_LOCAL_DATABASE_ID_HERE".

Sequence Diagram(s)

sequenceDiagram
    participant Dev as Developer
    participant Script as setup.sh
    participant FS as wrangler.toml
    participant CLI as Wrangler CLI
    participant D1 as D1 Service

    Dev->>Script: run ./setup.sh
    Script->>Script: verify `node` and `wrangler` in PATH
    Script->>CLI: wrangler d1 create education_db
    alt create reports "already exists"
        CLI-->>Script: create output (exists)
        Script->>CLI: wrangler d1 list --json
    else created
        CLI-->>Script: create output (with UUID)
    end
    CLI-->>Script: JSON output containing DB entry
    Script->>Script: extract UUID with Node one-liner (or fail)
    Script->>FS: sed -i replace first ADD_YOUR_LOCAL_DATABASE_ID_HERE with UUID
    Script->>CLI: wrangler d1 execute education_db --file=schema.sql
    CLI->>D1: apply schema
    D1-->>CLI: success/failure
    CLI-->>Script: result
    Script-->>Dev: print completion and next steps
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main changes: adding a dev environment for contributors and automating the DB_ID update in wrangler.toml via the setup.sh script.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@setup.sh`:
- Around line 23-27: The current script sets OUTPUT when the DB already exists
(using OUTPUT and the wrangler d1 list command) but later still prints the
"Database created with ID" message; update the messaging logic so when the
branch detecting "already exists" is taken you print a different, accurate
message (e.g., "Database already exists with ID ...") instead of "Database
created with ID"—locate the code that prints the creation message and
conditionally change it based on the earlier check of OUTPUT or introduce a flag
(e.g., db_already_exists) set in the if-block and use that to select the correct
message.
- Around line 55-56: Update the final echo message in setup.sh so the run
instruction includes the environment flag; locate the echo that prints "Run:
wrangler dev" and change it to instruct contributors to run "wrangler dev --env
dev" (i.e., update the string in that echo statement to include --env dev) so
local development uses the dev database.
- Around line 29-32: The script contains a duplicated DB_ID extraction command:
two identical lines using DB_ID=$(echo "$OUTPUT" | grep -oE '[a-f0-9-]{36}' |
head -n 1); remove the redundant occurrence so only a single extraction remains
(keep the first or the logically placed one) and ensure subsequent references
still use DB_ID and that OUTPUT is defined earlier; update any nearby comments
to reflect the single extraction.
- Line 45: The sed command in setup.sh (the line modifying wrangler.toml using
DB_ID) is GNU-specific and breaks on macOS; update setup.sh to perform a
portable replacement by either using an awk one-liner to replace the first
occurrence of the placeholder "ADD_YOUR_LOCAL_DATABASE_ID_HERE" with ${DB_ID} in
wrangler.toml, or detect the OS (uname) and use GNU sed on Linux but use BSD sed
with the -i '' form (or fallback to awk) on macOS; ensure the change targets the
placeholder string in wrangler.toml and preserves the DB_ID variable expansion.
- Around line 49-53: The wrangler d1 execute call in setup.sh currently targets
the default environment and has no error handling; change the invocation of
wrangler d1 execute education_db --file=schema.sql to explicitly include --env
dev and add checks to ensure schema.sql exists and the command succeeds (e.g.,
test for the file before running, run wrangler with --env dev, and exit non‑zero
with an error log if the command fails) so the schema is applied to the local
dev DB and any failure aborts the setup.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: alphaonelabs/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 83a3f030-de13-4173-bb30-65eae64cea05

📥 Commits

Reviewing files that changed from the base of the PR and between ef97da0 and 0cdfc23.

📒 Files selected for processing (2)
  • setup.sh
  • wrangler.toml

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@setup.sh`:
- Line 65: The final echo in setup.sh contains a stray extra double-quote
causing a shell syntax error; edit the echo command (the line with echo "Run:
wrangler dev --env dev"") to remove the trailing quote so the string is properly
quoted (i.e., ensure the argument to echo is enclosed by a single matching pair
of quotes).
- Around line 43-51: The wrangler.toml update currently prints success
unconditionally; change the script around the sed replacement (the conditional
branch using OSTYPE, the sed commands that substitute
ADD_YOUR_LOCAL_DATABASE_ID_HERE with ${DB_ID}) to verify the replacement
actually occurred: after running sed, check its exit status and/or confirm the
file now contains ${DB_ID} (e.g., grep -q "${DB_ID}" wrangler.toml) and if
either check fails print a clear error and exit with non-zero status so setup
stops when wrangler.toml was not updated.
- Around line 26-30: The script currently extracts DB_ID by grepping the first
UUID from OUTPUT (variable OUTPUT from the wrangler d1 list call), which breaks
when multiple databases exist; instead, parse the wrangler d1 list JSON and
select the entry where name == "education_db" and extract its id (replace the
DB_ID assignment that uses grep/ head -n 1). Use jq (or the wrangler JSON flag)
to filter by .name == "education_db" and output the matching .id so the script
always gets the correct database id for education_db.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: alphaonelabs/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: efee344a-8357-4216-b9c1-fceb69c3bfc6

📥 Commits

Reviewing files that changed from the base of the PR and between 0cdfc23 and 5b48003.

📒 Files selected for processing (1)
  • setup.sh

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (2)
setup.sh (2)

53-58: ⚠️ Potential issue | 🟠 Major

Target the dev environment explicitly and fail on schema-apply errors.

For contributor safety, schema execution should use --env dev; also guard missing schema.sql and command failures. The final run instruction should match this (wrangler dev --env dev).

When `wrangler.toml` defines `[env.dev]` D1 settings, does `wrangler d1 execute` target default config unless `--env dev` is passed?
🔧 Suggested fix
 echo "Applying schema..."
-wrangler d1 execute education_db --file=schema.sql
+if [ ! -f "schema.sql" ]; then
+  echo "Error: schema.sql not found"
+  exit 1
+fi
+
+if ! wrangler d1 execute education_db --env dev --file=schema.sql; then
+  echo "Error: failed to apply schema"
+  exit 1
+fi
 
 echo "Schema applied"
 
 echo "Setup complete!"
-echo "Run: wrangler dev"
+echo "Run: wrangler dev --env dev"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@setup.sh` around lines 53 - 58, Ensure the D1 schema execution explicitly
targets the dev environment and fails on errors: update the `wrangler d1
execute` call to include `--env dev`, verify `schema.sql` exists before running
(and exit with non-zero status if missing), and check the command's exit status
so the script aborts on failure; also update the final instruction/echo to
recommend `wrangler dev --env dev`. Use the existing `wrangler d1 execute`,
`schema.sql`, and `wrangler dev` symbols to locate and modify the lines.

47-49: ⚠️ Potential issue | 🟠 Major

Use a portable replacement and verify the update actually happened.

This replacement is GNU-sed specific and can fail on macOS; success is currently printed even if no substitution occurred.

Does BSD sed (macOS) support `0,/pattern/` addressing and GNU-style `sed -i` usage used in this script?
🔧 Suggested cross-platform update
-sed -i "0,/ADD_YOUR_LOCAL_DATABASE_ID_HERE/s//${DB_ID}/" wrangler.toml
-
-echo "wrangler.toml updated"
+if [ ! -f "wrangler.toml" ]; then
+  echo "Error: wrangler.toml not found"
+  exit 1
+fi
+
+awk -v id="$DB_ID" '
+  !done && /ADD_YOUR_LOCAL_DATABASE_ID_HERE/ {
+    sub(/ADD_YOUR_LOCAL_DATABASE_ID_HERE/, id)
+    done=1
+  }
+  { print }
+' wrangler.toml > wrangler.toml.tmp && mv wrangler.toml.tmp wrangler.toml
+
+if ! grep -q "$DB_ID" wrangler.toml; then
+  echo "Error: failed to update database_id in wrangler.toml"
+  exit 1
+fi
+
+echo "wrangler.toml updated"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@setup.sh` around lines 47 - 49, The sed command replacing
ADD_YOUR_LOCAL_DATABASE_ID_HERE in wrangler.toml is GNU-only and currently
always prints success; change the script to perform a POSIX-portable replacement
(e.g., use perl -pi -e or sed -i.bak with a single-address pattern) targeting
the placeholder and refer to the DB_ID variable, then verify the update
succeeded by checking wrangler.toml contains the new ${DB_ID} (e.g., grep or
awk) and exit with a non-zero status and a clear error message if the
substitution did not occur; update the lines around the existing sed invocation
and the echo "wrangler.toml updated" to only run on success.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@setup.sh`:
- Around line 21-34: The script currently assigns OUTPUT from `wrangler d1
create education_db` and only falls back to `wrangler d1 list --json` when the
text contains "already exists", which can leave OUTPUT as non-JSON on successful
create and causes the DB_ID JSON parse to fail; change the flow so you always
run `wrangler d1 list --json` to obtain deterministic JSON for DB discovery (use
the existing OUTPUT/DB_ID variables), check the exit status of the `wrangler d1
create` call and any `wrangler d1 list --json` invocation and fail fast with an
error message if either command errors, then parse DB_ID from the guaranteed
JSON output (matching name === "education_db") and error out if no UUID is
found.

---

Duplicate comments:
In `@setup.sh`:
- Around line 53-58: Ensure the D1 schema execution explicitly targets the dev
environment and fails on errors: update the `wrangler d1 execute` call to
include `--env dev`, verify `schema.sql` exists before running (and exit with
non-zero status if missing), and check the command's exit status so the script
aborts on failure; also update the final instruction/echo to recommend `wrangler
dev --env dev`. Use the existing `wrangler d1 execute`, `schema.sql`, and
`wrangler dev` symbols to locate and modify the lines.
- Around line 47-49: The sed command replacing ADD_YOUR_LOCAL_DATABASE_ID_HERE
in wrangler.toml is GNU-only and currently always prints success; change the
script to perform a POSIX-portable replacement (e.g., use perl -pi -e or sed
-i.bak with a single-address pattern) targeting the placeholder and refer to the
DB_ID variable, then verify the update succeeded by checking wrangler.toml
contains the new ${DB_ID} (e.g., grep or awk) and exit with a non-zero status
and a clear error message if the substitution did not occur; update the lines
around the existing sed invocation and the echo "wrangler.toml updated" to only
run on success.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: alphaonelabs/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 97a79177-2938-432e-aeff-1068c49601d2

📥 Commits

Reviewing files that changed from the base of the PR and between 5b48003 and fbd35a9.

📒 Files selected for processing (1)
  • setup.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant