Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 85x 85x | /**
* Migration detail dialog component
*/
import { AppDialog } from '@/components/common/AppDialog';
import { ScrollArea } from '@/components/ui/scroll-area';
import { Separator } from '@/components/ui/separator';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import type { MigrationRevision } from '@/types/migrations';
import { format } from 'date-fns';
import { Database, Clock, GitBranch, FileCode, Copy, Check } from 'lucide-react';
import { useState } from 'react';
import MigrationStatusBadge from './MigrationStatusBadge';
interface MigrationDetailDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
migration: MigrationRevision | null;
currentRevision: string | null;
}
const DetailItem = ({
label,
value,
icon: Icon,
mono = false,
}: {
label: string;
value: string | number | null | undefined;
icon?: React.ElementType;
mono?: boolean;
}) => (
<div className="flex flex-col space-y-1 py-2">
<div className="flex items-center gap-2 text-sm font-medium text-muted-foreground">
{Icon && <Icon className="h-4 w-4" />}
{label}
</div>
<div className={`text-sm break-all font-medium ${mono ? 'font-mono' : ''}`}>
{value || <span className="text-muted-foreground italic">None</span>}
</div>
</div>
);
export default function MigrationDetailDialog({
open,
onOpenChange,
migration,
currentRevision,
}: MigrationDetailDialogProps) {
const [copiedRevision, setCopiedRevision] = useState(false);
Eif (!migration) return null;
const handleCopyRevision = async () => {
await navigator.clipboard.writeText(migration.revision);
setCopiedRevision(true);
setTimeout(() => setCopiedRevision(false), 2000);
};
return (
<AppDialog
open={open}
onOpenChange={onOpenChange}
title={
<span className="flex items-center gap-2">
Migration: {migration.revision.substring(0, 12)}
<MigrationStatusBadge
isApplied={migration.is_applied}
isHead={migration.is_head}
isCurrent={migration.revision === currentRevision}
/>
</span>
}
description={migration.description}
className="max-w-2xl"
>
<ScrollArea className="pr-4 h-full">
<div className="space-y-6">
{/* Summary Section */}
<div className="grid grid-cols-2 gap-4">
<div className="flex flex-col space-y-1 py-2">
<div className="flex items-center gap-2 text-sm font-medium text-muted-foreground">
<Database className="h-4 w-4" />
Revision Hash
</div>
<div className="flex items-center gap-2">
<code className="text-sm font-mono bg-muted px-2 py-1 rounded">
{migration.revision}
</code>
<Button
variant="ghost"
size="icon"
className="h-6 w-6"
onClick={handleCopyRevision}
>
{copiedRevision ? (
<Check className="h-3 w-3 text-green-500" />
) : (
<Copy className="h-3 w-3" />
)}
</Button>
</div>
</div>
<DetailItem
label="Created At"
value={
migration.created_at
? format(new Date(migration.created_at), 'yyyy-MM-dd HH:mm:ss')
: 'Unknown'
}
icon={Clock}
/>
<div className="flex flex-col space-y-1 py-2">
<div className="flex items-center gap-2 text-sm font-medium text-muted-foreground">
<FileCode className="h-4 w-4" />
Migration Type
</div>
<Badge
variant={migration.is_dynamic ? 'secondary' : 'outline'}
className={
migration.is_dynamic
? 'bg-purple-500 hover:bg-purple-600 text-white w-fit'
: 'w-fit'
}
>
{migration.is_dynamic ? 'Dynamic (Auto-generated)' : 'Core (Manual)'}
</Badge>
</div>
<div className="flex flex-col space-y-1 py-2">
<div className="flex items-center gap-2 text-sm font-medium text-muted-foreground">
<FileCode className="h-4 w-4" />
File Path
</div>
<code className="text-xs font-mono bg-muted px-2 py-1 rounded break-all">
alembic/versions/{migration.is_dynamic ? 'dynamic' : 'core'}/
{migration.revision}_{migration.description.toLowerCase().replace(/\s+/g, '_')}.py
</code>
</div>
</div>
<Separator />
{/* Revision Chain */}
<div>
<h3 className="flex items-center gap-2 text-sm font-bold uppercase text-primary mb-4">
<GitBranch className="h-4 w-4" />
Revision Chain
</h3>
<div className="grid grid-cols-2 gap-4">
<DetailItem
label="Revises (Previous)"
value={migration.down_revision || 'None (Initial migration)'}
mono
/>
<div className="flex flex-col space-y-1 py-2">
<div className="flex items-center gap-2 text-sm font-medium text-muted-foreground">
Branch Labels
</div>
{migration.branch_labels && migration.branch_labels.length > 0 ? (
<div className="flex gap-1 flex-wrap">
{migration.branch_labels.map((label) => (
<Badge key={label} variant="outline">
{label}
</Badge>
))}
</div>
) : (
<span className="text-sm text-muted-foreground italic">None</span>
)}
</div>
</div>
</div>
<Separator />
{/* SQL Information */}
<div className="space-y-4">
<div className="bg-muted/30 p-4 rounded-lg border border-muted">
<h4 className="text-xs font-bold uppercase text-muted-foreground mb-3">
Migration SQL
</h4>
<p className="text-sm text-muted-foreground mb-4">
To view the actual upgrade and downgrade SQL, inspect the migration file at the
path shown above. The file contains <code className="bg-muted px-1 py-0.5 rounded">upgrade()</code> and{' '}
<code className="bg-muted px-1 py-0.5 rounded">downgrade()</code> functions with the
SQL operations.
</p>
<div className="bg-background p-3 rounded border">
<p className="text-xs font-mono text-muted-foreground">
# To view migration details via CLI:
<br />
$ alembic show {migration.revision}
</p>
</div>
</div>
</div>
<Separator />
{/* CLI Documentation */}
<div className="bg-blue-50 dark:bg-blue-950/20 p-4 rounded-lg border border-blue-200 dark:border-blue-900">
<h4 className="text-sm font-bold text-blue-900 dark:text-blue-100 mb-2">
Migration Operations (CLI Only)
</h4>
<p className="text-sm text-blue-800 dark:text-blue-200 mb-3">
For production safety, migration operations must be performed via CLI:
</p>
<div className="space-y-2 text-xs font-mono bg-blue-100 dark:bg-blue-950/40 p-3 rounded">
<div># Apply migrations:</div>
<div className="text-blue-600 dark:text-blue-400">$ alembic upgrade head</div>
<div className="mt-2"># Rollback one migration:</div>
<div className="text-blue-600 dark:text-blue-400">$ alembic downgrade -1</div>
<div className="mt-2"># View migration history:</div>
<div className="text-blue-600 dark:text-blue-400">$ alembic history</div>
</div>
</div>
</div>
</ScrollArea>
</AppDialog>
);
}
|