-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMigrations.tsx
More file actions
243 lines (227 loc) · 6.19 KB
/
Migrations.tsx
File metadata and controls
243 lines (227 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { useEffect, useState } from "react";
import { useQuery, useMutation } from "urql";
import { DataTable, type Column } from "@/components/DataTable";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { RefreshCwIcon, ChevronDownIcon, ChevronRightIcon } from "lucide-react";
import { toast } from "sonner";
const TREX_MIGRATIONS_QUERY = `
query TrexMigrations {
trexMigrations {
pluginName
schema
database
currentVersion
totalMigrations
appliedCount
pendingCount
migrations { version name status appliedOn checksum }
}
}
`;
const RUN_MIGRATIONS_MUTATION = `
mutation RunPluginMigrations($pluginName: String) {
runPluginMigrations(pluginName: $pluginName) {
success
error
results { version name status }
}
}
`;
interface MigrationRow {
version: number;
name: string;
status: string;
appliedOn: string | null;
checksum: string | null;
}
interface MigrationSummary {
pluginName: string;
schema: string;
database: string;
currentVersion: number | null;
totalMigrations: number;
appliedCount: number;
pendingCount: number;
migrations: MigrationRow[];
}
export function Migrations() {
const [result, reexecute] = useQuery({ query: TREX_MIGRATIONS_QUERY });
const [, runMigrations] = useMutation(RUN_MIGRATIONS_MUTATION);
const [expanded, setExpanded] = useState<Set<string>>(new Set());
const summaries: MigrationSummary[] = result.data?.trexMigrations || [];
function refetch() {
reexecute({ requestPolicy: "network-only" });
}
useEffect(() => {
const interval = setInterval(refetch, 30000);
return () => clearInterval(interval);
}, []);
function toggleExpand(pluginName: string) {
setExpanded((prev) => {
const next = new Set(prev);
if (next.has(pluginName)) {
next.delete(pluginName);
} else {
next.add(pluginName);
}
return next;
});
}
async function handleRunMigrations() {
const res = await runMigrations({});
if (res.data?.runPluginMigrations?.success) {
const results = res.data.runPluginMigrations.results || [];
const applied = results.filter((r: any) => r.status === "applied").length;
if (applied > 0) {
toast.success(`${applied} migration(s) applied`);
} else {
toast.info("All migrations are up to date");
}
refetch();
} else {
toast.error(res.data?.runPluginMigrations?.error || "Migration failed");
}
}
const totalPending = summaries.reduce((sum, s) => sum + s.pendingCount, 0);
const columns: Column<MigrationSummary>[] = [
{
header: "",
cell: (row) => (
<button
onClick={() => toggleExpand(row.pluginName)}
className="p-1 hover:bg-accent rounded"
>
{expanded.has(row.pluginName) ? (
<ChevronDownIcon className="h-4 w-4" />
) : (
<ChevronRightIcon className="h-4 w-4" />
)}
</button>
),
},
{
header: "Plugin",
cell: (row) => (
<span className="text-sm font-medium">{row.pluginName}</span>
),
},
{
header: "Schema",
cell: (row) => (
<code className="text-xs font-mono">{row.schema}</code>
),
},
{
header: "Database",
cell: (row) => (
<code className="text-xs font-mono">{row.database}</code>
),
},
{
header: "Schema Version",
cell: (row) => (
<span className="text-sm">{row.currentVersion ?? "-"}</span>
),
},
{
header: "Applied",
cell: (row) => (
<Badge variant="default">{row.appliedCount}</Badge>
),
},
{
header: "Pending",
cell: (row) => (
<Badge variant={row.pendingCount > 0 ? "destructive" : "secondary"}>
{row.pendingCount}
</Badge>
),
},
{
header: "Status",
cell: (row) =>
row.pendingCount > 0 ? (
<Badge variant="outline">Pending</Badge>
) : (
<Badge variant="default">Up to date</Badge>
),
},
];
const migrationColumns: Column<MigrationRow>[] = [
{
header: "Version",
cell: (row) => <span className="text-sm font-mono">{row.version}</span>,
},
{
header: "Name",
cell: (row) => <span className="text-sm">{row.name}</span>,
},
{
header: "Status",
cell: (row) => (
<Badge
variant={
row.status === "applied"
? "default"
: row.status === "pending"
? "outline"
: "destructive"
}
>
{row.status}
</Badge>
),
},
{
header: "Applied On",
cell: (row) => (
<span className="text-sm text-muted-foreground">
{row.appliedOn || "-"}
</span>
),
},
];
return (
<div className="space-y-6">
<div className="flex items-start justify-between">
<div>
<h2 className="text-2xl font-bold">Migrations</h2>
<p className="text-muted-foreground">
{summaries.length} migration source{summaries.length === 1 ? "" : "s"}
{totalPending > 0 && `, ${totalPending} pending`}
</p>
</div>
<div className="flex gap-2">
<Button variant="outline" onClick={handleRunMigrations}>
Run Migrations
</Button>
<Button variant="outline" onClick={refetch}>
<RefreshCwIcon className="h-4 w-4" />
</Button>
</div>
</div>
<DataTable
columns={columns}
data={summaries}
loading={result.fetching}
emptyMessage="No migration sources found."
/>
{summaries
.filter((s) => expanded.has(s.pluginName))
.map((s) => (
<div key={s.pluginName} className="pl-8">
<h3 className="text-sm font-semibold mb-2">
{s.pluginName} — individual migrations
</h3>
<DataTable
columns={migrationColumns}
data={s.migrations}
loading={false}
emptyMessage="No migrations."
/>
</div>
))}
</div>
);
}