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

Fix model migrations since last release #1108

Merged
merged 10 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/financial-agreement.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FinancialAgreementModel extends BaseModel {

static get relationMappings () {
return {
licenceAgreementTypes: {
licenceAgreements: {
relation: Model.HasManyRelation,
modelClass: 'licence-agreement.model',
join: {
Expand Down
4 changes: 2 additions & 2 deletions app/models/licence-agreement.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class LicenceAgreementModel extends BaseModel {

static get relationMappings () {
return {
financialAgreements: {
relation: Model.HasManyRelation,
financialAgreement: {
relation: Model.HasOneRelation,
modelClass: 'financial-agreement.model',
join: {
from: 'licenceAgreements.financialAgreementId',
Expand Down
9 changes: 5 additions & 4 deletions app/models/scheduled-notification.model.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
'use strict'

/**
* Model for scheduled_notification (water.scheduled_notification)
* Model for scheduled_notifications (water.scheduled_notification)
* @module ScheduledNotificationModel
*/

const BaseModel = require('./base.model.js')
const { Model } = require('objection')

const BaseModel = require('./base.model.js')

class ScheduledNotificationModel extends BaseModel {
static get tableName () {
return 'scheduled_notification'
return 'scheduledNotifications'
}

static get relationMappings () {
Expand All @@ -19,7 +20,7 @@ class ScheduledNotificationModel extends BaseModel {
relation: Model.HasOneRelation,
modelClass: 'event.model',
join: {
from: 'scheduled_notification.eventId',
from: 'scheduledNotifications.eventId',
to: 'events.id'
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/presenters/licences/set-up.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function _agreements (commonData, agreements, auth) {
startDate: formatLongDate(agreement.startDate),
endDate: agreement.endDate ? formatLongDate(agreement.endDate) : '',
description: agreementDescriptions[_financialAgreementCode(agreement)],
dateSigned: agreement.dateSigned ? formatLongDate(agreement.dateSigned) : '',
signedOn: agreement.signedOn ? formatLongDate(agreement.signedOn) : '',
action: _agreementActionLinks(commonData, agreement, auth)
}
})
Expand Down Expand Up @@ -167,7 +167,7 @@ function _endsSixYearsAgo (endDate) {
}

function _financialAgreementCode (agreement) {
return agreement.financialAgreements[0].financialAgreementCode
return agreement.financialAgreement.code
}

function _returnVersions (returnVersions = [{}]) {
Expand Down
11 changes: 6 additions & 5 deletions app/services/licences/fetch-agreements.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const LicenceAgreementModel = require('../../models/licence-agreement.model.js')

/**
* Fetches charge version data needed for the view '/licences/{id}/set-up` page
* Fetches licence agreements data needed for the view '/licences/{id}/set-up` page
*
* @param {string} licenceRef - The licence ref for the licence to fetch licence agreements for
*
Expand All @@ -25,12 +25,13 @@ async function _fetch (licenceRef) {
'id',
'startDate',
'endDate',
'dateSigned'
'signedOn'
])
.withGraphFetched('financialAgreements')
.modifyGraph('financialAgreements', (builder) => {
.withGraphFetched('financialAgreement')
.modifyGraph('financialAgreement', (builder) => {
builder.select([
'financialAgreementCode'
'id',
'code'
])
})
.orderBy([
Expand Down
8 changes: 4 additions & 4 deletions app/services/licences/fetch-communications.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ async function _fetch (licenceRef, page) {
'messageRef'
])
.where('licences', '@>', `["${licenceRef}"]`)
.andWhere('notify_status', 'in', ['delivered', 'received'])
.andWhere('notifyStatus', 'in', ['delivered', 'received'])
.andWhere('eventId', 'is not', null)
.withGraphFetched('event')
.modifyGraph('event', (builder) => {
builder.select([
'createdAt',
'issuer',
'metadata',
'type',
'subtype',
'status',
'issuer'
'subtype',
'type'
])
})
.page(page - 1, DatabaseConfig.defaultPageSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ async function _fetch (licenceRef) {
.where('licenceRef', licenceRef)
.whereNull('endDate')
.orWhere('endDate', '>=', new Date())
.withGraphFetched('financialAgreements')
.modifyGraph('financialAgreements', (builder) => {
.withGraphFetched('financialAgreement')
.modifyGraph('financialAgreement', (builder) => {
builder.select([
'id'
])
.where('financialAgreementCode', 'S127')
.where('code', 'S127')
})
}

Expand Down
2 changes: 1 addition & 1 deletion app/views/licences/tabs/set-up.njk
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
text: agreement.description
},
{
text: agreement.signedDate
text: agreement.signedOn
},
{
html: createLink(agreement)
Expand Down
27 changes: 11 additions & 16 deletions db/migrations/legacy/20221108003009_crm-v2-company-contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

const tableName = 'company_contacts'

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex
.schema
Expand All @@ -15,26 +11,25 @@ exports.up = function (knex) {
table.uuid('company_contact_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.boolean('is_default')
table.boolean('is_test')
table.boolean('water_abstraction_alerts_enabled')
table.date('end_date')
table.date('start_date')
table.uuid('company_id').notNullable()
table.uuid('contact_id').notNullable()
table.uuid('role_id').notNullable()
table.boolean('is_default').notNullable().defaultTo(false)
table.string('email_address')
table.uuid('company_id')
table.uuid('contact_id')
table.uuid('role_id')
table.date('start_date').notNullable()
table.date('end_date')
table.boolean('is_test').notNullable().defaultTo(false)
table.boolean('water_abstraction_alerts_enabled').defaultTo(false)

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())
table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now())

// Constraints
table.unique(['company_id', 'contact_id', 'role_id', 'start_date'], { useConstraint: true })
})
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex
.schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ exports.up = function (knex) {
table.decimal('daily_quantity')
table.decimal('hourly_quantity')
table.decimal('annual_quantity')
table.string('external_id').unique()
table.boolean('is_test')
table.string('external_id').notNullable()
table.boolean('is_test').notNullable().default(false)

// Legacy timestamps
// NOTE: They are not automatically set
table.dateTime('date_created').notNullable()
table.dateTime('date_updated').notNullable()

// Constraints
table.unique(['external_id'], { useConstraint: true })
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ exports.up = function (knex) {
table.uuid('financial_agreement_type_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.string('financial_agreement_code')
table.string('description')
table.boolean('disabled')
table.boolean('is_test')
table.string('financial_agreement_code').notNullable()
table.string('description').notNullable()
table.boolean('disabled').default(false)
table.boolean('is_test').notNullable().default(false)

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())
Expand Down
10 changes: 5 additions & 5 deletions db/migrations/legacy/20221108007031_water-licence-agreements.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ exports.up = function (knex) {
table.uuid('licence_agreement_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.uuid('financial_agreement_type_id')
table.string('licence_ref')
table.date('start_date')
table.uuid('financial_agreement_type_id').notNullable()
table.string('licence_ref').notNullable()
table.date('start_date').notNullable()
table.date('end_date')
table.date('date_signed')
table.date('date_deleted')
table.string('source')
table.boolean('is_test')
table.string('source').notNullable().default('wrls')
table.boolean('is_test').notNullable().default(false)

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

const tableName = 'scheduled_notification'

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex
.schema
Expand All @@ -15,40 +11,35 @@ exports.up = function (knex) {
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.bigint('status_checks')
table.date('send_after')
table.integer('notification_type')
table.jsonb('licences')
table.jsonb('metadata')
table.string('recipient')
table.string('message_type')
table.string('message_ref')
table.jsonb('personalisation')
table.string('company_entity_id')
table.string('individual_entity_id')
table.string('job_id').unique()
table.timestamp('send_after')
table.string('status')
table.string('log')
table.jsonb('licences')
table.string('individual_entity_id')
table.string('company_entity_id')
table.string('medium')
table.string('message_ref')
table.string('message_type')
table.string('notify_id')
table.string('notify_status')
table.string('plaintext')
table.string('recipient')
table.string('status')
table.timestamp('next_status_check')
table.uuid('event_id')
table.jsonb('metadata')
table.bigint('status_checks')
table.timestamp('next_status_check')
table.decimal('notification_type')
table.string('job_id')

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now())
table.timestamp('date_created', { useTz: false }).notNullable()

// Constraints
table.unique(['job_id'], { useConstraint: true })
})
.raw(`
CREATE INDEX idx_scheduled_notification_statuses ON water.scheduled_notification USING btree (status, notify_status);
CREATE INDEX scheduled_notification_idx_send_after ON water.scheduled_notification USING btree (send_after);
`)
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex
.schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ exports.up = function (knex) {
view.as(knex('licence_version_purposes').withSchema('water').select([
'licence_version_purpose_id AS id',
'licence_version_id',
'purpose_primary_id',
'purpose_secondary_id',
'purpose_primary_id AS primary_purpose_id',
'purpose_secondary_id AS secondary_purpose_id',
'purpose_use_id AS purpose_id',
'abstraction_period_start_day',
'abstraction_period_start_month',
Expand All @@ -37,4 +37,26 @@ exports.down = function (knex) {
return knex
.schema
.dropViewIfExists(viewName)
.createView(viewName, (view) => {
// NOTE: We have commented out unused columns from the source table
view.as(knex('licence_version_purposes').withSchema('water').select([
'licence_version_purpose_id AS id',
'licence_version_id',
'purpose_primary_id',
'purpose_secondary_id',
'purpose_use_id AS purpose_id',
'abstraction_period_start_day',
'abstraction_period_start_month',
'abstraction_period_end_day',
'abstraction_period_end_month',
'time_limited_start_date',
'time_limited_end_date',
'notes',
'annual_quantity',
'external_id',
// 'is_test ',
'date_created AS created_at',
'date_updated AS updated_at'
]))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ exports.up = function (knex) {
return knex
.schema
.createView(viewName, (view) => {
// NOTE: We have commented out unused columns from the source table
view.as(knex('financial_agreement_types').withSchema('water').select([
'financial_agreement_type_id AS id',
'financial_agreement_code',
'financial_agreement_code AS code',
'description',
'disabled',
// 'is_test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ exports.up = function (knex) {
return knex
.schema
.createView(viewName, (view) => {
// NOTE: We have commented out unused columns from the source table
view.as(knex('licence_agreements').withSchema('water').select([
'licence_agreement_id AS id',
'financial_agreement_type_id AS financial_agreement_id',
'licence_ref',
'start_date',
'end_date',
'date_signed',
'date_signed AS signed_on',
'date_deleted AS deleted_at',
'source',
// 'is_test',
Expand Down
Loading
Loading