Skip to content

Commit 25272b1

Browse files
DennisAlundCopilot
andcommitted
Replace runtime migrations with standard wrangler deploy flow
The deploy script in package.json now runs wrangler d1 migrations apply before wrangler deploy. Cloudflare auto-detects this for one-click deploy and Workers Builds. Removed src/migrate.ts and its tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e440d1c commit 25272b1

6 files changed

Lines changed: 19 additions & 258 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 0.4.0
4+
5+
- Replaced runtime migration system with Cloudflare's standard `wrangler d1 migrations apply` during deploy
6+
- The `deploy` script in `package.json` now runs migrations before deploying, which Cloudflare auto-detects for one-click deploy and Workers Builds
7+
- Migration command references the D1 binding name (`DB`) instead of the database name, so it works regardless of what each user names their database
8+
- Removed `src/migrate.ts`: no application-level migration code runs at request time
9+
310
## 0.3.4
411

512
- Automatic database migrations: the Worker applies pending schema changes on cold start, no CLI commands or dashboard configuration needed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
### One-click
2222

23-
Click the **Deploy to Cloudflare** button above. It will fork the repo, create a D1 database, and deploy the Worker. Database migrations apply automatically on startup.
23+
Click the **Deploy to Cloudflare** button above. Cloudflare will fork the repo, provision a D1 database, apply schema migrations, and deploy the Worker. No manual database setup required.
2424

2525
After deploying, set up authentication (see below).
2626

@@ -31,10 +31,14 @@ git clone https://github.com/oddbit/shrtnr
3131
cd shrtnr
3232
yarn install
3333
yarn wrangler-login
34-
yarn db:migrate
35-
yarn deploy # auto-provisions the D1 database on first deploy
34+
yarn db:create
35+
yarn deploy # applies migrations and deploys the Worker
3636
```
3737

38+
### Continuous deployment
39+
40+
The deploy button sets up [Workers Builds](https://developers.cloudflare.com/workers/ci-cd/builds/) automatically. Cloudflare detects the `deploy` script in `package.json` and uses it as the deploy command. Each push to your production branch applies pending D1 migrations and redeploys the Worker.
41+
3842
## Authentication
3943

4044
shrtnr uses [Cloudflare Access](https://developers.cloudflare.com/cloudflare-one/applications/) to protect the admin UI. Access handles login, sessions, and identity — the Worker itself has zero auth code.
@@ -106,6 +110,7 @@ Administrative and internal endpoints are intentionally not documented here.
106110

107111
```bash
108112
yarn install
113+
yarn db:migrate # apply migrations to local D1
109114
yarn test
110115
yarn dev
111116
```

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"name": "shrtnr",
3-
"version": "0.3.4",
3+
"version": "0.4.0",
44
"description": "A free, open-source, self-hosted URL shortener built on Cloudflare Workers + D1",
55
"license": "Apache-2.0",
66
"scripts": {
77
"dev": "wrangler dev",
8-
"deploy": "wrangler deploy",
8+
"deploy": "npm run db:migrate:remote && wrangler deploy",
99
"wrangler-login": "wrangler login",
1010
"db:create": "wrangler d1 create shrtnr-db",
11-
"db:migrate": "wrangler d1 migrations apply shrtnr-db",
12-
"db:migrate:local": "wrangler d1 migrations apply shrtnr-db --local",
11+
"db:migrate": "wrangler d1 migrations apply shrtnr-db --local",
12+
"db:migrate:remote": "wrangler d1 migrations apply DB --remote",
1313
"secret:put": "wrangler secret put",
1414
"test": "vitest run",
1515
"test:watch": "vitest"

src/__tests__/migrate.test.ts

Lines changed: 0 additions & 110 deletions
This file was deleted.

src/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,15 @@ import { handleDashboardStats, handleLinkAnalytics } from "./api/analytics";
1515
import { serveAdminUI } from "./admin/ui";
1616
import { serveAsset } from "./assets";
1717
import { notFoundResponse } from "./404";
18-
import { applyMigrations } from "./migrate";
1918

2019
type AuthContext = {
2120
email: string;
2221
source: "access" | "apikey";
2322
scope: string | null; // null = full access (admin), "create" | "read" | "create,read"
2423
};
2524

26-
let migrated = false;
27-
2825
export default {
2926
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
30-
if (!migrated) {
31-
await applyMigrations(env.DB);
32-
migrated = true;
33-
}
34-
3527
const url = new URL(request.url);
3628
const path = url.pathname;
3729

src/migrate.ts

Lines changed: 0 additions & 133 deletions
This file was deleted.

0 commit comments

Comments
 (0)