-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Getting table names from schema (#237)
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
1 parent
c1f491a
commit 1830972
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |