(connectionString: string, cwd = process.cwd(), schemaEnginePath?: string)
| 106 | |
| 107 | // could be refactored with engines using JSON RPC instead and just passing the schema |
| 108 | export async function createDatabase(connectionString: string, cwd = process.cwd(), schemaEnginePath?: string) { |
| 109 | const dbExists = await canConnectToDatabase(connectionString, cwd, schemaEnginePath) |
| 110 | |
| 111 | // If database is already created, stop here, don't create it |
| 112 | if (dbExists === true) { |
| 113 | return false |
| 114 | } |
| 115 | |
| 116 | try { |
| 117 | await execaCommand({ |
| 118 | connectionString, |
| 119 | cwd, |
| 120 | schemaEnginePath, |
| 121 | engineCommandName: 'create-database', |
| 122 | }) |
| 123 | |
| 124 | return true |
| 125 | } catch (_e) { |
| 126 | const e = _e as ExecaError |
| 127 | |
| 128 | if (e.stderr) { |
| 129 | const logs = parseJsonFromStderr(e.stderr) |
| 130 | const error = logs.find((it) => it.level === 'ERROR' && it.target === 'schema_engine::logger') |
| 131 | |
| 132 | if (error && error.fields.error_code && error.fields.message) { |
| 133 | throw new Error(`${error.fields.error_code}: ${error.fields.message}`) |
| 134 | } else { |
| 135 | throw new Error(`Schema engine error:\n${logs.map((log) => log.fields.message).join('\n')}`) |
| 136 | } |
| 137 | } else { |
| 138 | throw new Error(`Schema engine exited. ${_e}`) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | export async function dropDatabase(connectionString: string, cwd = process.cwd(), schemaEnginePath?: string) { |
| 144 | try { |
no test coverage detected