diff --git a/db/migrations/20221108143017_add_pgcrypto_extension.js b/db/migrations/20221108143017_add-pgcrypto-extension.js similarity index 100% rename from db/migrations/20221108143017_add_pgcrypto_extension.js rename to db/migrations/20221108143017_add-pgcrypto-extension.js diff --git a/db/migrations/20221108143106_create_update_timestamp_trigger.js b/db/migrations/20221108143106_create-update-timestamp-trigger.js similarity index 100% rename from db/migrations/20221108143106_create_update_timestamp_trigger.js rename to db/migrations/20221108143106_create-update-timestamp-trigger.js diff --git a/db/migrations/20221108152353_create-charge-versions.js b/db/migrations/20221108152353_create-water-charge-versions.js similarity index 61% rename from db/migrations/20221108152353_create-charge-versions.js rename to db/migrations/20221108152353_create-water-charge-versions.js index 51becef007..9dc03f4444 100644 --- a/db/migrations/20221108152353_create-charge-versions.js +++ b/db/migrations/20221108152353_create-water-charge-versions.js @@ -13,18 +13,14 @@ exports.up = async function (knex) { // Data table.string('licence_ref') table.string('scheme') + table.uuid('licence_id') + table.date('start_date') + table.date('end_date') - // Automatic timestamps - table.timestamps(false, true) + // Legacy timestamps + table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) }) - - await knex.raw(` - CREATE TRIGGER update_timestamp - BEFORE UPDATE - ON water.${tableName} - FOR EACH ROW - EXECUTE PROCEDURE update_timestamp(); - `) } exports.down = function (knex) { diff --git a/db/migrations/20221111155222_create_licences.js b/db/migrations/20221111155222_create-water-licences.js similarity index 67% rename from db/migrations/20221111155222_create_licences.js rename to db/migrations/20221111155222_create-water-licences.js index 404ef3fc8b..34176f4cdd 100644 --- a/db/migrations/20221111155222_create_licences.js +++ b/db/migrations/20221111155222_create-water-licences.js @@ -13,18 +13,12 @@ exports.up = async function (knex) { // Data table.string('licence_ref') table.string('include_in_supplementary_billing') + table.uuid('region_id') - // Automatic timestamps - table.timestamps(false, true) + // Legacy timestamps + table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) }) - - await knex.raw(` - CREATE TRIGGER update_timestamp - BEFORE UPDATE - ON water.${tableName} - FOR EACH ROW - EXECUTE PROCEDURE update_timestamp(); - `) } exports.down = function (knex) { diff --git a/db/migrations/20221111184905_alter_charge_versions.js b/db/migrations/20221111184905_alter_charge_versions.js deleted file mode 100644 index 5c26177c12..0000000000 --- a/db/migrations/20221111184905_alter_charge_versions.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict' - -const tableName = 'charge_versions' - -exports.up = async function (knex) { - await knex - .schema - .withSchema('water') - .alterTable(tableName, table => { - table.uuid('licence_id') - table.date('end_date') - }) -} - -exports.down = function (knex) { - return knex - .schema - .withSchema('water') - .alterTable(tableName, table => { - table.dropColumns('licence_id', 'end_date') - }) -} diff --git a/db/migrations/20221114104700_create_regions.js b/db/migrations/20221114104700_create-water-regions.js similarity index 70% rename from db/migrations/20221114104700_create_regions.js rename to db/migrations/20221114104700_create-water-regions.js index b54c888845..d6d3608d06 100644 --- a/db/migrations/20221114104700_create_regions.js +++ b/db/migrations/20221114104700_create-water-regions.js @@ -13,9 +13,12 @@ exports.up = async function (knex) { // Data table.string('charge_region_id') table.integer('nald_region_id') + table.string('name') + table.string('display_name') - // Automatic timestamps - table.timestamps(false, true) + // Legacy timestamps + table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) }) await knex.raw(` diff --git a/db/migrations/20221114155444_alter_licences.js b/db/migrations/20221114155444_alter_licences.js deleted file mode 100644 index 85872907b7..0000000000 --- a/db/migrations/20221114155444_alter_licences.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict' - -const tableName = 'licences' - -exports.up = async function (knex) { - await knex - .schema - .withSchema('water') - .alterTable(tableName, table => { - table.uuid('region_id') - }) -} - -exports.down = function (knex) { - return knex - .schema - .withSchema('water') - .alterTable(tableName, table => { - table.dropColumns('region_id') - }) -} diff --git a/db/migrations/20221125164859_create_billing_batches.js b/db/migrations/20221125164859_create-water-billing-batches.js similarity index 77% rename from db/migrations/20221125164859_create_billing_batches.js rename to db/migrations/20221125164859_create-water-billing-batches.js index e12bed89e2..df7bbb509f 100644 --- a/db/migrations/20221125164859_create_billing_batches.js +++ b/db/migrations/20221125164859_create-water-billing-batches.js @@ -25,18 +25,12 @@ exports.up = async function (knex) { table.decimal('credit_note_value') table.string('transaction_file_reference') table.string('scheme') + table.boolean('is_summer').notNullable().defaultTo(false) - // Automatic timestamps - table.timestamps(false, true) + // Legacy timestamps + table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) }) - - await knex.raw(` - CREATE TRIGGER update_timestamp - BEFORE UPDATE - ON water.${tableName} - FOR EACH ROW - EXECUTE PROCEDURE update_timestamp(); - `) } exports.down = function (knex) { diff --git a/db/migrations/20221206084612_alter_charge_versions.js b/db/migrations/20221206084612_alter_charge_versions.js deleted file mode 100644 index 560c58c5d7..0000000000 --- a/db/migrations/20221206084612_alter_charge_versions.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict' - -const tableName = 'charge_versions' - -exports.up = async function (knex) { - await knex - .schema - .withSchema('water') - .alterTable(tableName, table => { - table.date('start_date') - }) -} - -exports.down = function (knex) { - return knex - .schema - .withSchema('water') - .alterTable(tableName, table => { - table.dropColumns('start_date') - }) -} diff --git a/db/migrations/20221209193845_create_events.js b/db/migrations/20221209193845_create-water-events.js similarity index 75% rename from db/migrations/20221209193845_create_events.js rename to db/migrations/20221209193845_create-water-events.js index a2d2964849..a81b0bbd38 100644 --- a/db/migrations/20221209193845_create_events.js +++ b/db/migrations/20221209193845_create-water-events.js @@ -17,8 +17,9 @@ exports.up = async function (knex) { table.jsonb('metadata') table.string('status') - // Automatic timestamps - table.timestamps(false, true) + // Legacy timestamps + table.timestamp('created', { precision: 0, useTz: false }).notNullable().defaultTo(knex.fn.now()) + table.timestamp('modified', { precision: 0, useTz: false }).notNullable().defaultTo(knex.fn.now()) }) await knex.raw(` diff --git a/db/migrations/20221215174926_create_charge_elements.js b/db/migrations/20221215174926_create-water-charge-elements.js similarity index 79% rename from db/migrations/20221215174926_create_charge_elements.js rename to db/migrations/20221215174926_create-water-charge-elements.js index 9f8f3c4ec1..a85557e32d 100755 --- a/db/migrations/20221215174926_create_charge_elements.js +++ b/db/migrations/20221215174926_create-water-charge-elements.js @@ -25,17 +25,10 @@ exports.up = async function (knex) { table.jsonb('adjustments') table.string('eiuc_region') - // Automatic timestamps - table.timestamps(false, true) + // Legacy timestamps + table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) }) - - await knex.raw(` - CREATE TRIGGER update_timestamp - BEFORE UPDATE - ON water.${tableName} - FOR EACH ROW - EXECUTE PROCEDURE update_timestamp(); - `) } exports.down = function (knex) { diff --git a/db/migrations/20221216112650_create_billing_charge_categories.js b/db/migrations/20221216112650_create-water-billing-charge-categories.js similarity index 86% rename from db/migrations/20221216112650_create_billing_charge_categories.js rename to db/migrations/20221216112650_create-water-billing-charge-categories.js index dd2d1ce69d..68c6aed7e8 100755 --- a/db/migrations/20221216112650_create_billing_charge_categories.js +++ b/db/migrations/20221216112650_create-water-billing-charge-categories.js @@ -22,8 +22,9 @@ exports.up = async function (knex) { table.bigInteger('min_volume') table.bigInteger('max_volume') - // Automatic timestamps - table.timestamps(false, true) + // Legacy timestamps + table.timestamp('date_created', { useTz: false }).notNullable() + table.timestamp('date_updated', { useTz: false }) }) await knex.raw(` diff --git a/db/migrations/20221216154722_create_charge_purposes.js b/db/migrations/20221216154722_create-water-charge-purposes.js similarity index 83% rename from db/migrations/20221216154722_create_charge_purposes.js rename to db/migrations/20221216154722_create-water-charge-purposes.js index 1373af45bb..9f447c395c 100755 --- a/db/migrations/20221216154722_create_charge_purposes.js +++ b/db/migrations/20221216154722_create-water-charge-purposes.js @@ -28,17 +28,10 @@ exports.up = async function (knex) { table.uuid('purpose_use_id') table.boolean('is_section_127_agreement_enabled') - // Automatic timestamps - table.timestamps(false, true) + // Legacy timestamps + table.timestamp('date_created', { useTz: false }).notNullable().defaultTo(knex.fn.now()) + table.timestamp('date_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now()) }) - - await knex.raw(` - CREATE TRIGGER update_timestamp - BEFORE UPDATE - ON water.${tableName} - FOR EACH ROW - EXECUTE PROCEDURE update_timestamp(); - `) } exports.down = function (knex) { diff --git a/db/migrations/20230102173756_alter_regions.js b/db/migrations/20230102173756_alter_regions.js deleted file mode 100755 index 2efa779764..0000000000 --- a/db/migrations/20230102173756_alter_regions.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict' - -const tableName = 'regions' - -exports.up = async function (knex) { - await knex - .schema - .withSchema('water') - .alterTable(tableName, table => { - table.string('name') - table.string('display_name') - }) -} - -exports.down = function (knex) { - return knex - .schema - .withSchema('water') - .alterTable(tableName, table => { - table.dropColumns('name', 'display_name') - }) -} diff --git a/db/migrations/20230102174930_alter_billing_batches.js b/db/migrations/20230102174930_alter_billing_batches.js deleted file mode 100755 index 28e5739cf9..0000000000 --- a/db/migrations/20230102174930_alter_billing_batches.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict' - -const tableName = 'billing_batches' - -exports.up = async function (knex) { - await knex - .schema - .withSchema('water') - .alterTable(tableName, table => { - table.boolean('is_summer').notNullable().defaultTo(false) - }) -} - -exports.down = function (knex) { - return knex - .schema - .withSchema('water') - .alterTable(tableName, table => { - table.dropColumns('is_summer') - }) -} diff --git a/test/presenters/supplementary-billing/create-billing-batch-event.presenter.test.js b/test/presenters/supplementary-billing/create-billing-batch-event.presenter.test.js index 224938cf2b..7b7a0416e4 100644 --- a/test/presenters/supplementary-billing/create-billing-batch-event.presenter.test.js +++ b/test/presenters/supplementary-billing/create-billing-batch-event.presenter.test.js @@ -44,8 +44,8 @@ describe('Create Billing Batch Event presenter', () => { expect(result.batch.isSummer).to.equal(billingBatch.isSummer) expect(result.batch.netTotal).to.equal(billingBatch.netTotal) - expect(result.batch.dateCreated).to.equal(billingBatch.createdAt) - expect(result.batch.dateUpdated).to.equal(billingBatch.updatedAt) + expect(result.batch.dateCreated).to.equal(billingBatch.dateCreated) + expect(result.batch.dateUpdated).to.equal(billingBatch.dateUpdated) expect(result.batch.invoiceCount).to.equal(billingBatch.invoiceCount) expect(result.batch.invoiceValue).to.equal(billingBatch.invoiceValue) expect(result.batch.creditNoteCount).to.equal(billingBatch.creditNoteCount) diff --git a/test/support/helpers/water/billing-charge-category.helper.js b/test/support/helpers/water/billing-charge-category.helper.js index 5e3089e2c6..dd8067b899 100644 --- a/test/support/helpers/water/billing-charge-category.helper.js +++ b/test/support/helpers/water/billing-charge-category.helper.js @@ -20,7 +20,8 @@ const BillingChargeCategoryModel = require('../../../../app/models/water/billing * - `modelTier` - tier 1 * - `isRestrictedSource` - true * - `minVolume` - 0 - * - `maxVolume` - 5000 + * - `maxVolume` - 5000, + * - `dateCreated` - Date.now() * * @param {Object} [data] Any data you want to use instead of the defaults used here or in the database * @@ -53,7 +54,14 @@ function defaults (data = {}) { modelTier: 'tier 1', isRestrictedSource: true, minVolume: 0, - maxVolume: 5000 + maxVolume: 5000, + // INFO: The billing_charge_categories table does not have a default for the date_created column. But it is set as + // 'not nullable'! So, we need to ensure we set it when creating a new record, something we'll never actually need + // to do because it's a static table. Also, we can't use Date.now() because Javascript returns the time since the + // epoch in milliseconds, whereas a PostgreSQL timestamp field can only hold the seconds since the epoch. Pass it + // an ISO string though ('2023-01-05T08:37:05.575Z') and PostgreSQL can do the conversion + // https://stackoverflow.com/a/61912776/6117745 + dateCreated: new Date().toISOString() } return {