MCPcopy
hub / github.com/prisma/prisma / ensureDatabaseExists

Function ensureDatabaseExists

packages/migrate/src/utils/ensureDatabaseExists.ts:131–169  ·  view source on GitHub ↗
(
  pathResolutionRoot: string,
  provider: ConnectorType,
  config: PrismaConfigWithDatasource,
)

Source from the content-addressed store, hash-verified

129type SuccessMessage = string
130
131export async function ensureDatabaseExists(
132 pathResolutionRoot: string,
133 provider: ConnectorType,
134 config: PrismaConfigWithDatasource,
135): Promise<SuccessMessage | undefined> {
136 const url = config.datasource.url
137
138 const canConnect = await canConnectToDatabase(url, pathResolutionRoot)
139 if (canConnect === true) {
140 return
141 }
142 const { code, message } = canConnect
143
144 // P1003 means we can connect but that the database doesn't exist
145 if (code !== 'P1003') {
146 throw new Error(`${code}: ${message}`)
147 }
148
149 if (await createDatabase(url, pathResolutionRoot)) {
150 // URI parsing is not implemented for SQL server yet
151 if (provider === 'sqlserver') {
152 return `SQL Server database created.\n`
153 }
154
155 // parse the url
156 const credentials = uriToCredentials(url)
157 const prettyProvider = prettifyProvider(provider)
158
159 let message = `${prettyProvider} database${credentials.database ? ` ${credentials.database} ` : ' '}created`
160 const dbLocation = getDbLocation(credentials)
161 if (dbLocation) {
162 message += ` at ${bold(dbLocation)}`
163 }
164
165 return message
166 }
167
168 return undefined
169}
170
171// returns the "host" like localhost / 127.0.0.1 + default port
172export function getDbLocation(credentials: DatabaseCredentials): string | undefined {

Callers 5

parseMethod · 0.90
parseMethod · 0.90
parseMethod · 0.90
parseMethod · 0.90

Calls 5

canConnectToDatabaseFunction · 0.90
createDatabaseFunction · 0.90
uriToCredentialsFunction · 0.90
prettifyProviderFunction · 0.85
getDbLocationFunction · 0.85

Tested by

no test coverage detected