Skip to content

Commit

Permalink
wip add db creation support
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Sep 17, 2019
1 parent ba8c1b1 commit 94ddbd9
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 72 deletions.
6 changes: 3 additions & 3 deletions cli/introspection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"types": "dist/index.d.ts",
"license": "Apache-2.0",
"devDependencies": {
"@prisma/cli": "^0.0.47",
"@prisma/lift": "^0.2.5",
"@prisma/photon": "^0.2.8",
"@prisma/cli": "^0.0.70",
"@prisma/lift": "^0.2.53",
"@prisma/photon": "^0.2.60",
"@types/dotenv": "^6.1.1",
"@types/figures": "^3.0.1",
"@types/fs-extra": "^7.0.0",
Expand Down
2 changes: 2 additions & 0 deletions cli/introspection/src/prompt/initPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Step41Introspection from './screens/Step41Introspection'
import Step60ProcessBlank from './screens/Step60ProcessBlank'
import Step60DownloadExample from './screens/Step60DownloadExample'
import Step61Success from './screens/Step61Success'
import Step2CreateOrSelectDB from './screens/Step2CreateOrSelectDB'

export async function initPrompt(outputDir: string) {
return new Promise(resolve => {
Expand All @@ -35,6 +36,7 @@ export async function initPrompt(outputDir: string) {
<Route path="choose-database" component={<Step2ChooseDatabase />} />
<Route path="sqlite-file-path" component={<Step21SqliteFilePath />} />
<Route path="tool-selection" component={<Step22ToolSelection />} />
<Route path="create-or-select-db" component={<Step2CreateOrSelectDB />} />
<Route path="language-selection" component={<Step3LanguageSelection />} />
<Route path="demo-script-selection" component={<Step4DemoScriptSelection />} />
<Route path="select-database" component={<Step4SelectDatabase />} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { Checkbox } from '../components/inputs/Checkbox'
import { useConnector } from '../components/useConnector'
import { ErrorBox } from '../components/ErrorBox'
import { RouterContext } from '../components/Router'
import Spinner from 'ink-spinner'
import DummySelectable from '../components/DummySelectable'
import Spinner from 'ink-spinner'
const AnySpinner: any = Spinner

const Step1PostgresCredentials: React.FC = () => {
Expand All @@ -36,7 +36,7 @@ const Step1PostgresCredentials: React.FC = () => {
router.setRoute('introspection')
}
} else {
router.setRoute(state.useStarterKit ? 'download-example' : 'language-selection')
router.setRoute('create-or-select-db')
}
} else {
router.setRoute('choose-database')
Expand Down
101 changes: 101 additions & 0 deletions cli/introspection/src/prompt/screens/Step2CreateOrSelectDB.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React, { useContext, useState } from 'react'
import { Box, Color } from 'ink'
import BorderBox from '../components/BorderBox'
import chalk from 'chalk'
import { Link } from '../components/Link'
import { useInitState } from '../components/InitState'
import { DatabaseType } from 'prisma-datamodel'
import { useConnector } from '../components/useConnector'
import { prettyDb } from '../utils/print'
import { RouterContext } from '../components/Router'
import { createDatabase } from '@prisma/lift'
import { ErrorBox, FixBox } from '../components/ErrorBox'
import DummySelectable from '../components/DummySelectable'
import Spinner from 'ink-spinner'
const AnySpinner: any = Spinner

const Step2CreateOrSelectDB: React.FC = () => {
const [state] = useInitState()
const [error, setError] = useState('')
const [creatingDb, setCreatingDb] = useState(false)

const router = useContext(RouterContext)
const { schemas, disconnect } = useConnector()
if (!state.dbCredentials) {
throw new Error('Missing credentials in choose db view')
}
const { dbCredentials } = state
const db = prettyDb(dbCredentials.type)
const schemaWord = dbCredentials.type === DatabaseType.postgres ? 'schema' : 'database'
const dbName = dbCredentials[schemaWord]!

const schemaCount = state.useStarterKit ? schemas!.filter(s => s.countOfTables === 0).length : schemas!.length
const href = dbCredentials.type === DatabaseType.postgres ? 'postgres-credentials' : 'mysql-credentials'

const goBack = async () => {
await disconnect()
router.setRoute(href)
}

const onCreate = async () => {
try {
setCreatingDb(true)
await createDatabase(dbCredentials.uri!)
setCreatingDb(false)
router.setRoute(state.useStarterKit ? 'download-example' : 'language-selection')
} catch (e) {
setCreatingDb(false)
setError(e.message || JSON.stringify(e))
}
}

return (
<Box flexDirection="column">
<Box flexDirection="column" marginLeft={2}>
<Color bold>
A {schemaWord} named `{dbName}` doesn't exist on this {db} server.
</Color>
<Color bold>
Create {db} database `{dbName}` or select an existing one.
</Color>
</Box>
<BorderBox flexDirection="column" title={chalk.bold('Database options')} marginTop={1} marginBottom={1}>
{creatingDb ? (
<DummySelectable tabIndex={0}>
<Color cyan>
<AnySpinner /> Creating {schemaWord} `{dbName}`
</Color>
</DummySelectable>
) : (
!error && (
<Link
label={`Create ${db} ${schemaWord} \`${dbName}\``}
description="Start from scratch"
tabIndex={0}
padding={40}
onSelect={onCreate}
/>
)
)}
{schemaCount > 0 && (
<Link
label={`Use existing ${db} ${schemaWord}`}
href="select-database"
description={`Found ${schemaCount} ${schemaWord}${schemaCount === 1 ? '' : 's'}`}
tabIndex={1}
padding={40}
/>
)}
</BorderBox>
{error && error.length > 0 && (
<Box flexDirection="column" marginBottom={1}>
<ErrorBox>{error}</ErrorBox>
<FixBox>Make sure you have the correct rights to create the database.</FixBox>
</Box>
)}
<Link onSelect={goBack} label="Back" description="(Database credentials)" tabIndex={3} kind="back" />
</Box>
)
}

export default Step2CreateOrSelectDB
44 changes: 35 additions & 9 deletions cli/introspection/src/prompt/screens/Step4DatabaseName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ import { useInitState } from '../components/InitState'
import { prettyDb } from '../utils/print'
import { DatabaseType } from 'prisma-datamodel'
import { RouterContext } from '../components/Router'
import { ErrorBox, FixBox } from '../components/ErrorBox'
import { createDatabase } from '@prisma/lift'
import DummySelectable from '../components/DummySelectable'
import Spinner from 'ink-spinner'
const AnySpinner: any = Spinner

// We can't use this screen yet, as we don't have SQLite introspection yet
const Step4DatabaseName: React.FC = () => {
const [state, { setDbCredentials }] = useInitState()
const [creatingDb, setCreatingDb] = useState(false)

if (!state.dbCredentials) {
throw new Error('Missing credentials in database name view')
}
const { dbCredentials } = state
const db = prettyDb(dbCredentials.type)
const schemaWord = dbCredentials.type === DatabaseType.postgres ? 'schema' : 'database'
const href = state.useStarterKit ? 'download-example' : 'language-selection'
const dbName = dbCredentials[schemaWord]!

const router = useContext(RouterContext)

const next = () => {
if (!error) {
router.setRoute(href)
}
}

const [error, setError] = useState<null | string>(null)

useEffect(() => {
Expand All @@ -41,6 +41,18 @@ const Step4DatabaseName: React.FC = () => {
}
}, [dbCredentials])

const onCreate = async () => {
try {
setCreatingDb(true)
await createDatabase(dbCredentials.uri!)
setCreatingDb(false)
router.setRoute(state.useStarterKit ? 'download-example' : 'language-selection')
} catch (e) {
setCreatingDb(false)
setError(e.message || JSON.stringify(e))
}
}

return (
<Box flexDirection="column">
<Box flexDirection="column" marginLeft={2}>
Expand All @@ -56,10 +68,24 @@ const Step4DatabaseName: React.FC = () => {
value={state.dbCredentials[schemaWord] || ''}
onChange={value => setDbCredentials({ [schemaWord]: value })}
placeholder="my_db"
onSubmit={next}
onSubmit={onCreate}
/>
</BorderBox>
{!error && <Link label="Create" href={href} tabIndex={1} kind="forward" />}
{creatingDb ? (
<DummySelectable tabIndex={0}>
<Color cyan>
<AnySpinner /> Creating {schemaWord} `{dbName}`
</Color>
</DummySelectable>
) : (
<Link label={`Create`} tabIndex={1} onSelect={onCreate} />
)}
{error && error.length > 0 && (
<Box flexDirection="column" marginBottom={1}>
<ErrorBox>{error}</ErrorBox>
<FixBox>Make sure you have the correct rights to create the database.</FixBox>
</Box>
)}
<Link label="Back" description="(Database options)" tabIndex={2} kind="back" />
</Box>
)
Expand Down

0 comments on commit 94ddbd9

Please sign in to comment.