Skip to content

Commit d95f1bf

Browse files
authored
Merge pull request #23 from icefoganalytics/issue-22/fixup--handle-non-convertable-table-name-identifiers
Fixup: Handle Non-Convertable Table Name Identifiers
2 parents d39259f + cd7b3b1 commit d95f1bf

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

src/api/db/db-client.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { snakeCase } from "lodash"
44

55
import { DB_CONFIG } from "@/config"
66

7+
import { NON_STANDARD_COLUMN_NAMES_TRANSFORMS } from "@/utils/db-wrap-identifier-helpers"
8+
79
const db = knex({
810
client: DB_CONFIG.client,
911
connection: DB_CONFIG.connection,
@@ -16,7 +18,14 @@ const db = knex({
1618
return camelcaseKeys(result, { deep: true })
1719
}
1820
},
19-
wrapIdentifier: (value, origImpl, queryContext) => origImpl(snakeCase(value)),
21+
wrapIdentifier: (value, origImpl, queryContext) => {
22+
const specialValue = NON_STANDARD_COLUMN_NAMES_TRANSFORMS[value]
23+
if (specialValue) {
24+
return origImpl(specialValue)
25+
}
26+
27+
return origImpl(snakeCase(value))
28+
},
2029
})
2130

2231
const dbWithSchema: Knex = new Proxy(db, {
@@ -27,6 +36,8 @@ const dbWithSchema: Knex = new Proxy(db, {
2736
if (typeof target[prop] === "function") {
2837
if (prop === "withSchema") {
2938
return (schema: string) => target[prop](schema) // format taken from src/api/node_modules/knex/types/index.d.ts
39+
} else if (prop === "destroy") {
40+
return (callback: Function) => db.destroy(callback)
3041
} else {
3142
return (...args: any[]) => target[prop](...args).withSchema(DB_CONFIG.defaultSchema)
3243
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
////
2+
// As seen by
3+
// SELECT
4+
// TABLE_SCHEMA + '.' + TABLE_NAME + '.' + COLUMN_NAME
5+
// FROM
6+
// INFORMATION_SCHEMA.COLUMNS
7+
// WHERE
8+
// TABLE_SCHEMA = 'sfa'
9+
// AND COLUMN_NAME LIKE '%[^_][0-9]%'
10+
// ORDER BY
11+
// TABLE_NAME
12+
// , COLUMN_NAME;
13+
//
14+
// Except for:
15+
// - sfa.csl_nars_history.children_over_12_dis
16+
// - sfa.csl_nars_history.children_over_12_not_dis
17+
// - sfa.csl_nars_history.children_to_11
18+
// as they match their snake case value.
19+
20+
// Also ran
21+
// import { NON_STANDARD_COLUMN_NAMES_TRANSFORMS } from "@/utils/db-wrap-identifier-helpers"
22+
// import { snakeCase } from "lodash"
23+
24+
// Object.entries(NON_STANDARD_COLUMN_NAMES_TRANSFORMS).forEach(([key, value]) => {
25+
// if (snakeCase(key) !== value) {
26+
// // console.log(`Key "${key}" does not match its snake-cased value "${snakeCase(key)}".`)
27+
// } else {
28+
// console.log(`Key "${key}" matches its snake-cased value.`)
29+
// }
30+
// })
31+
export const NON_STANDARD_COLUMN_NAMES_TRANSFORMS: { [key: string]: string | undefined } = {
32+
address1: "address1", // sfa.person_address_v.address1, sfa.person_address.address1
33+
address2: "address2", // sfa.person_address_v.address2, sfa.person_address.address2
34+
calscRestrict1: "calsc_restrict1", // sfa.csl_restricted.calsc_restrict1
35+
calscRestrict2: "calsc_restrict2", // sfa.csl_restricted.calsc_restrict2
36+
calscRestrict3: "calsc_restrict3", // sfa.csl_restricted.calsc_restrict3
37+
csgptDep2PhaseOutRate: "csgpt_dep2_phase_out_rate", // sfa.csg_threshold.csgpt_dep2_phase_out_rate
38+
csgptDep3PhaseOutRate: "csgpt_dep3_phase_out_rate", // sfa.csg_threshold.csgpt_dep3_phase_out_rate
39+
fiRestrict1: "fi_restrict1", // sfa.csl_restricted.fi_restrict1
40+
kinAddress1: "kin_address1", // sfa.student.kin_address1
41+
kinAddress2: "kin_address2", // sfa.student.kin_address2
42+
nslscRestrict1: "nslsc_restrict1", // sfa.csl_restricted.nslsc_restrict1
43+
nslscRestrict2: "nslsc_restrict2", // sfa.csl_restricted.nslsc_restrict2
44+
nslscRestrict3: "nslsc_restrict3", // sfa.csl_restricted.nslsc_restrict3
45+
parent1Id: "parent1_id", // sfa.application.parent1_id
46+
parent1IncomeTaxable: "parent1_income_taxable", // sfa.csl_nars_history.parent1_income_taxable
47+
parent1IncomeTaxpaid: "parent1_income_taxpaid", // sfa.csl_nars_history.parent1_income_taxpaid
48+
parent1Income: "parent1_income", // sfa.application.parent1_income, sfa.assessment.parent1_income, sfa.csl_nars_history.parent1_income
49+
parent1NetIncome: "parent1_net_income", // sfa.application.parent1_net_income
50+
parent1PostalCode: "parent1_postal_code", // sfa.csl_nars_history.parent1_postal_code
51+
parent1RelationshipId: "parent1_relationship_id", // sfa.application.parent1_relationship_id
52+
parent1Sin: "parent1_sin", // sfa.csl_nars_history.parent1_sin
53+
parent1TaxPaid: "parent1_tax_paid", // sfa.application.parent1_tax_paid, sfa.assessment.parent1_tax_paid
54+
parent2Id: "parent2_id", // sfa.application.parent2_id
55+
parent2IncomeTaxable: "parent2_income_taxable", // sfa.csl_nars_history.parent2_income_taxable
56+
parent2IncomeTaxpaid: "parent2_income_taxpaid", // sfa.csl_nars_history.parent2_income_taxpaid
57+
parent2Income: "parent2_income", // sfa.application.parent2_income, sfa.assessment.parent2_income, sfa.csl_nars_history.parent2_income
58+
parent2NetIncome: "parent2_net_income", // sfa.application.parent2_net_income
59+
parent2PostalCode: "parent2_postal_code", // sfa.csl_nars_history.parent2_postal_code
60+
parent2RelationshipId: "parent2_relationship_id", // sfa.application.parent2_relationship_id
61+
parent2Sin: "parent2_sin", // sfa.csl_nars_history.parent2_sin
62+
parent2TaxPaid: "parent2_tax_paid", // sfa.application.parent2_tax_paid, sfa.assessment.parent2_tax_paid
63+
rTrans16wk: "r_trans_16wk", // sfa.assessment.r_trans_16wk
64+
spouseLn150Income: "spouse_ln150_income", // sfa.application.spouse_ln150_income, sfa.assessment.spouse_ln150_income
65+
studentLn150Income: "student_ln150_income", // sfa.application.student_ln150_income, sfa.assessment.student_ln150_income
66+
t4aRequired: "t4a_required", // sfa.request_type.t4a_required
67+
taxes1FiledProvinceId: "taxes1_filed_province_id", // sfa.application.taxes1_filed_province_id
68+
taxes1FiledYear: "taxes1_filed_year", // sfa.application.taxes1_filed_year
69+
taxes1NotFiled: "taxes1_not_filed", // sfa.application.taxes1_not_filed
70+
taxes1Verified: "taxes1_verified", // sfa.application.taxes1_verified
71+
taxes2FiledProvinceId: "taxes2_filed_province_id", // sfa.application.taxes2_filed_province_id
72+
taxes2FiledYear: "taxes2_filed_year", // sfa.application.taxes2_filed_year
73+
taxes2NotFiled: "taxes2_not_filed", // sfa.application.taxes2_not_filed
74+
taxes2Verified: "taxes2_verified", // sfa.application.taxes2_verified
75+
}

0 commit comments

Comments
 (0)