refactor: Update database migration scripts and enhance CI workflow

This commit is contained in:
beilunyang
2024-12-16 13:02:40 +08:00
parent cc7e5003c5
commit e0bd04818e
5 changed files with 566 additions and 138 deletions

View File

@@ -33,6 +33,22 @@ jobs:
- name: Install Dependencies
run: pnpm install --frozen-lockfile
# Check if database migrations have changes
- name: Check migrations changes
id: check_migrations
run: |
# If this is the first tag, we need to run migrations
if [ -z "${{ steps.previoustag.outputs.tag }}" ]; then
echo "migrations_changed=true" >> $GITHUB_OUTPUT
else
# Check if any files in drizzle directory have changed
if git diff ${{ steps.previoustag.outputs.tag }}..HEAD --name-only | grep -q "^drizzle/"; then
echo "migrations_changed=true" >> $GITHUB_OUTPUT
else
echo "migrations_changed=false" >> $GITHUB_OUTPUT
fi
fi
# Process configuration files
- name: Process configuration files
run: |
@@ -57,6 +73,14 @@ jobs:
sed -i "s/database_id = \".*\"/database_id = \"${{ secrets.DATABASE_ID }}\"/" wrangler.cleanup.toml
fi
# Run database migrations if needed
- name: Run database migrations
if: steps.check_migrations.outputs.migrations_changed == 'true'
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: pnpm db:migrate-remote
# Check if workers have changes
- name: Check workers changes
id: check_changes

View File

@@ -79,7 +79,7 @@ cp wrangler.example.toml wrangler.toml
cp wrangler.email.example.toml wrangler.email.toml
cp wrangler.cleanup.example.toml wrangler.cleanup.toml
```
设置 Cloudflare D1 数据库名以及数据库 ID
设置 Cloudflare D1 数据库名以及数据库 ID
4. 设置环境变量:
```bash

View File

@@ -8,8 +8,8 @@
"start": "next start",
"lint": "next lint",
"build:pages": "npx @cloudflare/next-on-pages",
"db:migrate-local": "drizzle-kit generate && wrangler d1 migrations apply temp_mail_db --local",
"db:migrate-remote": "drizzle-kit generate && wrangler d1 migrations apply temp_mail_db --remote",
"db:migrate-local": "tsx scripts/migrate.ts local",
"db:migrate-remote": "tsx scripts/migrate.ts remote",
"generate-test-data": "wrangler dev scripts/generate-test-data.ts",
"dev:cleanup": "wrangler dev --config wrangler.cleanup.toml --test-scheduled",
"test:cleanup": "curl http://localhost:8787/__scheduled",
@@ -55,6 +55,8 @@
"tailwindcss": "^3.4.1",
"typescript": "^5",
"vercel": "39.1.1",
"wrangler": "^3.91.0"
"wrangler": "^3.91.0",
"@iarna/toml": "^3.0.0",
"tsx": "^4.7.1"
}
}

603
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

67
scripts/migrate.ts Normal file
View File

@@ -0,0 +1,67 @@
import { parse } from '@iarna/toml'
import { readFileSync } from 'fs'
import { exec } from 'child_process'
import { promisify } from 'util'
import { join } from 'path'
const execAsync = promisify(exec)
interface D1Database {
binding: string
database_name: string
database_id: string
}
interface WranglerConfig {
d1_databases: D1Database[]
}
async function migrate() {
try {
// Get command line arguments
const args = process.argv.slice(2)
const mode = args[0]
if (!mode || !['local', 'remote'].includes(mode)) {
console.error('Error: Please specify mode (local or remote)')
process.exit(1)
}
// Read wrangler.toml
const wranglerPath = join(process.cwd(), 'wrangler.toml')
let wranglerContent: string
try {
wranglerContent = readFileSync(wranglerPath, 'utf-8')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (error) {
console.error('Error: wrangler.toml not found')
process.exit(1)
}
// Parse wrangler.toml
const config = parse(wranglerContent) as unknown as WranglerConfig
if (!config.d1_databases?.[0]?.database_name) {
console.error('Error: Database name not found in wrangler.toml')
process.exit(1)
}
const dbName = config.d1_databases[0].database_name
// Generate migrations
console.log('Generating migrations...')
await execAsync('drizzle-kit generate')
// Apply migrations
console.log(`Applying migrations to ${mode} database: ${dbName}`)
await execAsync(`wrangler d1 migrations apply ${dbName} --${mode}`)
console.log('Migration completed successfully!')
} catch (error) {
console.error('Migration failed:', error)
process.exit(1)
}
}
migrate()