Skip to content

Commit

Permalink
chore: work in progress
Browse files Browse the repository at this point in the history
Signed-off-by: mateonunez <mateonunez95@gmail.com>
  • Loading branch information
mateonunez committed Apr 21, 2023
1 parent 2f785ea commit fbfdfe3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 0 additions & 1 deletion packages/drill/lib/database.js
Original file line number Diff line number Diff line change
@@ -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'}`
}
Expand Down
3 changes: 0 additions & 3 deletions packages/drill/lib/queries/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)}`)
Expand Down Expand Up @@ -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}`
Expand Down
2 changes: 1 addition & 1 deletion packages/drill/lib/queries/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
}
Expand Down
18 changes: 18 additions & 0 deletions packages/drill/test/postgres.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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: {
Expand All @@ -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)
Expand All @@ -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: {
Expand All @@ -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()
})

0 comments on commit fbfdfe3

Please sign in to comment.