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 & tidy test DB legacy migrations #725

Merged
merged 45 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
69b493c
Fix test DB legacy migrations
Cruikshanks Feb 9, 2024
1159ba0
Update water.charge_versions
Cruikshanks Feb 19, 2024
9f91160
Update water.licences
Cruikshanks Feb 19, 2024
6a45303
Update water.regions
Cruikshanks Feb 19, 2024
756dfb9
Fix broken test after water.licences update
Cruikshanks Feb 19, 2024
a6501f9
Update water.billing_batches
Cruikshanks Feb 19, 2024
5f811b4
Update water.events
Cruikshanks Feb 19, 2024
e8bb118
Update water.charge_elements
Cruikshanks Feb 19, 2024
072ef6d
Update water.billing_charge_categories
Cruikshanks Feb 19, 2024
9626806
Update water.charge_purposes
Cruikshanks Feb 19, 2024
68e5fc3
Update water.billing_invoices
Cruikshanks Feb 19, 2024
07edeb7
Fix broken test after water.billing_invoices update
Cruikshanks Feb 19, 2024
c3b34a4
Update water.billing_invoice_licences
Cruikshanks Feb 19, 2024
f3007a9
Update crm_v2.invoice_accounts
Cruikshanks Feb 19, 2024
0be0828
Fix broken tests after water.invoice_account update
Cruikshanks Feb 19, 2024
c7ef04a
Update water.billing_transactions
Cruikshanks Feb 19, 2024
f4102a5
Fix broken tests after water.billing_trans update
Cruikshanks Feb 19, 2024
00fda7a
Update water.charge_version_workflows
Cruikshanks Feb 19, 2024
620943f
Update returns.returns
Cruikshanks Feb 19, 2024
99e706d
Replace single constraints with unique()
Cruikshanks Feb 19, 2024
e3a1e73
Update crm_v2.addresses
Cruikshanks Feb 19, 2024
f78e833
Update crm_v2.invoice_account_addresses
Cruikshanks Feb 19, 2024
b1466f3
Update crm_v2.contacts
Cruikshanks Feb 19, 2024
0bec1df
Update crm_v2.companies
Cruikshanks Feb 19, 2024
cdc5822
Update idm.users
Cruikshanks Feb 19, 2024
c53b387
Update idm.group_roles
Cruikshanks Feb 19, 2024
93aa475
Update idm.groups
Cruikshanks Feb 19, 2024
1a6fd20
Update idm.roles
Cruikshanks Feb 19, 2024
cf44039
Update returns.lines
Cruikshanks Feb 19, 2024
79980b4
Update returns.versions
Cruikshanks Feb 19, 2024
adfde2f
Update water.billing_volumes
Cruikshanks Feb 19, 2024
2a37977
Update water.licence_versions
Cruikshanks Feb 19, 2024
3716cf6
Update crm_v2.documents
Cruikshanks Feb 19, 2024
086698f
Update crm_v2.documents
Cruikshanks Feb 19, 2024
22ea9be
Update crm.document_header
Cruikshanks Feb 19, 2024
4d3435c
Update crm.entity
Cruikshanks Feb 19, 2024
db4ec70
Update crm.entity_roles
Cruikshanks Feb 19, 2024
3c0c5db
Update water.licence_version_purposes
Cruikshanks Feb 19, 2024
ffe5115
Start tidy up - update filenames
Cruikshanks Feb 19, 2024
c09f0c8
Move all schema creation to single migration
Cruikshanks Feb 19, 2024
85eaeb5
Rename and group migrations
Cruikshanks Feb 19, 2024
2a45ad9
Add permit schema creation
Cruikshanks Feb 19, 2024
a30699d
Correct use of text columns
Cruikshanks Feb 19, 2024
bb64567
Merge branch 'main' into fix-legacy-migrations
Cruikshanks Feb 19, 2024
e97a338
Merge branch 'main' into fix-legacy-migrations
Cruikshanks Feb 20, 2024
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
23 changes: 23 additions & 0 deletions db/migrations/legacy/20221108001003_create-schemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

exports.up = function (knex) {
return knex.raw(`
CREATE SCHEMA IF NOT EXISTS "crm";
CREATE SCHEMA IF NOT EXISTS "crm_v2";
CREATE SCHEMA IF NOT EXISTS "idm";
CREATE SCHEMA IF NOT EXISTS "permit";
CREATE SCHEMA IF NOT EXISTS "returns";
CREATE SCHEMA IF NOT EXISTS "water";
`)
}

