The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
SQL Editor: CLI commands to import snippet as migration/seed (#17341)
Greg Richardson247c72fff247
diff --git a/apps/studio/components/interfaces/SQLEditor/DownloadSnippetModal.tsx b/apps/studio/components/interfaces/SQLEditor/DownloadSnippetModal.tsxnew file mode 100644index 00000000000..ba9cc6a11f3--- /dev/null+++ b/apps/studio/components/interfaces/SQLEditor/DownloadSnippetModal.tsx@@ -0,0 +1,139 @@+import { ModalProps } from '@ui/components/Modal/Modal'+import { snakeCase } from 'lodash'+import Link from 'next/link'+import { useState } from 'react'+import { Button, CodeBlock, IconExternalLink, Modal, Separator, Tabs, TabsProvider } from 'ui'++import TwoOptionToggle from 'components/ui/TwoOptionToggle'+import { useSqlEditorStateSnapshot } from 'state/sql-editor'+import { Markdown } from '../Markdown'+import {+ generateFileCliCommand,+ generateMigrationCliCommand,+ generateSeedCliCommand,+} from './SQLEditor.utils'++export interface DownloadSnippetModalProps extends ModalProps {+ id: string+}++const DownloadSnippetModal = ({ id, ...props }: DownloadSnippetModalProps) => {+ const snap = useSqlEditorStateSnapshot()+ const snippet = snap.snippets[id].snippet+ const migrationName = snakeCase(snippet.name)++ const [selectedView, setSelectedView] = useState<'CLI' | 'NPM'>('CLI')++ const SNIPPETS = [+ {+ id: 'migration',+ label: 'Migration',+ title: 'Download as migration',+ description: `Download the snippet in a new migration named \`${migrationName}\``,+ cli: generateMigrationCliCommand(id, migrationName),+ npm: generateMigrationCliCommand(id, migrationName, true),+ },+ {+ id: 'seed',+ label: 'Seed file',+ title: 'Download as seed file',+ description:+ 'If your query consists of sample data, append the snippet to the end of `supabase/seed.sql`',+ cli: generateSeedCliCommand(id),+ npm: generateSeedCliCommand(id, true),+ },+ {+ id: 'sql',+ label: 'SQL file',+ title: 'Download as SQL file',+ description: `Download the snippet directly into a new SQL file named \`${migrationName}.sql\``,+ cli: generateFileCliCommand(id, migrationName),+ npm: generateFileCliCommand(id, migrationName, true),+ },+ ]++ return (+ <Modal+ hideFooter+ showCloseButton+ size="xlarge"+ header={<p>Download snippet as local migration file via the Supabase CLI.</p>}+ {...props}+ >+ <TabsProvider>+ <div className="flex flex-col items-start justify-between gap-4 py-5 relative">+ <Tabs type="underlined" listClassNames="pl-5">+ {SNIPPETS.map((snippet) => {+ return (+ <Tabs.Panel key={snippet.id} id={snippet.id} label={snippet.label} className="px-5">+ <div className="flex items-center justify-between mb-3">+ <div className="flex flex-col gap-y-1">+ <h2 className="text-lg">{snippet.title}</h2>+ <Markdown+ className="text-sm text-scale-1000 [&>p>code]:!break-normal"+ content={snippet.description}+ />+ </div>+ <TwoOptionToggle+ width={75}+ options={['CLI', 'NPM']}+ activeOption={selectedView}+ borderOverride="border-gray-100"+ onClickOption={() =>+ selectedView === 'CLI' ? setSelectedView('NPM') : setSelectedView('CLI')+ }+ />+ </div>+ <pre>+ <CodeBlock+ language="bash"+ className="language-bash prose dark:prose-dark max-w-none"+ >+ {selectedView === 'CLI' ? snippet.cli : snippet.npm}+ </CodeBlock>+ </pre>+ </Tabs.Panel>+ )+ })}+ </Tabs>+ <div className="w-full flex items-center justify-between">+ <p className="text-xs text-lighter mx-5">+ Run this command from your project directory+ </p>+ <div className="flex justify-between items-center gap-x-2 mx-5">+ <Button+ asChild+ type="default"+ icon={<IconExternalLink size={14} strokeWidth={1.5} />}+ >+ <Link+ href="https://supabase.com/docs/guides/cli/local-development#database-migrations"+ target="_blank"+ rel="noreferrer"+ >+ About migrations+ </Link>+ </Button>++ <Button+ asChild+ type="default"+ icon={<IconExternalLink size={14} strokeWidth={1.5} />}+ >+ <Link+ href="https://supabase.com/docs/guides/cli/local-development"+ target="_blank"+ rel="noreferrer"+ >+ About CLI+ </Link>+ </Button>+ </div>+ </div>+ </div>+ </TabsProvider>+ </Modal>+ )+}++export default DownloadSnippetModaldiff --git a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.tsindex 7c67ed7e6a4..b9d484dad0e 100644--- a/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts+++ b/apps/studio/components/interfaces/SQLEditor/SQLEditor.utils.ts@@ -4,6 +4,7 @@ import { NEW_SQL_SNIPPET_SKELETON, destructiveSqlRegex } from './SQLEditor.constimport { SqlSnippets, UserContent } from 'types'import { DiffType } from './SQLEditor.types'import { removeCommentsFromSql } from 'lib/helpers'+import { stripIndent } from 'common-tags'export const getResultsMarkdown = (results: any[]) => {const columns = Object.keys(results[0])@@ -72,3 +73,18 @@ export function getDiffTypeDropdownLabel(diffType: DiffType) {export function checkDestructiveQuery(sql: string) {return destructiveSqlRegex.some((regex) => regex.test(removeCommentsFromSql(sql)))}++export const generateMigrationCliCommand = (id: string, name: string, isNpx = false) => stripIndent`+ ${isNpx ? 'npx ' : ''}supabase snippets download ${id} |+ ${isNpx ? 'npx ' : ''}supabase migration new ${name}+`++export const generateSeedCliCommand = (id: string, isNpx = false) => stripIndent`+ ${isNpx ? 'npx ' : ''}supabase snippets download ${id} >> \\+ supabase/seed.sql+`++export const generateFileCliCommand = (id: string, name: string, isNpx = false) => stripIndent`+ ${isNpx ? 'npx ' : ''}supabase snippets download ${id} > \\+ ${name}.sql+`diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsxindex bee4ca97daa..396ab951bee 100644--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/UtilityActions.tsx@@ -29,6 +29,7 @@ const UtilityActions = ({return (<><SavingIndicator id={id} />+{IS_PLATFORM && <FavoriteButton id={id} />}<SizeToggleButton id={id} /><Tooltip.Root delayDuration={0}>diff --git a/apps/studio/components/layouts/SQLEditorLayout/QueryItem.tsx b/apps/studio/components/layouts/SQLEditorLayout/QueryItem.tsxindex e79f528a590..c7924459b92 100644--- a/apps/studio/components/layouts/SQLEditorLayout/QueryItem.tsx+++ b/apps/studio/components/layouts/SQLEditorLayout/QueryItem.tsx@@ -19,6 +19,7 @@ import {IconAlertTriangle,IconChevronDown,IconCopy,+ IconDownload,IconEdit2,IconEye,IconShare,@@ -37,6 +38,7 @@ import { IS_PLATFORM } from 'lib/constants'import { uuidv4 } from 'lib/helpers'import { useProfile } from 'lib/profile'import { useSqlEditorStateSnapshot } from 'state/sql-editor'+import DownloadSnippetModal from 'components/interfaces/SQLEditor/DownloadSnippetModal'export interface QueryItemProps {tabInfo: SqlSnippet@@ -129,6 +131,7 @@ const QueryItemActions = observer(({ tabInfo, activeId }: QueryItemActionsProps)const [renameModalOpen, setRenameModalOpen] = useState(false)const [shareModalOpen, setShareModalOpen] = useState(false)const [deleteModalOpen, setDeleteModalOpen] = useState(false)+ const [isDownloadSnippetModalOpen, setIsDownloadSnippetModalOpen] = useState(false)const isActive = id === activeIdconst canCreateSQLSnippet = useCheckPermissions(PermissionAction.CREATE, 'user_content', {@@ -231,6 +234,16 @@ const QueryItemActions = observer(({ tabInfo, activeId }: QueryItemActionsProps)<p>Duplicate personal copy</p></DropdownMenuItem>)}++ {IS_PLATFORM && (+ <DropdownMenuItem+ onClick={() => setIsDownloadSnippetModalOpen(!isDownloadSnippetModalOpen)}+ className="space-x-2"+ >+ <IconDownload size="tiny" />+ <p>Download as migration file</p>+ </DropdownMenuItem>+ )}<><DropdownMenuSeparator /><DropdownMenuItem onClick={onClickDelete} className="space-x-2">@@ -317,6 +330,11 @@ const QueryItemActions = observer(({ tabInfo, activeId }: QueryItemActionsProps)</div></Modal.Content></ConfirmationModal>+ <DownloadSnippetModal+ id={id as string}+ visible={isDownloadSnippetModalOpen}+ onCancel={() => setIsDownloadSnippetModalOpen(false)}+ /></div>)})diff --git a/packages/ui/index.tsx b/packages/ui/index.tsxindex b17032dbff8..47f7bff4cbd 100644--- a/packages/ui/index.tsx+++ b/packages/ui/index.tsx@@ -156,6 +156,8 @@ export {export { ScrollArea, ScrollBar } from './src/components/shadcn/ui/scroll-area'+export { Separator } from './src/components/shadcn/ui/separator'+// linksexport * from './src/components/TextLink'diff --git a/packages/ui/src/components/CodeBlock/CodeBlock.tsx b/packages/ui/src/components/CodeBlock/CodeBlock.tsxindex 5fa18aa568c..4147132f810 100644--- a/packages/ui/src/components/CodeBlock/CodeBlock.tsx+++ b/packages/ui/src/components/CodeBlock/CodeBlock.tsx@@ -79,7 +79,7 @@ export const CodeBlock = ({const large = false// don't show line numbers if bash == lang- if (lang !== 'bash') hideLineNumbers = true+ if (lang === 'bash' || lang === 'sh') hideLineNumbers = trueconst showLineNumbers = !hideLineNumbersreturn (diff --git a/packages/ui/src/components/Modal/Modal.tsx b/packages/ui/src/components/Modal/Modal.tsxindex 5752f01a973..a96d2aa10d9 100644--- a/packages/ui/src/components/Modal/Modal.tsx+++ b/packages/ui/src/components/Modal/Modal.tsx@@ -149,8 +149,8 @@ const Modal = ({<ButtononClick={onCancel}type="text"- icon={<IconX size="medium" />}- className="px-1"+ icon={<IconX size="small" strokeWidth={1.5} />}+ className="p-0.5 !mt-0"/>)}</div>