Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BugIcon, DatabaseZap, GithubIcon } from "lucide-react";
import { ConfigureDB } from "./ConfigureDB";
import { ModeToggle } from "./mode-toggle";
import { NetworkLatencyConfigurator } from "./NetworkLatencyConfigurator";
import { ResetTheDbDialog } from "./ResetTheDbDialog";
import { Button } from "./ui/button";
import { SidebarTrigger } from "./ui/sidebar";

Expand Down Expand Up @@ -41,6 +42,9 @@ export function Header() {
<ConfigureDB />
</div>
</ClientOnly>
<ClientOnly fallback={null}>
<ResetTheDbDialog />
</ClientOnly>
<ClientOnly fallback={null}>
<NetworkLatencyConfigurator />
</ClientOnly>
Expand Down
69 changes: 69 additions & 0 deletions src/components/ResetTheDbDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Trash2 } from "lucide-react";
import { useState } from "react";
import { toast } from "sonner";
import { resetAndSeed } from "../db/seed";
import { Button } from "./ui/button";
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "./ui/dialog";

export function ResetTheDbDialog() {
const [open, setOpen] = useState(false);
const [isResetting, setIsResetting] = useState(false);

const handleReset = async () => {
try {
setIsResetting(true);
await resetAndSeed();
setOpen(false);
// Reload the page to reflect the reset data
window.location.reload();
} catch (error) {
console.error("Failed to reset database:", error);
// alert("Failed to reset the database. Please try again.");
toast.error("Failed to reset the database. Please try again.");
} finally {
setIsResetting(false);
}
};

return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="outline" size="icon">
<Trash2 />
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Reset the db</DialogTitle>
<DialogDescription>
This will reset the database to its initial seed data. All of your
current changes will be permanently lost.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose asChild>
<Button variant="outline" disabled={isResetting}>
Cancel
</Button>
</DialogClose>
<Button
variant="destructive"
onClick={handleReset}
disabled={isResetting}
>
<Trash2 /> {isResetting ? "Resetting..." : "Reset"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
32 changes: 32 additions & 0 deletions src/db/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,35 @@ export async function seed() {
throw error;
}
}

export async function resetAndSeed() {
try {
// Find all tables in the database
const tablesResult = await db.execute<{ tablename: string }>(
`SELECT tablename FROM pg_tables WHERE schemaname = 'public'`,
);

console.log(`Found ${tablesResult.rows.length} tables to clear.`);

// Delete all records from each table (except drizzle_migrations)
for (const { tablename } of tablesResult.rows) {
if (tablename === "drizzle_migrations") {
console.log(`Skipping table: ${tablename}`);
continue;
}
await db.execute(`DELETE FROM "${tablename}"`);
console.log(`Cleared table: ${tablename}`);
}

console.log("All tables have been cleared.");

// Run the seed function
const result = await seed();
console.log("Database has been reseeded successfully.");

return result;
} catch (error) {
console.error("Error during reset and seed:", error);
throw error;
}
}