env for contributors + auto update DB_ID in wrangler.toml#10
env for contributors + auto update DB_ID in wrangler.toml#10Shrey1006 wants to merge 3 commits intoalphaonelabs:mainfrom
Conversation
|
Note
|
| 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
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.
Comment @coderabbitai help to get the list of available commands and usage tips.
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
setup.shwrangler.toml
There was a problem hiding this comment.
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
📒 Files selected for processing (1)
setup.sh
There was a problem hiding this comment.
Actionable comments posted: 1
♻️ Duplicate comments (2)
setup.sh (2)
53-58:⚠️ Potential issue | 🟠 MajorTarget the dev environment explicitly and fail on schema-apply errors.
For contributor safety, schema execution should use
--env dev; also guard missingschema.sqland 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 | 🟠 MajorUse 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
📒 Files selected for processing (1)
setup.sh
This PR does the following things
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)
wrangler d1 create education_db(orwrangler d1 list --jsonif it already exists) and extracts the database UUID for the entry named "education_db" using a Node JSON parser.sed -i "0,/.../s//${DB_ID}/".wrangler d1 execute education_db --file=schema.sqlto apply the schema.wrangler dev.wrangler.toml (+[env.dev])
Impact
wrangler dev --env devand use their own D1 instance; setup.sh automates inserting their local DB ID into the env.dev config to make local development safer.Notes / Potential issues
sed -iwithout macOS-compatible backup suffix; the in-place edit may fail on macOS as-is.wrangler devbut contributors should usewrangler dev --env devto target the dev environment added by this PR.