|
|
@ -372,70 +372,30 @@ class DatabricksClient extends KnexClient { |
|
|
|
const _func = this.createDatabaseIfNotExists.name; |
|
|
|
const _func = this.createDatabaseIfNotExists.name; |
|
|
|
const result = new Result(); |
|
|
|
const result = new Result(); |
|
|
|
log.api(`${_func}:args:`, args); |
|
|
|
log.api(`${_func}:args:`, args); |
|
|
|
let tempSqlClient; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
const connectionParamsWithoutDb = JSON.parse( |
|
|
|
// check if catalog exists
|
|
|
|
JSON.stringify(this.connectionConfig), |
|
|
|
const catalogs = await this.sqlClient.raw(`SHOW CATALOGS LIKE ?`, [ |
|
|
|
); |
|
|
|
args.database, |
|
|
|
let rows = []; |
|
|
|
]); |
|
|
|
try { |
|
|
|
|
|
|
|
connectionParamsWithoutDb.connection.database = 'postgres'; |
|
|
|
|
|
|
|
tempSqlClient = knex({ |
|
|
|
|
|
|
|
...connectionParamsWithoutDb, |
|
|
|
|
|
|
|
pool: { min: 0, max: 1 }, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.debug('checking if db exists'); |
|
|
|
if (catalogs.length === 0) { |
|
|
|
rows = ( |
|
|
|
throw new Error( |
|
|
|
await tempSqlClient.raw( |
|
|
|
'We do not support creating catalogs yet, please use an existing catalog', |
|
|
|
`SELECT datname as database FROM pg_database WHERE datistemplate = false and datname = ?`, |
|
|
|
); |
|
|
|
[args.database], |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
).rows; |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
log.debug('checking if db exists'); |
|
|
|
|
|
|
|
rows = ( |
|
|
|
|
|
|
|
await this.sqlClient.raw( |
|
|
|
|
|
|
|
`SELECT datname as database FROM pg_database WHERE datistemplate = false and datname = ?`, |
|
|
|
|
|
|
|
[args.database], |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
).rows; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (rows.length === 0) { |
|
|
|
|
|
|
|
log.debug('creating database:', args); |
|
|
|
|
|
|
|
await tempSqlClient.raw(`CREATE DATABASE ?? ENCODING 'UTF8'`, [ |
|
|
|
|
|
|
|
args.database, |
|
|
|
|
|
|
|
]); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const schemaName = this.connectionConfig.schema || 'default'; |
|
|
|
// check if database exists
|
|
|
|
|
|
|
|
const databases = await this.sqlClient.raw(`SHOW DATABASES LIKE ?`, [ |
|
|
|
// Check schemaExists because `CREATE SCHEMA IF NOT EXISTS` requires permissions of `CREATE ON DATABASE`
|
|
|
|
args.schema, |
|
|
|
const schemaExists = !!( |
|
|
|
]); |
|
|
|
await this.sqlClient.raw( |
|
|
|
|
|
|
|
`SELECT schema_name FROM information_schema.schemata WHERE schema_name = ?`, |
|
|
|
|
|
|
|
[schemaName], |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
).rows?.[0]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!schemaExists) { |
|
|
|
if (databases.length === 0) { |
|
|
|
await this.sqlClient.raw( |
|
|
|
await this.sqlClient.raw(`CREATE DATABASE ${args.schema}`); |
|
|
|
`CREATE SCHEMA IF NOT EXISTS ?? AUTHORIZATION ?? `, |
|
|
|
|
|
|
|
[schemaName, this.connectionConfig.connection.user], |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// this.sqlClient = knex(this.connectionConfig);
|
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
log.ppe(e, _func); |
|
|
|
log.ppe(e, _func); |
|
|
|
throw e; |
|
|
|
throw e; |
|
|
|
} finally { |
|
|
|
|
|
|
|
if (tempSqlClient) { |
|
|
|
|
|
|
|
await tempSqlClient.destroy(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
log.api(`${_func}: result`, result); |
|
|
|
log.api(`${_func}: result`, result); |
|
|
@ -2555,7 +2515,12 @@ class DatabricksClient extends KnexClient { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
get schema(): string { |
|
|
|
get schema(): string { |
|
|
|
return (this.connectionConfig && this.connectionConfig.schema) || 'default'; |
|
|
|
return ( |
|
|
|
|
|
|
|
(this.connectionConfig && |
|
|
|
|
|
|
|
this.connectionConfig.connection && |
|
|
|
|
|
|
|
this.connectionConfig.connection.schema) || |
|
|
|
|
|
|
|
'default' |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|