exports.down = function (knex) {
return knex.raw(`
DROP SCHEMA IF EXISTS "water";
DROP SCHEMA IF EXISTS "returns";
DROP SCHEMA IF EXISTS "permit";
DROP SCHEMA IF EXISTS "idm";
DROP SCHEMA IF EXISTS "crm_v2";
DROP SCHEMA IF EXISTS "crm";
`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ exports.up = function (knex) {
table.timestamp('date_created').notNullable().defaultTo(knex.fn.now())
table.timestamp('date_updated').notNullable().defaultTo(knex.fn.now())

// Constraints
table.unique(
['system_id', 'system_internal_id', 'regime_entity_id'],
{ useConstraint: true, indexName: 'external_key' }
)
table.unique([
'document_id', 'regime_entity_id', 'system_id', 'system_internal_id',
'system_external_id', 'company_entity_id', 'verification_id'
])
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@ exports.up = function (knex) {
.schema
.withSchema('crm')
.createTable(tableName, (table) => {
// Primary Key
table.string('entity_id').primary()

// Data
table.string('entity_id').notNullable()
table.string('entity_nm').notNullable()
table.string('entity_type').notNullable()
table.jsonb('entity_definition')
table.string('source')

// Legacy timestamps
// NOTE: They are not automatically set and there are large numbers of records where these fields are null!
// NOTE: They are not automatically set
table.timestamp('created_at')
table.timestamp('updated_at')

// Constraints
table.unique(['entity_id', 'entity_nm', 'entity_type'], { useConstraint: true })
// Primary Key
// NOTE: It is not entity_id. The primary key is made up of all these columns
table.primary(['entity_id', 'entity_nm', 'entity_type'])
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports.up = function (knex) {
table.string('created_by')

// Legacy timestamps
// NOTE: They are not automatically set and there are large numbers of records where these fields are null!
// NOTE: They are not automatically set
table.timestamp('created_at').defaultTo(knex.fn.now())
})
// If it was a simple check constraint we could have used https://knexjs.org/guide/schema-builder.html#checks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ exports.up = function (knex) {
table.string('county')
table.string('postcode')
table.string('country')
table.string('external_id')
table.string('external_id').unique()
table.boolean('is_test').notNullable().defaultTo(false)
table.string('data_source')
table.integer('uprn')
table.string('data_source').notNullable()
table.integer('uprn').unique()
table.string('last_hash')
table.string('current_hash')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports.up = function (knex) {
table.string('first_name')
table.string('middle_initials')
table.string('last_name')
table.string('external_id')
table.string('external_id').unique()
table.string('initials')
table.boolean('is_test').notNullable().defaultTo(false)
table.string('data_source').notNullable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ exports.up = function (knex) {
// Data
table.string('name').notNullable()
table.string('type')
table.string('company_number')
table.string('external_id')
table.string('company_number').unique()
table.string('external_id').unique()
table.boolean('is_test').notNullable().defaultTo(false)
table.string('organisation_type')
table.string('last_hash')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ exports.up = function (knex) {
table.timestamp('date_created').notNullable().defaultTo(knex.fn.now())
table.timestamp('date_updated').notNullable().defaultTo(knex.fn.now())

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

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

// Data
table.uuid('company_id')
table.string('invoice_account_number')
table.uuid('company_id').notNullable()
table.string('invoice_account_number').notNullable().unique()
table.date('start_date')
table.date('end_date')
table.boolean('is_test').notNullable().defaultTo(false)
table.string('last_transaction_file_reference')
table.timestamp('date_last_transaction_file_reference_updated', { useTz: false }).notNullable().defaultTo(knex.fn.now())

// 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', 'invoice_account_number'], { useConstraint: true })
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ exports.up = function (knex) {
// Legacy timestamps
table.timestamp('date_created').notNullable().defaultTo(knex.fn.now())
table.timestamp('date_updated').notNullable().defaultTo(knex.fn.now())

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ exports.up = function (knex) {
// 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(['application', 'group'], { useConstraint: true })
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ exports.up = function (knex) {
// 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(['application', 'role'], { useConstraint: true })
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ exports.up = function (knex) {
// 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(['user_name', 'application'], { useConstraint: true })
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ exports.up = function (knex) {
// 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(['group_id', 'role_id'], { useConstraint: true })
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ exports.up = function (knex) {
table.string('return_id').primary()

// Data
table.string('regime').notNullable()
table.string('licence_type').notNullable()
table.string('regime').notNullable().defaultTo('water')
table.string('licence_type').notNullable().defaultTo('abstraction')
table.string('licence_ref').notNullable()
table.date('start_date').notNullable()
table.date('end_date').notNullable()
Expand All @@ -29,8 +29,12 @@ exports.up = function (knex) {
table.uuid('return_cycle_id')

// Legacy timestamps
table.timestamp('created_at', { useTz: false }).notNullable().defaultTo(knex.fn.now())
table.timestamp('updated_at', { useTz: false }).notNullable().defaultTo(knex.fn.now())
// NOTE: They are not automatically set
table.timestamp('created_at').notNullable()
table.timestamp('updated_at')

// Constraints
table.unique(['regime', 'licence_type', 'licence_ref', 'start_date', 'end_date', 'return_requirement'], { useConstraint: true })
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ exports.up = function (knex) {
table.boolean('nil_return').notNullable()
table.boolean('current')

table.unique(['return_id', 'version_number'], { useConstraint: true })

// Legacy timestamps
// NOTE: They are not automatically set
table.timestamp('created_at').notNullable()
table.timestamp('updated_at')

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@ exports.up = function (knex) {

// Data
table.string('version_id').notNullable()
table.string('substance').notNullable()
table.string('substance').notNullable().defaultTo('water')
table.decimal('quantity').notNullable()
table.string('unit').notNullable()
table.string('unit').notNullable().defaultTo('m³')
table.date('start_date').notNullable()
table.date('end_date').notNullable()
table.string('time_period').notNullable()
table.jsonb('metadata')
table.string('reading_type')
table.string('user_unit')

table.unique(['version_id', 'substance', 'start_date', 'end_date'], { useConstraint: true })

// Legacy timestamps
// NOTE: They are not automatically set
table.timestamp('created_at').notNullable()
table.timestamp('updated_at')

// Constraints
table.unique(['version_id', 'substance', 'start_date', 'end_date'], { useConstraint: true })
})
}

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

// Data
table.string('charge_region_id')
table.integer('nald_region_id')
table.string('name')
table.string('display_name')
table.string('charge_region_id').notNullable()
table.integer('nald_region_id').notNullable()
table.string('name').notNullable()
table.text('display_name').notNullable()
table.boolean('is_test').notNullable().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())
})
.then(() => {
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 @@ -11,30 +11,21 @@ exports.up = function (knex) {
table.uuid('billing_charge_category_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.string('reference')
table.integer('subsistence_charge')
table.string('description')
table.string('short_description')
table.boolean('is_tidal')
table.string('reference').notNullable().unique()
table.integer('subsistence_charge').notNullable()
table.string('description').notNullable()
table.string('short_description').notNullable()
table.boolean('is_tidal').defaultTo(false)
table.string('loss_factor')
table.string('model_tier')
table.boolean('is_restricted_source')
table.bigInteger('min_volume')
table.bigInteger('max_volume')
table.boolean('is_restricted_source').defaultTo(false)
table.bigInteger('min_volume').notNullable().defaultTo(0)
table.bigInteger('max_volume').notNullable().defaultTo(0)

// Legacy timestamps
table.timestamp('date_created', { useTz: false }).notNullable()
table.timestamp('date_updated', { useTz: false })
})
.then(() => {
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 @@ -12,11 +12,12 @@ exports.up = function (knex) {

// Data
table.string('reference_code')
table.string('type')
table.string('type').notNullable()
table.string('subtype')
table.string('issuer')
table.jsonb('licences')
table.jsonb('entities')
table.string('comment')
table.jsonb('metadata')
table.string('status')

Expand Down
38 changes: 38 additions & 0 deletions db/migrations/legacy/20221108007006-water-licences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

const tableName = 'licences'

exports.up = function (knex) {
return knex
.schema
.withSchema('water')
.createTable(tableName, (table) => {
// Primary Key
table.uuid('licence_id').primary().defaultTo(knex.raw('gen_random_uuid()'))

// Data
table.uuid('region_id').notNullable()
table.string('licence_ref').notNullable().unique()
table.boolean('is_water_undertaker').notNullable()
table.jsonb('regions').notNullable()
table.date('start_date').notNullable()
table.date('expired_date')
table.date('lapsed_date')
table.date('revoked_date')
table.boolean('suspend_from_billing').notNullable().defaultTo(false)
table.boolean('is_test').notNullable().defaultTo(false)
table.string('include_in_supplementary_billing').notNullable().defaultTo('no')
table.boolean('include_in_sroc_supplementary_billing').notNullable().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())
})
}

exports.down = function (knex) {
return knex
.schema
.withSchema('water')
.dropTableIfExists(tableName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ exports.up = function (knex) {
table.string('status').notNullable()
table.date('start_date').notNullable()
table.date('end_date')
table.string('external_id').notNullable()
table.string('external_id').notNullable().unique()
table.boolean('is_test').notNullable().defaultTo(false)

// Legacy timestamps
// NOTE: They are not automatically set
table.timestamp('date_created', { useTz: false }).notNullable()
table.timestamp('date_updated', { useTz: false }).notNullable()
})
Expand Down
Loading
Loading