diff --git a/packages/drill/lib/database.js b/packages/drill/lib/database.js index a26dc05b..a68dd54a 100644 --- a/packages/drill/lib/database.js +++ b/packages/drill/lib/database.js @@ -1,6 +1,5 @@ export function generateConnectionString ({ host, port, user, password, databaseName }) { return { - /* c8 ignore next 2 */ postgres: `postgres://${user}:${encodeURIComponent(password)}@${host}:${port}${databaseName ? `/${databaseName}` : '/db'}`, mysql: `mysql://${user}:${encodeURIComponent(password)}@${host}:${port}${databaseName ? `/${databaseName}` : '/db'}` } diff --git a/packages/drill/lib/queries/common.js b/packages/drill/lib/queries/common.js index a30ef33d..b0ae1816 100644 --- a/packages/drill/lib/queries/common.js +++ b/packages/drill/lib/queries/common.js @@ -32,7 +32,6 @@ async function selectData (logger, db, sql, tableName, columns = '*', where = {} if (logger) logger.info(`Selecting data from "${tableName}".`) const columnDefinitions = [] const values = [] - /* c8 ignore next 8 */ if (columns === '*') { columnDefinitions.push(sql`*`) } else { @@ -41,7 +40,6 @@ async function selectData (logger, db, sql, tableName, columns = '*', where = {} values.push(value) } } - /* c8 ignore next 10 */ const conditions = [] for (const [name, value] of Object.entries(where)) { conditions.push(sql`${sql.ident(name)} = ${sql.value(value)}`) @@ -70,7 +68,6 @@ async function dropDatabase (logger, db, sql, database) { async function createTable (logger, db, sql, tableName, columns, options) { if (logger) logger.info(`Creating table "${tableName}".`) const columnDefinitions = [] - /* c8 ignore next 6 */ for (const [name, definition] of Object.entries(columns)) { const dangerousRaw = sql.__dangerous__rawValue(`${definition.type + (definition.length ? `(${definition.length})` : '')} ${definition.autoIncrement ? 'AUTO_INCREMENT' : ''} ${definition.primaryKey ? 'PRIMARY KEY' : ''}`) const row = sql`${sql.ident(name)} ${dangerousRaw}` diff --git a/packages/drill/lib/queries/postgres.js b/packages/drill/lib/queries/postgres.js index 61d25f43..a4d20d1c 100644 --- a/packages/drill/lib/queries/postgres.js +++ b/packages/drill/lib/queries/postgres.js @@ -25,7 +25,7 @@ async function getTables (logger, db, sql) { WHERE table_schema = 'public' AND table_type = 'BASE TABLE' `) - /* c8 ignore next 3 */ + console.log({ response }) if (response.length === 0) { if (logger) logger.warn('No tables found.') } diff --git a/packages/drill/test/postgres.test.js b/packages/drill/test/postgres.test.js index 9c345e7e..87500344 100644 --- a/packages/drill/test/postgres.test.js +++ b/packages/drill/test/postgres.test.js @@ -12,6 +12,7 @@ test('should create a new database', async ({ ok, end }) => { await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true }) await killDatabase(_db) const { db } = await setupDatabase(logger, 'postgres', { ...postgresOptions, databaseName }) + await queryer[privateMethods].dropDatabase(databaseName) await killDatabase(db) ok(_db) @@ -20,6 +21,8 @@ test('should create a new database', async ({ ok, end }) => { test('should retrieve tables', async ({ ok, end }) => { const { db, queryer } = await setupDatabase(logger, 'postgres', postgresOptions) + const databaseName = 'new_database_2' + await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true }) const tableName = 'test' await queryer[privateMethods].createTable(tableName, { id: { @@ -33,6 +36,7 @@ test('should retrieve tables', async ({ ok, end }) => { }, { dropIfExists: true }) const tables = await queryer.getTables() + await queryer[privateMethods].dropDatabase(databaseName) await killDatabase(db) ok(tables.length > 0) @@ -41,6 +45,8 @@ test('should retrieve tables', async ({ ok, end }) => { test('should retrieve single table', async ({ ok, end }) => { const { db, queryer } = await setupDatabase(logger, 'postgres', postgresOptions) + const databaseName = 'new_database_3' + await queryer[privateMethods].createDatabase(databaseName, { dropIfExists: true }) const tableName = 'test' await queryer[privateMethods].createTable(tableName, { id: { @@ -54,8 +60,20 @@ test('should retrieve single table', async ({ ok, end }) => { }, { dropIfExists: true }) const tables = await queryer.getTable('test') + await queryer[privateMethods].dropDatabase(databaseName) await killDatabase(db) ok(tables) end() }) + +test('should log warning when table does not exist', async ({ same, end }) => { + const { db, queryer } = await setupDatabase(undefined, 'postgres', postgresOptions) + await queryer[privateMethods].createDatabase('empty_database', { dropIfExists: true }) + const tables = await queryer.getTables() + await queryer[privateMethods].dropDatabase('empty_database') + await killDatabase(db) + + same(tables, []) + end() +})