Skip to content

Commit

Permalink
Rewrite the migrations to match legacy tables (#77)
Browse files Browse the repository at this point in the history
DEFRA/water-abstraction-team#69

> **WARNING** - This will break your test environment. Run `npm run rollback:db:test` before pulling down to prevent this.

> Note - Whilst the repo is new, not yet in `production` and we only have a few migrations purely for test purposes we have opted for a rewrite rather than a 'fix-forward' approach.

This change rewrites the existing migrations. We'll be

- renaming the files to include which schemas they will be added to
- adding timestamp fields that match the original tables
- consolidating any changes we've made up to this point

This is to ensure our test tables match what we see in the legacy code so we know we're handling the issues we find there and make it clearer what schema a table belongs to.
  • Loading branch information
Cruikshanks authored Jan 9, 2023
1 parent 02f33a9 commit 88cb9dc
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 0 additions & 22 deletions db/migrations/20221111184905_alter_charge_versions.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down
21 changes: 0 additions & 21 deletions db/migrations/20221114155444_alter_licences.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 0 additions & 21 deletions db/migrations/20221206084612_alter_charge_versions.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 0 additions & 22 deletions db/migrations/20230102173756_alter_regions.js

This file was deleted.

21 changes: 0 additions & 21 deletions db/migrations/20230102174930_alter_billing_batches.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions test/support/helpers/water/billing-charge-category.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 88cb9dc

Please sign in to comment.