Skip to content

Commit 58e1fb3

Browse files
fix: delete associated scripts when removing server (#145)
- Update database foreign key constraint to ON DELETE CASCADE - Add deleteInstalledScriptsByServer method to DatabaseService - Update server DELETE API to remove associated scripts before server deletion - Add confirmation modal with type-to-confirm for server deletion - Require users to type server name to confirm deletion - Show warning about deleting associated scripts in confirmation modal
1 parent 546d729 commit 58e1fb3

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/app/_components/ServerList.tsx

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useState } from 'react';
44
import type { Server, CreateServerData } from '../../types/server';
55
import { ServerForm } from './ServerForm';
66
import { Button } from './ui/button';
7+
import { ConfirmationModal } from './ConfirmationModal';
78

89
interface ServerListProps {
910
servers: Server[];
@@ -15,6 +16,14 @@ export function ServerList({ servers, onUpdate, onDelete }: ServerListProps) {
1516
const [editingId, setEditingId] = useState<number | null>(null);
1617
const [testingConnections, setTestingConnections] = useState<Set<number>>(new Set());
1718
const [connectionResults, setConnectionResults] = useState<Map<number, { success: boolean; message: string }>>(new Map());
19+
const [confirmationModal, setConfirmationModal] = useState<{
20+
isOpen: boolean;
21+
variant: 'danger';
22+
title: string;
23+
message: string;
24+
confirmText: string;
25+
onConfirm: () => void;
26+
} | null>(null);
1827

1928
const handleEdit = (server: Server) => {
2029
setEditingId(server.id);
@@ -32,9 +41,20 @@ export function ServerList({ servers, onUpdate, onDelete }: ServerListProps) {
3241
};
3342

3443
const handleDelete = (id: number) => {
35-
if (window.confirm('Are you sure you want to delete this server configuration?')) {
36-
onDelete(id);
37-
}
44+
const server = servers.find(s => s.id === id);
45+
if (!server) return;
46+
47+
setConfirmationModal({
48+
isOpen: true,
49+
variant: 'danger',
50+
title: 'Delete Server',
51+
message: `This will permanently delete the server configuration "${server.name}" (${server.ip}) and all associated installed scripts. This action cannot be undone!`,
52+
confirmText: server.name,
53+
onConfirm: () => {
54+
onDelete(id);
55+
setConfirmationModal(null);
56+
}
57+
});
3858
};
3959

4060
const handleTestConnection = async (server: Server) => {
@@ -228,6 +248,21 @@ export function ServerList({ servers, onUpdate, onDelete }: ServerListProps) {
228248
)}
229249
</div>
230250
))}
251+
252+
{/* Confirmation Modal */}
253+
{confirmationModal && (
254+
<ConfirmationModal
255+
isOpen={confirmationModal.isOpen}
256+
onClose={() => setConfirmationModal(null)}
257+
onConfirm={confirmationModal.onConfirm}
258+
title={confirmationModal.title}
259+
message={confirmationModal.message}
260+
variant={confirmationModal.variant}
261+
confirmText={confirmationModal.confirmText}
262+
confirmButtonText="Delete Server"
263+
cancelButtonText="Cancel"
264+
/>
265+
)}
231266
</div>
232267
);
233268
}

src/app/api/servers/[id]/route.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ export async function DELETE(
173173
);
174174
}
175175

176+
// Delete all installed scripts associated with this server
177+
db.deleteInstalledScriptsByServer(id);
178+
176179
const result = db.deleteServer(id);
177180

178181
return NextResponse.json(

src/server/database.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class DatabaseService {
9090
installation_date DATETIME DEFAULT CURRENT_TIMESTAMP,
9191
status TEXT NOT NULL CHECK(status IN ('in_progress', 'success', 'failed')),
9292
output_log TEXT,
93-
FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE SET NULL
93+
FOREIGN KEY (server_id) REFERENCES servers(id) ON DELETE CASCADE
9494
)
9595
`);
9696

@@ -276,6 +276,14 @@ class DatabaseService {
276276
return stmt.run(id);
277277
}
278278

279+
/**
280+
* @param {number} server_id
281+
*/
282+
deleteInstalledScriptsByServer(server_id) {
283+
const stmt = this.db.prepare('DELETE FROM installed_scripts WHERE server_id = ?');
284+
return stmt.run(server_id);
285+
}
286+
279287
close() {
280288
this.db.close();
281289
}

0 commit comments

Comments
 (0)