Skip to content

Commit 8efa1dc

Browse files
committed
deletion functionality
1 parent 46b09dc commit 8efa1dc

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use server";
2+
3+
import { getServerUser } from "@/lib/auth";
4+
import { getProjectForUser } from "@/lib/dal/projects";
5+
import { revalidateQueryCache } from "@/lib/dal/queries";
6+
import { deleteComputedColumn } from "@/db/crud/query";
7+
8+
export async function deleteComputedColumnAction(
9+
projectId: string,
10+
queryId: string,
11+
columnName: string,
12+
): Promise<void> {
13+
const user = await getServerUser();
14+
const userProject = await getProjectForUser(user, projectId);
15+
16+
if (!userProject) {
17+
throw new Error("Project not found");
18+
}
19+
20+
await deleteComputedColumn(queryId, columnName);
21+
22+
revalidateQueryCache(queryId, projectId);
23+
}

packages/app/src/components/data/query/query-sidebar.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { autocompletion } from "@codemirror/autocomplete";
1111
import { EditorState } from "@codemirror/state";
1212
import { addComputedColumnAction } from "@/actions/data/queries/addComputedColumn";
1313
import { updateComputedColumnAction } from "@/actions/data/queries/updateComputedColumn";
14+
import { deleteComputedColumnAction } from "@/actions/data/queries/deleteComputedColumn";
1415
import { ComputedColumn } from "@common/db/schema/query";
1516

1617
type QuerySidebarProps = {
@@ -245,6 +246,27 @@ export function QuerySidebar({
245246
}
246247
};
247248

249+
const handleDeleteColumn = async (columnName: string) => {
250+
if (confirm(`Are you sure you want to delete the computed column "${columnName}"?`)) {
251+
try {
252+
await deleteComputedColumnAction(projectId, query.id, columnName);
253+
console.log("Deleted computed column:", columnName);
254+
255+
// Clear editor state after successful deletion
256+
setEditingColumn(null);
257+
setColumnName("");
258+
if (viewRef.current) {
259+
const transaction = viewRef.current.state.update({
260+
changes: { from: 0, to: viewRef.current.state.doc.length, insert: "" },
261+
});
262+
viewRef.current.dispatch(transaction);
263+
}
264+
} catch (error) {
265+
console.error("Failed to delete computed column:", error);
266+
}
267+
}
268+
};
269+
248270
const handleSave = async () => {
249271
if (!columnName.trim()) {
250272
console.warn("Column name is required");
@@ -365,6 +387,17 @@ export function QuerySidebar({
365387
Cancel
366388
</Button>
367389
)}
390+
{editingColumn && (
391+
<Button
392+
variant="outline"
393+
size="sm"
394+
onClick={() => handleDeleteColumn(editingColumn)}
395+
className="flex items-center gap-2 text-destructive hover:text-destructive hover:bg-destructive/10"
396+
>
397+
<Trash2 className="h-4 w-4" />
398+
Delete
399+
</Button>
400+
)}
368401
<Button
369402
variant="outline"
370403
size="sm"

packages/app/src/db/crud/query.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,25 @@ export async function updateComputedColumn(
155155

156156
return updated;
157157
}
158+
159+
export async function deleteComputedColumn(
160+
queryId: string,
161+
columnName: string,
162+
): Promise<Query> {
163+
const existing = await readQuery(queryId);
164+
if (!existing) {
165+
throw new Error("Query not found");
166+
}
167+
168+
const updatedComputedColumns = (existing.computedColumns ?? []).filter(
169+
(col) => col.name !== columnName
170+
);
171+
172+
const [updated] = await db
173+
.update(query)
174+
.set({ computedColumns: updatedComputedColumns })
175+
.where(eq(query.id, queryId))
176+
.returning();
177+
178+
return updated;
179+
}

0 commit comments

Comments
 (0)