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

Rewrite the migrations to match legacy tables #77

Merged
merged 5 commits into from
Jan 9, 2023
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
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