From 6fefdf376852bfaca3862ee78cee2cb20881b67d Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Tue, 28 Nov 2023 13:07:11 +0000 Subject: [PATCH 1/7] Create `idm` schema views 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 `idm` 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. From ddd39c318d91e150c7f031a785690e9908a3c3b8 Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Tue, 28 Nov 2023 13:36:03 +0000 Subject: [PATCH 2/7] Create users view migration diff --git a/db/migrations/public/20231128131625_create-users-view.js b/db/migrations/public/20231128131625_create-users-view.js new file mode 100644 index 0000000..9f556a0 --- /dev/null +++ b/db/migrations/public/20231128131625_create-users-view.js @@ -0,0 +1,34 @@ +'use strict' + +const viewName = 'users' + +exports.up = function (knex) { + return knex + .schema + .createView(viewName, (view) => { + // NOTE: We have commented out unused columns from the source table + view.as(knex('idm').withSchema('users').select([ + 'users.user_id AS id', + 'users.user_name AS username', + 'users.password', + // 'users.user_data', // inconsistently set and in most cases is {} + 'users.reset_guid', + 'users.reset_required', + 'users.last_login', + 'users.bad_logins', + 'users.application', + // 'users.role', // was made redundant when roles were moved to be stored separately in tables + // 'users.external_id', // was set during a migration of users from the crm schema but is never used + 'users.reset_guid_created_at AS reset_guid_date_created', + 'users.enabled', + 'users.date_created AS created_at', + 'users.date_updated AS updated_at' + ])) + }) +} + +exports.down = function (knex) { + return knex + .schema + .dropViewIfExists(viewName) +} --- .../20231128131625_create-users-view.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 db/migrations/public/20231128131625_create-users-view.js diff --git a/db/migrations/public/20231128131625_create-users-view.js b/db/migrations/public/20231128131625_create-users-view.js new file mode 100644 index 0000000000..bf443056b4 --- /dev/null +++ b/db/migrations/public/20231128131625_create-users-view.js @@ -0,0 +1,34 @@ +'use strict' + +const viewName = 'users' + +exports.up = function (knex) { + return knex + .schema + .createView(viewName, (view) => { + // NOTE: We have commented out unused columns from the source table + view.as(knex('users').withSchema('idm').select([ + 'users.user_id AS id', + 'users.user_name AS username', + 'users.password', + // 'users.user_data', // inconsistently set and in most cases is {} + 'users.reset_guid', + 'users.reset_required', + 'users.last_login', + 'users.bad_logins', + 'users.application', + // 'users.role', // was made redundant when roles were moved to be stored separately in tables + // 'users.external_id', // was set during a migration of users from the crm schema but is never used + 'users.reset_guid_date_created AS reset_guid_created_at', + 'users.enabled', + 'users.date_created AS created_at', + 'users.date_updated AS updated_at' + ])) + }) +} + +exports.down = function (knex) { + return knex + .schema + .dropViewIfExists(viewName) +} From 44d83e907a975222874a8d8781d8ece9a7b6eba3 Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Tue, 28 Nov 2023 13:39:11 +0000 Subject: [PATCH 3/7] Create roles view --- .../20231128133650_create-roles-view.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 db/migrations/public/20231128133650_create-roles-view.js diff --git a/db/migrations/public/20231128133650_create-roles-view.js b/db/migrations/public/20231128133650_create-roles-view.js new file mode 100644 index 0000000000..e73b98f508 --- /dev/null +++ b/db/migrations/public/20231128133650_create-roles-view.js @@ -0,0 +1,25 @@ +'use strict' + +const viewName = 'roles' + +exports.up = function (knex) { + return knex + .schema + .createView(viewName, (view) => { + // NOTE: We have commented out unused columns from the source table + view.as(knex('roles').withSchema('idm').select([ + 'roles.role_id AS id', + // 'roles.application', // is always water_admin + 'roles.role', + 'roles.description', + 'roles.date_created AS created_at', + 'roles.date_updated AS updated_at' + ])) + }) +} + +exports.down = function (knex) { + return knex + .schema + .dropViewIfExists(viewName) +} From 17a344a26a0d2fa093d5944146f34da0927aabef Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Tue, 28 Nov 2023 13:42:26 +0000 Subject: [PATCH 4/7] Add groups view migration --- .../20231128133938_create-groups-view.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 db/migrations/public/20231128133938_create-groups-view.js diff --git a/db/migrations/public/20231128133938_create-groups-view.js b/db/migrations/public/20231128133938_create-groups-view.js new file mode 100644 index 0000000000..b359216cba --- /dev/null +++ b/db/migrations/public/20231128133938_create-groups-view.js @@ -0,0 +1,25 @@ +'use strict' + +const viewName = 'groups' + +exports.up = function (knex) { + return knex + .schema + .createView(viewName, (view) => { + // NOTE: We have commented out unused columns from the source table + view.as(knex('groups').withSchema('idm').select([ + 'groups.group_id AS id', + // 'groups.application', // is always water_admin + 'groups.group', + 'groups.description', + 'groups.date_created AS created_at', + 'groups.date_updated AS updated_at' + ])) + }) +} + +exports.down = function (knex) { + return knex + .schema + .dropViewIfExists(viewName) +} From 9d10fd4b6ec51826812bf7be403e572baa2f39b6 Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Tue, 28 Nov 2023 13:46:29 +0000 Subject: [PATCH 5/7] Add user_roles view migration --- .../20231128134441_create-user-roles-view.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 db/migrations/public/20231128134441_create-user-roles-view.js diff --git a/db/migrations/public/20231128134441_create-user-roles-view.js b/db/migrations/public/20231128134441_create-user-roles-view.js new file mode 100644 index 0000000000..634b5e4dad --- /dev/null +++ b/db/migrations/public/20231128134441_create-user-roles-view.js @@ -0,0 +1,23 @@ +'use strict' + +const viewName = 'user_roles' + +exports.up = function (knex) { + return knex + .schema + .createView(viewName, (view) => { + view.as(knex('user_roles').withSchema('idm').select([ + 'user_roles.user_role_id AS id', + 'user_roles.user_id', + 'user_roles.role_id', + 'user_roles.date_created AS created_at', + 'user_roles.date_updated AS updated_at' + ])) + }) +} + +exports.down = function (knex) { + return knex + .schema + .dropViewIfExists(viewName) +} From ea243d3835e17ce5f9e704858447aa16bc2421be Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Tue, 28 Nov 2023 13:47:55 +0000 Subject: [PATCH 6/7] Add user_groups view migration --- .../20231128134656_create-user-groups-view.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 db/migrations/public/20231128134656_create-user-groups-view.js diff --git a/db/migrations/public/20231128134656_create-user-groups-view.js b/db/migrations/public/20231128134656_create-user-groups-view.js new file mode 100644 index 0000000000..9f836cbb65 --- /dev/null +++ b/db/migrations/public/20231128134656_create-user-groups-view.js @@ -0,0 +1,23 @@ +'use strict' + +const viewName = 'user_groups' + +exports.up = function (knex) { + return knex + .schema + .createView(viewName, (view) => { + view.as(knex('user_groups').withSchema('idm').select([ + 'user_groups.user_group_id AS id', + 'user_groups.user_id', + 'user_groups.group_id', + 'user_groups.date_created AS created_at', + 'user_groups.date_updated AS updated_at' + ])) + }) +} + +exports.down = function (knex) { + return knex + .schema + .dropViewIfExists(viewName) +} From 13f0b4fdad682768fbd989bbbed392457eaa55e2 Mon Sep 17 00:00:00 2001 From: Alan Cruikshanks Date: Tue, 28 Nov 2023 13:50:00 +0000 Subject: [PATCH 7/7] Add group_roles view migration --- .../20231128134839_create-group-roles-view.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 db/migrations/public/20231128134839_create-group-roles-view.js diff --git a/db/migrations/public/20231128134839_create-group-roles-view.js b/db/migrations/public/20231128134839_create-group-roles-view.js new file mode 100644 index 0000000000..208259d56c --- /dev/null +++ b/db/migrations/public/20231128134839_create-group-roles-view.js @@ -0,0 +1,23 @@ +'use strict' + +const viewName = 'group_roles' + +exports.up = function (knex) { + return knex + .schema + .createView(viewName, (view) => { + view.as(knex('group_roles').withSchema('idm').select([ + 'group_roles.group_role_id AS id', + 'group_roles.group_id', + 'group_roles.role_id', + 'group_roles.date_created AS created_at', + 'group_roles.date_updated AS updated_at' + ])) + }) +} + +exports.down = function (knex) { + return knex + .schema + .dropViewIfExists(viewName) +}