-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathMigrationChecker.tsx
More file actions
116 lines (99 loc) · 3.35 KB
/
Copy pathMigrationChecker.tsx
File metadata and controls
116 lines (99 loc) · 3.35 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
'use client';
import { useState, useEffect, useRef, useCallback } from 'react';
import { Label } from '@/components/ui/label';
import { Button } from '@/components/ui/button';
import BuilderLoading from '@/components/BuilderLoading';
/**
* Migration Checker Component: Checks for and runs pending migrations before allowing
* builder access. This prevents the builder from trying to query tables that don't exist yet.
*/
interface MigrationCheckerProps {
onComplete: () => void;
}
export default function MigrationChecker({ onComplete }: MigrationCheckerProps) {
const [isChecking, setIsChecking] = useState(true);
const [progress, setProgress] = useState('Checking database status...');
const [error, setError] = useState<string | null>(null);
// Ref to ensure migration only runs once (prevents React Strict Mode double-run)
const hasRunRef = useRef(false);
const checkAndRunMigrations = useCallback(async () => {
try {
setIsChecking(true);
setProgress('Checking and running migrations...');
setError(null);
// Single API call: checks AND runs migrations if needed
const response = await fetch('/ycode/api/setup/migrate', {
method: 'POST',
});
if (!response.ok) {
console.error('Migration request failed');
console.error(await response.json());
onComplete(); // Allow builder to load anyway
return;
}
const result = await response.json();
if (result.error) {
setError(result.error);
setIsChecking(false);
return;
}
// Successfully ran migrations, allow builder to load
onComplete();
} catch (err) {
console.error('Failed to run migrations:', err);
setError(err instanceof Error ? err.message : 'Migration failed');
setIsChecking(false);
}
}, [onComplete]);
useEffect(() => {
// Skip if already run (React Strict Mode protection)
if (hasRunRef.current) {
return;
}
hasRunRef.current = true;
checkAndRunMigrations();
}, [checkAndRunMigrations]);
const handleRetry = () => {
setError(null);
checkAndRunMigrations();
};
const handleSkip = () => {
// Allow user to skip and try to use builder anyway (risky but their choice)
onComplete();
};
// Always show this component while checking migrations
if (!isChecking && !error) {
return null;
}
// Show error state
if (error) {
return (
<div className="fixed inset-0 z-[100] bg-neutral-950 flex items-center justify-center">
<div className="max-w-md w-full mx-4">
<div className="flex-1 flex items-center text-center flex-col gap-1">
<Label size="sm">
Migration failed
</Label>
<Label variant="muted" size="sm">
{error}
</Label>
<div className="w-full max-w-xs grid grid-cols-2 gap-3 mt-2">
<Button onClick={handleRetry}>
Retry migration
</Button>
<Button
variant="secondary"
onClick={handleSkip}
>
<span>Skip</span>
<span className="text-[10px] opacity-60">Not recommended</span>
</Button>
</div>
</div>
</div>
</div>
);
}
// Show loading state
return <BuilderLoading message={progress} />;
}