Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Prettify migrations page

Joshen Limd99b672b8d19
diff --git a/studio/components/interfaces/Database/Migrations/Migrations.tsx b/studio/components/interfaces/Database/Migrations/Migrations.tsx
new file mode 100644
index 00000000000..fcf2f7649e9
--- /dev/null
+++ b/studio/components/interfaces/Database/Migrations/Migrations.tsx
@@ -0,0 +1,162 @@
+import { observer } from 'mobx-react-lite'
+import Link from 'next/link'
+import { useState } from 'react'
+import { Alert, Button, IconExternalLink, IconSearch, Input, SidePanel } from 'ui'
+
+import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
+import Table from 'components/to-be-cleaned/Table'
+import ShimmeringLoader from 'components/ui/ShimmeringLoader'
+import { DatabaseMigration, useMigrationsQuery } from 'data/database/migrations-query'
+import { Editor } from '@monaco-editor/react'
+import CodeEditor from 'components/ui/CodeEditor'
+
+const Wrappers = () => {
+ const [search, setSearch] = useState('')
+ const [selectedMigration, setSelectedMigration] = useState<DatabaseMigration>()
+
+ const { project } = useProjectContext()
+ const { data, isLoading, isSuccess, isError, error } = useMigrationsQuery({
+ projectRef: project?.ref,
+ connectionString: project?.connectionString,
+ })
+ const migrations =
+ search.length === 0
+ ? data?.result ?? []
+ : data?.result.filter((migration) => migration.version.includes(search)) ?? []
+
+ return (
+ <>
+ <div>
+ <div className="flex items-center justify-between mb-6">
+ <div>
+ <h3 className="mb-2 text-xl text-scale-1200">Migrations</h3>
+ <div className="text-sm text-scale-900">
+ History of migrations that have been run on your database
+ </div>
+ </div>
+ <div className="flex items-center space-x-2">
+ <Link href="https://supabase.com/docs/guides/getting-started/local-development#database-migrations">
+ <a target="_blank" rel="noreferrer">
+ <Button type="default" icon={<IconExternalLink strokeWidth={1.5} />}>
+ Documentation
+ </Button>
+ </a>
+ </Link>
+ </div>
+ </div>
+
+ {isLoading ? (
+ <div className="space-y-2">
+ <ShimmeringLoader />
+ <ShimmeringLoader className="w-3/4" />
+ <ShimmeringLoader className="w-1/2" />
+ </div>
+ ) : (
+ <div>
+ {isError && (
+ <Alert
+ withIcon
+ variant="warning"
+ title="Failed to retrieve migration history for database"
+ actions={[
+ <Link
+ key="contact-support"
+ href={`/support/new?ref=${project?.ref}&category=dashboard_bug&subject=Unable%20to%20view%20database%20migrations`}
+ >
+ <a>
+ <Button type="default" className="ml-4">
+ Contact support
+ </Button>
+ </a>
+ </Link>,
+ ]}
+ >
+ <p className="mb-1">
+ Try refreshing your browser, but if the issue persists, please reach out to us via
+ support.
+ </p>
+ <p>Error: {(error as any)?.message ?? 'Unknown'}</p>
+ </Alert>
+ )}
+ {isSuccess && (
+ <div>
+ <div className="w-80 mb-4">
+ <Input
+ size="small"
+ placeholder="Search for a migration"
+ value={search}
+ onChange={(e: any) => setSearch(e.target.value)}
+ icon={<IconSearch size="tiny" />}
+ />
+ </div>
+
+ {data.result.length <= 0 && <div>No migrations state...</div>}
+
+ {data.result.length > 0 && (
+ <Table
+ head={[
+ <Table.th key="version">Version</Table.th>,
+ <Table.th key="buttons"></Table.th>,
+ ]}
+ body={
+ migrations.length > 0 ? (
+ migrations.map((migration) => (
+ <Table.tr key={migration.version}>
+ <Table.td>{migration.version}</Table.td>
+ <Table.td align="right">
+ <Button
+ type="default"
+ onClick={() => setSelectedMigration(migration)}
+ >
+ View migration SQL
+ </Button>
+ </Table.td>
+ </Table.tr>
+ ))
+ ) : (
+ <Table.tr>
+ <Table.td>
+ <p className="text-sm text-scale-1200">No results found</p>
+ <p className="text-sm text-scale-1100">
+ Your search for "{search}" did not return any results
+ </p>
+ </Table.td>
+ </Table.tr>
+ )
+ }
+ />
+ )}
+ </div>
+ )}
+ </div>
+ )}
+ </div>
+ <SidePanel
+ size="large"
+ visible={selectedMigration !== undefined}
+ header={`Migration: ${selectedMigration?.version}`}
+ onCancel={() => setSelectedMigration(undefined)}
+ customFooter={
+ <div className="flex items-center justify-end p-4 border-t border-overlay-border">
+ <Button type="default" onClick={() => setSelectedMigration(undefined)}>
+ Close
+ </Button>
+ </div>
+ }
+ >
+ <div className="h-full">
+ <div className="relative h-full">
+ <CodeEditor
+ isReadOnly
+ id={selectedMigration?.version ?? ''}
+ language="pgsql"
+ defaultValue={selectedMigration?.statements?.join('\n')}
+ />
+ </div>
+ </div>
+ </SidePanel>
+ </>
+ )
+}
+
+export default observer(Wrappers)
diff --git a/studio/components/to-be-cleaned/Table.tsx b/studio/components/to-be-cleaned/Table.tsx
index dc3c37f83ce..3b25fbcf4da 100644
--- a/studio/components/to-be-cleaned/Table.tsx
+++ b/studio/components/to-be-cleaned/Table.tsx
@@ -79,6 +79,7 @@ type TdProps = {
colSpan?: number
className?: string
style?: React.CSSProperties
+ align?: 'left' | 'center' | 'right' | 'justify' | 'char' | undefined
} & React.HTMLProps<HTMLTableCellElement>
const Td: React.FC<TdProps> = ({ children, colSpan, className, style, ...rest }) => {
return (
diff --git a/studio/components/ui/CodeEditor/CodeEditor.tsx b/studio/components/ui/CodeEditor/CodeEditor.tsx
index 253e2ad4775..7f291e08e3a 100644
--- a/studio/components/ui/CodeEditor/CodeEditor.tsx
+++ b/studio/components/ui/CodeEditor/CodeEditor.tsx
@@ -4,14 +4,14 @@ import Editor, { EditorProps } from '@monaco-editor/react'
import { timeout } from 'lib/helpers'
import Connecting from '../Loading'
import { alignEditor } from './CodeEditor.utils'
-import { merge } from 'lodash'
+import { merge, noop } from 'lodash'
interface Props {
id: string
language: 'pgsql' | 'json' | 'html'
defaultValue?: string
isReadOnly?: boolean
- onInputChange: (value?: string) => void
+ onInputChange?: (value?: string) => void
onInputRun?: (value: string) => void
hideLineNumbers?: boolean
className?: string
@@ -26,8 +26,8 @@ const CodeEditor: FC<Props> = ({
defaultValue,
isReadOnly = false,
hideLineNumbers = false,
- onInputChange = () => {},
- onInputRun = () => {},
+ onInputChange = noop,
+ onInputRun = noop,
className,
loading,
options,
diff --git a/studio/pages/project/[ref]/database/migrations.tsx b/studio/pages/project/[ref]/database/migrations.tsx
index 0cfb2e8f266..16a3cd2500e 100644
--- a/studio/pages/project/[ref]/database/migrations.tsx
+++ b/studio/pages/project/[ref]/database/migrations.tsx
@@ -1,3 +1,5 @@
+import clsx from 'clsx'
+import Migrations from 'components/interfaces/Database/Migrations/Migrations'
import { DatabaseLayout } from 'components/layouts'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { useMigrationsQuery } from 'data/database/migrations-query'
@@ -11,26 +13,13 @@ const MigrationsPage: NextPageWithLayout = () => {
})
return (
- <div>
- {isLoading && <div>Loading state...</div>}
- {isError && <div>Error state...</div>}
- {isSuccess && (
- <div>
- {data.result.length <= 0 && <div>No migrations state...</div>}
-
- {data.result.length > 0 &&
- data.result.map((migration) => (
- <div key={migration.version}>
- <div>{migration.version}</div>
- <div>
- {migration.statements?.map((statement, i) => (
- <div key={i}>{statement}</div>
- ))}
- </div>
- </div>
- ))}
- </div>
+ <div
+ className={clsx(
+ 'mx-auto flex flex-col px-5 pt-6 pb-14',
+ 'lg:pt-8 lg:px-14 1xl:px-28 2xl:px-32 h-full'
)}
+ >
+ <Migrations />
</div>
)
}