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

Create crm_v2 schema views #556

Merged
merged 7 commits into from
Nov 28, 2023
Merged

Create crm_v2 schema views #556

merged 7 commits into from
Nov 28, 2023

Conversation

Cruikshanks
Copy link
Member

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

As part of the work we have been doing on two-part tariff we are going to be creating all our new tables in the default public schema.

We have also decided that when there is a legacy table that we are still going to need we will create a View of it in the public schema. This allows us to correct any issues with naming conventions, strip out unused fields, and join entities that are currently sat in different schemas. The first example of this approach was done in PR #531 .

This change adds the migrations needed to create views for the tables we are currently using from crm_v2 schema.

We'll follow this up with new models and test helpers in another change. The final step is then to refactor the existing code to use the new models and delete the old legacy ones.

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

As part of the work we have been doing on two-part tariff we are going to be creating all our new tables in the default `public` schema.

We have also decided that when there is a legacy table that we are still going to need we will create a [View](https://www.postgresql.org/docs/current/sql-createview.html) of it in the `public` schema. This allows us to correct any issues with naming conventions, strip out unused fields, and join entities that are currently sat in different schemas. The first example of this approach was done in PR #531 .

This change adds the migrations needed to create views for the tables we are currently using from `crm_v2` schema.

> We'll follow this up with new models and test helpers in another change. The final step is then to refactor the existing code to use the new models and delete the old legacy ones.
@Cruikshanks Cruikshanks added the housekeeping Refactoring, tidying up or other work which supports the project label Nov 28, 2023
@Cruikshanks Cruikshanks self-assigned this Nov 28, 2023
diff --git a/db/migrations/public/20231128140249_create-billing-accounts-view.js b/db/migrations/public/20231128140249_create-billing-accounts-view.js
new file mode 100644
index 0000000..279c299
--- /dev/null
+++ b/db/migrations/public/20231128140249_create-billing-accounts-view.js
@@ -0,0 +1,29 @@
+'use strict'
+
+const viewName = 'billing_accounts'
+
+exports.up = function (knex) {
+  return knex
+    .schema
+    .createView(viewName, (view) => {
+      // NOTE: We have commented out unused columns from the source table
+      view.as(knex('invoice_accounts').withSchema('crm_v2').select([
+        'invoice_accounts.invoice_account_id AS id',
+        'invoice_accounts.company_id',
+        'invoice_accounts.invoice_account_number AS account_number',
+        // 'invoice_accounts.start_date', // is populated but is never displayed in the UI or used
+        // 'invoice_accounts.end_date', // is null for all records
+        // 'invoice_accounts.is_test', // we ignore this legacy test column in tables
+        'invoice_accounts.last_transaction_file_reference AS last_transaction_file',
+        'invoice_accounts.date_last_transaction_file_reference_updated AS last_transaction_file_created_at',
+        'invoice_accounts.date_created AS created_at',
+        'invoice_accounts.date_updated AS updated_at'
+      ]))
+    })
+}
+
+exports.down = function (knex) {
+  return knex
+    .schema
+    .dropViewIfExists(viewName)
+}
diff --git a/db/migrations/public/20231128143653_create-companies-view.js b/db/migrations/public/20231128143653_create-companies-view.js
new file mode 100644
index 0000000..9897717
--- /dev/null
+++ b/db/migrations/public/20231128143653_create-companies-view.js
@@ -0,0 +1,30 @@
+'use strict'
+
+const viewName = 'companies'
+
+exports.up = function (knex) {
+  return knex
+    .schema
+    .createView(viewName, (view) => {
+      // NOTE: We have commented out unused columns from the source table
+      view.as(knex('companies').withSchema('crm_v2').select([
+        'companies.company_id AS id',
+        'companies.name',
+        'companies.type',
+        'companies.company_number',
+        // 'companies.external_id', // is populated for companies migrated from NALD but not actually used
+        // 'companies.is_test', // we ignore this legacy test column in tables
+        'companies.organisation_type',
+        // 'companies.last_hash', // is populated but is only used by the legacy import process
+        // 'companies.current_hash', // is populated but is only used by the legacy import process
+        'companies.date_created AS created_at',
+        'companies.date_updated AS updated_at'
+      ]))
+    })
+}
+
+exports.down = function (knex) {
+  return knex
+    .schema
+    .dropViewIfExists(viewName)
+}
@Cruikshanks Cruikshanks marked this pull request as ready for review November 28, 2023 14:57
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 d8ad52a into main Nov 28, 2023
6 checks passed
@Cruikshanks Cruikshanks deleted the create-crm-v2-schema-views branch November 28, 2023 17:37
Cruikshanks added a commit that referenced this pull request Dec 1, 2023
https://eaflood.atlassian.net/browse/WATER-4057

As part of the work we have been doing on two-part tariff we are going to be creating all our new tables in the default `public` schema.

We have also decided that when there is a legacy table that we are still going to need we will create a [View](https://www.postgresql.org/docs/current/sql-createview.html) of it in the `public` schema. This allows us to correct any issues with naming conventions, strip out unused fields, and join entities that are currently sat in different schemas. The first example of this approach was done in PR #531 .

This change adds the models and helpers for the views in the `crm_v2` schema that were created in PR #556

> The final step will then be to refactor the existing code to use the new models and delete the old legacy ones.
Cruikshanks added a commit that referenced this pull request Dec 4, 2023
https://eaflood.atlassian.net/browse/WATER-4057

As part of the work we have been doing on two-part tariff we are going to be creating all our new tables in the default `public` schema.

We have also decided that when there is a legacy table that we are still going to need we will create a [View](https://www.postgresql.org/docs/current/sql-createview.html) of it in the `public` schema. This allows us to correct any issues with naming conventions, strip out unused fields, and join entities that are currently sat in different schemas. The first example of this approach was done in PR #531 .

This change adds the models and helpers for the views in the `crm_v2` schema that were created in PR #556

> The final step will then be to refactor the existing code to use the new models and delete the old legacy ones.
Cruikshanks added a commit that referenced this pull request Dec 4, 2023
https://eaflood.atlassian.net/browse/WATER-4057

As part of the work we have been doing on two-part tariff, we will be creating all our new tables in the default `public` schema.

We have also decided that when there is a legacy table that we are still going to need we will create a [View](https://www.postgresql.org/docs/current/sql-createview.html) of it in the `public` schema. This allows us to correct any issues with naming conventions, strip out unused fields, and join entities currently sat in different schemas. The first example of this approach was done in PR #531 .

We created the new views in [Create CRM_V2 schema views](#556). Then in [Create CRM_V2 schema models and helpers](#561) we added the new models and helpers that use them.

This is the final step in the process, we are refactoring any use of the legacy models to use the new ones and deleting all the legacy-based code.
Cruikshanks added a commit that referenced this pull request Dec 5, 2023
https://eaflood.atlassian.net/browse/WATER-4057

As part of the work we have been doing on two-part tariff, we will be creating all our new tables in the default `public` schema.

We have also decided that when there is a legacy table that we are still going to need we will create a [View](https://www.postgresql.org/docs/current/sql-createview.html) of it in the `public` schema. This allows us to correct any issues with naming conventions, strip out unused fields, and join entities currently sat in different schemas. The first example of this approach was done in PR #531 .

We created the new views in [Create CRM_V2 schema views](#556). Then in [Create CRM_V2 schema models and helpers](#561) we added the new models and helpers that use them.

This is the final step in the process, we are refactoring any use of the legacy models to use the new ones and deleting all the legacy-based code.
Cruikshanks added a commit that referenced this pull request Feb 20, 2024
https://eaflood.atlassian.net/browse/WATER-4365

> For context this came out of us working on re-implementing the SROC annual bill run using what we've learnt and components from our supplementary billing engine.

As part of looking at re-implementing the SROC annual billing engine in this project our spike (WATER-4348 ) confirmed we could simplify the implementation and improve performance if we extracted the billing account details at the same time as the charge versions to be billed.

When we implemented the SROC supplementary billing engine we were directly working with the legacy schemas and tables. This meant we couldn't create relationships in models that sat in different schemas. But thanks to [Create water schema views](#551) and [Create crm_v2 schema views](#556) they are hidden away from us. We can now work as if the legacy schemas are a single schema.

So, this change updates the `ChargeVersionModel` and `BillingAccountModel` to add a relationship between them which we can then exploit with our [Objection.js queries](https://vincit.github.io/objection.js/).
Cruikshanks added a commit that referenced this pull request Feb 20, 2024
https://eaflood.atlassian.net/browse/WATER-4365

> For context this came out of us working on re-implementing the SROC annual bill run using what we've learnt and components from our supplementary billing engine.

As part of looking at re-implementing the SROC annual billing engine in this project our spike (WATER-4348 ) confirmed we could simplify the implementation and improve performance if we extracted the billing account details at the same time as the charge versions to be billed.

When we implemented the SROC supplementary billing engine we were directly working with the legacy schemas and tables. This meant we couldn't create relationships in models that sat in different schemas. But thanks to [Create water schema views](#551) and [Create crm_v2 schema views](#556) they are hidden away from us. We can now work as if the legacy schemas are a single schema.

So, this change updates the `ChargeVersionModel` and `BillingAccountModel` to add a relationship between them which we can then exploit with our [Objection.js queries](https://vincit.github.io/objection.js/).
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.

3 participants