Skip to content

Commit

Permalink
Getting table names from schema (#237)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4017

This pull request is just a small part of a larger project that involves exporting all our database schemas, converting them into CSV files, and uploading them to our Amazon S3 bucket. This PR's primary focus is to get the table names from the database using only the schema name provided. To expedite the export process and see the output sooner, we are using a vertical slicing approach, rather than a horizontal one, which means exporting a single table at a time from each schema.
  • Loading branch information
Beckyrose200 authored May 22, 2023
1 parent c1f491a commit 1830972
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
47 changes: 47 additions & 0 deletions app/services/db-export/fetch-table-names.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

/**
* Fetches all the table names from a given schema
* @module FetchTableNames
*/

const { db } = require('../../../db/db.js')

/**
* Retrieves the table names for a specific schema
*
* @param schemaName The name of the schema from which to fetch the table names
*
* @returns {String[]} Table names for the specified schema
*/
async function go (schemaName) {
const tableData = await _fetchTableNames(schemaName)

// tableData has information we do not need
const tableNames = _pluckTableNames(tableData.rows)

if (tableNames.length === 0) {
throw new Error('Error: Unable to fetch table names')
}

return tableNames
}

async function _fetchTableNames (schemaName) {
const query = `
SELECT table_name
FROM information_schema.tables
WHERE table_schema = '${schemaName}'
AND table_type = 'BASE TABLE';
`

return await db.raw(query)
}

function _pluckTableNames (tableData) {
return tableData.map(obj => obj.table_name)
}

module.exports = {
go
}
32 changes: 32 additions & 0 deletions test/services/db-export/fetch-table-names.service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it } = exports.lab = Lab.script()
const { expect } = Code

// Thing under test
const FetchTableNames = require('../../../app/services/db-export/fetch-table-names.service')

describe('Fetch table names', () => {
describe('when given a schema name', () => {
it('returns a list of the schemas table names', async () => {
const result = await FetchTableNames.go('water')

expect(result).to.include('billing_charge_categories')
expect(result).to.include('charge_purposes')
expect(result).to.include('billing_batches')
})
})

describe('when not given a schema name', () => {
it('throws an error', async () => {
const result = await expect(FetchTableNames.go()).to.reject()

expect(result).to.be.an.error()
expect(result.message).to.equal('Error: Unable to fetch table names')
})
})
})

0 comments on commit 1830972

Please sign in to comment.