|
| 1 | +'use server' |
| 2 | + |
| 3 | +/** |
| 4 | + * Database Server Actions |
| 5 | + * |
| 6 | + * Server Actions for database operations. Frontend components call these |
| 7 | + * to create and delete databases on-demand. |
| 8 | + */ |
| 9 | + |
| 10 | +import type { Database } from '@prisma/client' |
| 11 | + |
| 12 | +import { auth } from '@/lib/auth' |
| 13 | +import { prisma } from '@/lib/db' |
| 14 | +import { getK8sServiceForUser } from '@/lib/k8s/k8s-service-helper' |
| 15 | +import { KubernetesUtils } from '@/lib/k8s/kubernetes-utils' |
| 16 | +import { VERSIONS } from '@/lib/k8s/versions' |
| 17 | +import { logger as baseLogger } from '@/lib/logger' |
| 18 | + |
| 19 | +import type { ActionResult } from './types' |
| 20 | + |
| 21 | +const logger = baseLogger.child({ module: 'actions/database' }) |
| 22 | + |
| 23 | +/** |
| 24 | + * Create a database for an existing project |
| 25 | + * |
| 26 | + * @param projectId - Project ID |
| 27 | + * @param databaseName - Optional custom database name (auto-generated if not provided) |
| 28 | + */ |
| 29 | +export async function createDatabase( |
| 30 | + projectId: string, |
| 31 | + databaseName?: string |
| 32 | +): Promise<ActionResult<Database>> { |
| 33 | + const session = await auth() |
| 34 | + if (!session) { |
| 35 | + return { success: false, error: 'Unauthorized' } |
| 36 | + } |
| 37 | + |
| 38 | + // Verify project exists and belongs to user |
| 39 | + const project = await prisma.project.findUnique({ |
| 40 | + where: { id: projectId }, |
| 41 | + include: { databases: true }, |
| 42 | + }) |
| 43 | + |
| 44 | + if (!project) { |
| 45 | + return { success: false, error: 'Project not found' } |
| 46 | + } |
| 47 | + |
| 48 | + if (project.userId !== session.user.id) { |
| 49 | + return { success: false, error: 'Unauthorized' } |
| 50 | + } |
| 51 | + |
| 52 | + // Check if database already exists |
| 53 | + if (project.databases.length > 0) { |
| 54 | + return { success: false, error: 'Database already exists for this project' } |
| 55 | + } |
| 56 | + |
| 57 | + // Get K8s service for user |
| 58 | + let k8sService |
| 59 | + let namespace |
| 60 | + try { |
| 61 | + k8sService = await getK8sServiceForUser(session.user.id) |
| 62 | + namespace = k8sService.getDefaultNamespace() |
| 63 | + } catch (error) { |
| 64 | + if (error instanceof Error && error.message.includes('does not have KUBECONFIG configured')) { |
| 65 | + return { |
| 66 | + success: false, |
| 67 | + error: 'Please configure your kubeconfig before creating a database', |
| 68 | + } |
| 69 | + } |
| 70 | + throw error |
| 71 | + } |
| 72 | + |
| 73 | + // Generate database name if not provided |
| 74 | + const k8sProjectName = KubernetesUtils.toK8sProjectName(project.name) |
| 75 | + const randomSuffix = KubernetesUtils.generateRandomString() |
| 76 | + const finalDatabaseName = databaseName || `${k8sProjectName}-db-${randomSuffix}` |
| 77 | + |
| 78 | + // Create Database record |
| 79 | + const database = await prisma.database.create({ |
| 80 | + data: { |
| 81 | + projectId: project.id, |
| 82 | + name: finalDatabaseName, |
| 83 | + k8sNamespace: namespace, |
| 84 | + databaseName: finalDatabaseName, |
| 85 | + status: 'CREATING', |
| 86 | + lockedUntil: null, |
| 87 | + storageSize: VERSIONS.STORAGE.DATABASE_SIZE, |
| 88 | + cpuRequest: VERSIONS.RESOURCES.DATABASE.requests.cpu, |
| 89 | + cpuLimit: VERSIONS.RESOURCES.DATABASE.limits.cpu, |
| 90 | + memoryRequest: VERSIONS.RESOURCES.DATABASE.requests.memory, |
| 91 | + memoryLimit: VERSIONS.RESOURCES.DATABASE.limits.memory, |
| 92 | + }, |
| 93 | + }) |
| 94 | + |
| 95 | + logger.info(`Database created: ${database.id} for project: ${project.id}`) |
| 96 | + |
| 97 | + return { success: true, data: database } |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Delete a database |
| 102 | + * |
| 103 | + * @param databaseId - Database ID |
| 104 | + */ |
| 105 | +export async function deleteDatabase(databaseId: string): Promise<ActionResult<void>> { |
| 106 | + const session = await auth() |
| 107 | + if (!session) { |
| 108 | + return { success: false, error: 'Unauthorized' } |
| 109 | + } |
| 110 | + |
| 111 | + // Verify database exists and belongs to user |
| 112 | + const database = await prisma.database.findUnique({ |
| 113 | + where: { id: databaseId }, |
| 114 | + include: { project: true }, |
| 115 | + }) |
| 116 | + |
| 117 | + if (!database) { |
| 118 | + return { success: false, error: 'Database not found' } |
| 119 | + } |
| 120 | + |
| 121 | + if (database.project.userId !== session.user.id) { |
| 122 | + return { success: false, error: 'Unauthorized' } |
| 123 | + } |
| 124 | + |
| 125 | + // Update status to TERMINATING (reconciliation job will handle K8s deletion) |
| 126 | + await prisma.database.update({ |
| 127 | + where: { id: databaseId }, |
| 128 | + data: { |
| 129 | + status: 'TERMINATING', |
| 130 | + lockedUntil: null, // Unlock for reconciliation job |
| 131 | + }, |
| 132 | + }) |
| 133 | + |
| 134 | + logger.info(`Database ${databaseId} marked for deletion`) |
| 135 | + |
| 136 | + return { success: true, data: undefined } |
| 137 | +} |
0 commit comments