Skip to content

Commit ca0b421

Browse files
authored
Merge pull request #321 from lane711/new-local-db-check
feat: add npm run db:reset for worktree database setup
2 parents 62ae732 + b0a0842 commit ca0b421

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ npx create-sonicjs@latest my-sonicjs-app
9494
npm test
9595
```
9696

97+
#### Setting Up a Fresh Database
98+
99+
When working in a new worktree or wanting to reset your local database, run from the project root:
100+
101+
```bash
102+
# Create a fresh D1 database for your branch
103+
npm run db:reset
104+
```
105+
106+
This will:
107+
- Create a new D1 database named `sonicjs-worktree-<branch-name>`
108+
- Apply all migrations
109+
- Update `wrangler.toml` with the new database ID
110+
97111
#### Working with Database Migrations
98112

99113
When developing the core package, migrations are located in `packages/core/migrations/`. Your test app will reference these migrations through the npm workspace symlink.

my-sonicjs-app/scripts/setup-worktree-db.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,28 @@ fi
8181
echo "Updated wrangler.toml:"
8282
grep -A2 "d1_databases" wrangler.toml
8383

84-
# Run migrations
84+
# Reset local database by removing it
8585
echo ""
86-
echo "Running migrations..."
86+
echo "Resetting local database..."
87+
rm -rf .wrangler/state/v3/d1
88+
echo "Local database cleared."
89+
90+
# Run migrations on remote
91+
echo ""
92+
echo "Running migrations on remote database..."
8793
npx wrangler d1 migrations apply "$DB_NAME" --remote
8894

95+
# Run migrations on local
96+
echo ""
97+
echo "Running migrations on local database..."
98+
npx wrangler d1 migrations apply "$DB_NAME" --local
99+
89100
echo ""
90101
echo "=========================================="
91102
echo "Database setup complete!"
92103
echo "Database name: $DB_NAME"
93104
echo "Database ID: $DB_ID"
105+
echo "Both remote and local databases are ready."
94106
echo "=========================================="
95107
echo ""
96108
echo "You can now run: npm run dev"

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"publish:all": "npm run publish:core && npm run publish:create-app",
3434
"release:patch": "npm run version:patch && npm run publish:all",
3535
"release:minor": "npm run version:minor && npm run publish:all",
36-
"release:major": "npm run version:major && npm run publish:all"
36+
"release:major": "npm run version:major && npm run publish:all",
37+
"db:reset": "npm run setup:db --workspace=my-sonicjs-app"
3738
},
3839
"dependencies": {
3940
"@headlessui/react": "^2.2.9",

0 commit comments

Comments
 (0)