Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ jobs:
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
D1_DATABASE_ID: ${{ secrets.D1_DATABASE_ID }}

- name: Deploy to Cloudflare Pages
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
Expand Down
19 changes: 15 additions & 4 deletions scripts/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,35 @@
* Environment variables required:
* - CLOUDFLARE_API_TOKEN: Cloudflare API token with D1 permissions
* - CLOUDFLARE_ACCOUNT_ID: Your Cloudflare account ID
* - D1_DATABASE_ID: The D1 database ID (from wrangler.toml)
*
* The D1 database ID is read from wrangler.toml.
*/

import { readFileSync, readdirSync } from "fs";
import { join } from "path";

function getD1DatabaseId(): string {
const wranglerPath = join(process.cwd(), "wrangler.toml");
const content = readFileSync(wranglerPath, "utf-8");
const match = content.match(/database_id\s*=\s*"([^"]+)"/);
if (!match) {
throw new Error("Could not find database_id in wrangler.toml");
}
return match[1];
}

const CLOUDFLARE_API_TOKEN = process.env.CLOUDFLARE_API_TOKEN;
const CLOUDFLARE_ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID;
const D1_DATABASE_ID = process.env.D1_DATABASE_ID;

if (!CLOUDFLARE_API_TOKEN || !CLOUDFLARE_ACCOUNT_ID || !D1_DATABASE_ID) {
if (!CLOUDFLARE_API_TOKEN || !CLOUDFLARE_ACCOUNT_ID) {
console.error("❌ Missing required environment variables:");
if (!CLOUDFLARE_API_TOKEN) console.error(" - CLOUDFLARE_API_TOKEN");
if (!CLOUDFLARE_ACCOUNT_ID) console.error(" - CLOUDFLARE_ACCOUNT_ID");
if (!D1_DATABASE_ID) console.error(" - D1_DATABASE_ID");
process.exit(1);
}

const D1_DATABASE_ID = getD1DatabaseId();

const D1_API_BASE = `https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/d1/database/${D1_DATABASE_ID}`;

interface D1Result<T = unknown> {
Expand Down