Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add legacy db snake case mappers #131

Merged
merged 4 commits into from
Feb 24, 2023
Merged

Conversation

Cruikshanks
Copy link
Member

https://eaflood.atlassian.net/browse/WATER-3896

Whilst working on Add SROC Supplementary Billing Invoice Service we hit an issue. It was the first time we needed to query the crm_v2 schema. But that threw errors due to the way Objection.js knexSnakeCaseMappers() works. It uses Knex's wrapIdentifier() and postProcessResponse() hooks to see each 'identifier' name. It can then test whether it needs converting, either from or to snake case.

For example, knex('table').withSchema('foo').select('table.field as otherName').where('id', 1) will call wrapIdentifier() for the values 'table', 'foo', 'table', 'field', 'otherName' and 'id'.

knexSnakeCaseMappers() takes some options, one of them being underscoreBeforeDigits. If we didn't use this, fields like address_line_1 or section_127_agreement in the DB would be incorrectly converted to address_line1 and section127_agreement. But this is what leads to our problem.

The previous teams' decision to name one of the schemas crm_v2 leads to an incorrect conversion. knexSnakeCaseMappers() is seeing this and returning 'crm_v_2'. It doesn't know it's a schema instead of a column name because Knex doesn't provide that context. This is the exact same issue we faced in Make timestamps consistent at model layer.

Thankfully, a solution that didn't work there will work here. We can provide our own custom Knex snake case mappers implementation which knows to ignore 'crm_v_2', whilst calling Objection.js own methods for everything else. It also gives us a solution if we face any more funnies like this when dealing with the legacy database.

https://eaflood.atlassian.net/browse/WATER-3896

Whilst working on [Add SROC Supplementary Billing Invoice Service](#119) we hit an issue. It was the first time we needed to query the `crm_v2` schema. But that threw errors due to the way [Objection.js knexSnakeCaseMappers()](https://vincit.github.io/objection.js/api/objection/#knexsnakecasemappers) works. It uses Knex's [wrapIdentifier()](https://knexjs.org/guide/#wrapidentifier) and [postProcessResponse()](https://knexjs.org/guide/#postprocessresponse) hooks to see each 'identifier' name. It can then test whether it needs converting, either from or to snake case.

For example, `knex('table').withSchema('foo').select('table.field as otherName').where('id', 1)` will call `wrapIdentifier()` for the values `'table'`, `'foo'`, `'table'`, `'field'`, `'otherName'` and `'id'`.

**knexSnakeCaseMappers()** takes some options, one of them being `underscoreBeforeDigits`. If we didn't use this fields like `address_line_1` or `section_127_agreement` in the DB would be incorrectly converted to `address_line1` and `section127_agreement`. But this is what leads to our problem.

The previous teams' decision to name one of the schemas `crm_v2` leads to an incorrect conversion. **knexSnakeCaseMappers()** is seeing this and returning `'crm_v_2'`. It doesn't know it's a schema instead of a column name because Knex doesn't provide that context. This is the exact same issue we faced in [Make timestamps consistent at model layer](#85).

Thankfully, a solution that didn't work there will work here. We can provide our own custom knex snake case mappers implementation which knows to ignore `'crm_v_2'`, whilst calling Objection.js own methods for everything else. It also gives us a solution if we face any more funnies like this when dealing with the legacy data.
@Cruikshanks Cruikshanks added the housekeeping Refactoring, tidying up or other work which supports the project label Feb 24, 2023
@Cruikshanks Cruikshanks self-assigned this Feb 24, 2023
@Cruikshanks Cruikshanks changed the title Add custom knex snake case mappers Add legacy knex snake case mappers Feb 24, 2023
@Cruikshanks Cruikshanks changed the title Add legacy knex snake case mappers Add legacy db snake case mappers Feb 24, 2023
We're testing the behaviour we expect from the mappers; that they do what we expect with everything but 'crm_v2' which should just be ignored.

(See note about our failed attempts to confirm how Objection is being used. It's not critical though. I was just a nice to have!)
@Cruikshanks Cruikshanks marked this pull request as ready for review February 24, 2023 09:50
Copy link
Contributor

@Jozzey Jozzey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cruikshanks Cruikshanks merged commit 7e7cf8d into main Feb 24, 2023
@Cruikshanks Cruikshanks deleted the add-custom-snake-case-mappers branch February 24, 2023 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
housekeeping Refactoring, tidying up or other work which supports the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants