Files
aiworker-backend/src/db/migrate.ts
Hector Ros 5672127593
All checks were successful
Build and Push Backend / build (push) Successful in 5s
Implement Backend API, MCP Server, and Gitea integration
- Add REST API routes for projects, tasks, and agents (CRUD operations)
- Implement MCP Server with 4 core tools:
  - get_next_task: Assign tasks to agents
  - update_task_status: Update task states
  - create_branch: Create Git branches via Gitea API
  - create_pull_request: Create PRs via Gitea API
- Add Gitea API client for repository operations
- Fix database migration error handling for existing tables
- Connect all routes to Bun.serve() main server

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
2026-01-20 00:43:46 +01:00

55 lines
1.4 KiB
TypeScript

/**
* Database Migrations Runner
* Runs automatically on app startup
*/
import { drizzle } from 'drizzle-orm/mysql2'
import { migrate } from 'drizzle-orm/mysql2/migrator'
import mysql from 'mysql2/promise'
export async function runMigrations() {
console.log('🔄 Running database migrations...')
const connection = await mysql.createConnection({
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT || '3306'),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
})
const db = drizzle(connection)
try {
await migrate(db, { migrationsFolder: './drizzle/migrations' })
console.log('✅ Migrations completed successfully')
await connection.end()
return true
} catch (error: any) {
// If table already exists, it's not a fatal error
const errorMessage = error?.message || String(error)
const errorCode = error?.code || error?.cause?.code
const errorErrno = error?.errno || error?.cause?.errno
if (
errorCode === 'ER_TABLE_EXISTS_ERROR' ||
errorErrno === 1050 ||
errorMessage.includes('already exists')
) {
console.log('⚠️ Tables already exist, skipping migrations')
await connection.end()
return true
}
console.error('❌ Migration failed:', error)
await connection.end()
throw error
}
}
// Run if executed directly
if (import.meta.main) {
await runMigrations()
process.exit(0)
}