Skip to content

Commit 61a9026

Browse files
authored
refactor: introduce platform control commands (#149)
1 parent 51833cb commit 61a9026

19 files changed

Lines changed: 469 additions & 329 deletions

docs/architecture-evolution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ Typical examples:
189189
- task prerequisite rules
190190
- lifecycle transition rules
191191

192-
### `lib/control/`
192+
### `lib/platform/control/`
193193

194194
Should contain:
195195

lib/actions/database.ts

Lines changed: 11 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
import type { Database } from '@prisma/client'
1111

1212
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'
13+
import {
14+
createDatabaseCommand,
15+
deleteDatabaseCommand,
16+
} from '@/lib/platform/control/commands/database'
1817

1918
import type { ActionResult } from './types'
2019

21-
const logger = baseLogger.child({ module: 'actions/database' })
22-
2320
/**
2421
* Create a database for an existing project
2522
*
@@ -35,66 +32,11 @@ export async function createDatabase(
3532
return { success: false, error: 'Unauthorized' }
3633
}
3734

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-
},
35+
return createDatabaseCommand({
36+
userId: session.user.id,
37+
projectId,
38+
databaseName,
9339
})
94-
95-
logger.info(`Database created: ${database.id} for project: ${project.id}`)
96-
97-
return { success: true, data: database }
9840
}
9941

10042
/**
@@ -108,30 +50,8 @@ export async function deleteDatabase(databaseId: string): Promise<ActionResult<v
10850
return { success: false, error: 'Unauthorized' }
10951
}
11052

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-
},
53+
return deleteDatabaseCommand({
54+
userId: session.user.id,
55+
databaseId,
13256
})
133-
134-
logger.info(`Database ${databaseId} marked for deletion`)
135-
136-
return { success: true, data: undefined }
13757
}

0 commit comments

Comments
 (0)