( pathResolutionRoot: string, provider: ConnectorType, config: PrismaConfigWithDatasource, )
| 129 | type SuccessMessage = string |
| 130 | |
| 131 | export 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 |
| 172 | export function getDbLocation(credentials: DatabaseCredentials): string | undefined { |
no test coverage detected