Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
cacd29f
Added basic test to see if a scottish lodgment could go to scotland s…
Christine-horrocks Sep 15, 2025
3fae7e6
EPBR-9018 Update the migrate lodgement process to add Scottish RdSAP …
Christine-horrocks Oct 7, 2025
f4356d7
EPBR-9159 Expand tests to include SAP
Christine-horrocks Oct 8, 2025
9e80857
EPBR-9037 Prevent scottish assessments going to data warehouse
Christine-horrocks Oct 20, 2025
04524df
EPBR-9184 Update is_scottish gateway params to be named params
Christine-horrocks Oct 21, 2025
e797188
EPBR-9159 Update assessments_country_id_gateway_spec.rb to avoid conf…
Christine-horrocks Oct 22, 2025
156746f
EPBR-9159 Update related_assessments_gateway_spec.rb to use unique ad…
Christine-horrocks Oct 23, 2025
590461d
EPBR-9159 Update assessments_country_id_gateway_spec.rb test labels f…
Christine-horrocks Oct 23, 2025
bae195a
EPBR-9159 Update search_address_gateway_spec.rb to remove redundant t…
Christine-horrocks Oct 23, 2025
771c801
EPBR-9159 Add tests to cover the insert or update method
Christine-horrocks Oct 30, 2025
e8447a3
EPBR-9159 Add tests to cover the sent to db method in the assessment_…
Christine-horrocks Oct 30, 2025
803b4bd
EPBR-9159 Update insert_country fall back param to named param in lod…
Christine-horrocks Nov 3, 2025
942cb5b
EPBR-9159 Update lodge_spec.rb for clarity
Christine-horrocks Nov 3, 2025
9c1314b
EPBR-9159 Update scotland_schema_migration_spec.rb tables test to exc…
Christine-horrocks Nov 3, 2025
a4b760c
EPBR-9159 Run make format
Christine-horrocks Nov 3, 2025
39ed832
EPBR-9159 Add scottish assessment migration tests to lodge_assessment…
Christine-horrocks Nov 3, 2025
29d4ef2
EPBR-9159 Fix the typo in the name of foreign key migrations
Christine-horrocks Nov 3, 2025
a4e4bab
EPBR-9159 Add scotland schema helper
Christine-horrocks Nov 3, 2025
347cccf
EPBR-9159 Update Ruby
Christine-horrocks Nov 4, 2025
9dc3687
EPBR-9159 Clean up migrations
Christine-horrocks Nov 4, 2025
4541c67
EPBR-9159 Clean up migrations which corrected foreign key issues and …
Christine-horrocks Nov 4, 2025
8e5898d
EPBR-9159 Remove scotland schema from schema.rb and make format
Christine-horrocks Nov 4, 2025
a891333
EPBR-9159 Correct scotland_helper.rb to constant
Christine-horrocks Nov 10, 2025
3fb2350
Merge branch 'master' into scotland_end_to_end_test
Christine-horrocks Nov 10, 2025
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
@@ -0,0 +1,7 @@
class RemoveIncorrectForeignKeysFromScotlandTable < ActiveRecord::Migration[8.0]
def change
remove_foreign_key "scotland.assessments_xml", "assessments"
remove_foreign_key "scotland.green_deal_assessments", "assessments"
remove_foreign_key "scotland.green_deal_assessments", "green_deal_plans"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AddCorrectForeignKeysToScotlandSchemaTables < ActiveRecord::Migration[8.0]
def change
add_foreign_key "scotland.assessments_xml",
"scotland.assessments",
column: :assessment_id,
primary_key: :assessment_id,
name: "fk_scotland_assessment_xml_scotland_assessments"
add_foreign_key "scotland.green_deal_assessments",
"scotland.assessments",
column: :assessment_id,
primary_key: :assessment_id,
name: "fk_scotland_green_deal_assessments_scotland_assessments"
end
end
22 changes: 22 additions & 0 deletions db/migrate/20250922145129_add_green_deal_plans_scotland_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class AddGreenDealPlansScotlandTable < ActiveRecord::Migration[8.0]
def change
create_table "scotland.green_deal_plans", primary_key: :green_deal_plan_id, id: false do |t|
t.string :green_deal_plan_id
t.datetime :start_date
t.datetime :end_date
t.string :provider_name
t.string :provider_telephone
t.string :provider_email
t.decimal :interest_rate
t.boolean :fixed_interest_rate
t.decimal :charge_uplift_amount
t.datetime :charge_uplift_date
t.boolean :cca_regulated
t.boolean :structure_changed
t.boolean :measures_removed
t.jsonb :measures, null: false, default: "[]"
t.jsonb :charges, null: false, default: "[]"
t.jsonb :savings, null: false, default: "[]"
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddGreenDealPlansForeignKeyToScotlandGreenDealAssessments < ActiveRecord::Migration[8.0]
def change
add_index "scotland.green_deal_plans", :green_deal_plan_id, unique: true

add_foreign_key "scotland.green_deal_assessments",
"scotland.green_deal_plans",
column: :green_deal_plan_id, primary_key: :green_deal_plan_id
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2025_05_19_124713) do
ActiveRecord::Schema[8.0].define(version: 2025_09_22_151938) do

# These are extensions that must be enabled in order to support this database
enable_extension "fuzzystrmatch"
Expand Down
2 changes: 1 addition & 1 deletion lib/events/listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def attach_listeners
def attach_assessment_lodged
@event_broadcaster.on :assessment_lodged do |**data|
if notify_data_warehouse_enabled?
NotifyFactory.new_assessment_to_data_warehouse_use_case.execute(assessment_id: data[:assessment_id])
NotifyFactory.new_assessment_to_data_warehouse_use_case.execute(assessment_id: data[:assessment_id], is_scottish: data[:is_scottish])
end
NotifyFactory.lodgement_to_audit_log(entity_id: data[:assessment_id])
end
Expand Down
22 changes: 16 additions & 6 deletions lib/gateway/assessments_address_id_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ class AssessmentsAddressIdGateway
class AssessmentsAddressId < ActiveRecord::Base
end

def fetch(assessment_id)
AssessmentsAddressId.find(assessment_id).as_json.symbolize_keys
class AssessmentsAddressIdScotland < ActiveRecord::Base
self.table_name = "scotland.assessments_address_id"
end

def send_to_db(record)
existing_assessment_address_id =
AssessmentsAddressId.find_by assessment_id: record[:assessment_id]
AssessmentsAddressId.create(record) if existing_assessment_address_id.nil?
def fetch(assessment_id, is_scottish: false)
is_scottish ? AssessmentsAddressIdScotland.find(assessment_id).as_json.symbolize_keys : AssessmentsAddressId.find(assessment_id).as_json.symbolize_keys
end

def send_to_db(record, is_scottish: false)
if is_scottish
existing_assessment_address_id =
AssessmentsAddressIdScotland.find_by assessment_id: record[:assessment_id]
AssessmentsAddressIdScotland.create(record) if existing_assessment_address_id.nil?
else
existing_assessment_address_id =
AssessmentsAddressId.find_by assessment_id: record[:assessment_id]
AssessmentsAddressId.create(record) if existing_assessment_address_id.nil?
end
end

def update_assessments_address_id_mapping(
Expand Down
8 changes: 5 additions & 3 deletions lib/gateway/assessments_country_id_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ class AssessmentsCountryIdGateway
class AssessmentsCountryId < ActiveRecord::Base
end

def insert(assessment_id:, country_id:, upsert: false)
def insert(assessment_id:, country_id:, upsert: false, is_scottish: false)
schema = Helper::ScotlandHelper.select_schema(is_scottish)

sql = if upsert
<<-SQL
INSERT INTO assessments_country_ids (assessment_id,country_id) VALUES ($1, $2)
INSERT INTO #{schema}assessments_country_ids (assessment_id,country_id) VALUES ($1, $2)
ON CONFLICT(assessment_id)
DO UPDATE SET country_id=$2
SQL
else
<<-SQL
INSERT INTO assessments_country_ids (assessment_id,country_id) VALUES ($1, $2)
INSERT INTO #{schema}assessments_country_ids (assessment_id,country_id) VALUES ($1, $2)
SQL
end

Expand Down
137 changes: 77 additions & 60 deletions lib/gateway/assessments_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class AssessmentsGateway

class Assessment < ActiveRecord::Base; end

class AssessmentScotland < ActiveRecord::Base
self.table_name = "scotland.assessments"
end

class InvalidAssessmentType < StandardError; end

class AssessmentAlreadyExists < StandardError; end
Expand All @@ -21,9 +25,9 @@ class AssessmentAlreadyExists < StandardError; end
AC-REPORT
].freeze

def insert_or_update(assessment)
def insert_or_update(assessment, is_scottish: false)
check_valid_energy_ratings assessment
send_update_to_db assessment
send_update_to_db(assessment, is_scottish)
end

def insert(assessment)
Expand Down Expand Up @@ -218,83 +222,96 @@ def send_insert_to_db(assessment)
end

unless assessment.get(:related_rrn).nil?
add_linked_assessment assessment
add_linked_assessment(assessment, "public.")
end
end
end

def send_update_to_db(assessment)
def send_update_to_db(assessment, is_scottish)
ActiveRecord::Base.transaction do
existing_assessment =
Assessment.exists? assessment_id: assessment.get(:assessment_id)
existing_assessment = if is_scottish
AssessmentScotland.exists?(assessment_id: assessment.get(:assessment_id))
else
Assessment.exists?(assessment_id: assessment.get(:assessment_id))
end

if existing_assessment
delete_xml = <<-SQL
DELETE FROM assessments_xml WHERE assessment_id = $1
SQL
schema = Helper::ScotlandHelper.select_schema(is_scottish)
remove_and_relodge_assessment(assessment, schema)
else
is_scottish ? AssessmentScotland.create(assessment.to_record) : Assessment.create(assessment.to_record)
end

delete_address_id = <<-SQL
DELETE FROM assessments_address_id WHERE assessment_id = $1
SQL
unless assessment.get(:related_rrn).nil?
add_linked_assessment(assessment, schema)
end
end
end

green_deal_plan_id = <<-SQL
SELECT green_deal_plan_id FROM green_deal_assessments WHERE assessment_id = $1
SQL
def remove_and_relodge_assessment(assessment, schema)
delete_xml = <<-SQL
DELETE FROM #{schema}assessments_xml WHERE assessment_id = $1
SQL

delete_green_deal_assessment = <<-SQL
DELETE FROM green_deal_assessments WHERE assessment_id = $1
SQL
delete_address_id = <<-SQL
DELETE FROM #{schema}assessments_address_id WHERE assessment_id = $1
SQL

delete_assessment = <<-SQL
DELETE FROM assessments WHERE assessment_id = $1
SQL
green_deal_plan_id = <<-SQL
SELECT green_deal_plan_id FROM #{schema}green_deal_assessments WHERE assessment_id = $1
SQL

delete_linked_assessment = <<-SQL
DELETE FROM linked_assessments WHERE assessment_id = $1
SQL
delete_green_deal_assessment = <<-SQL
DELETE FROM #{schema}green_deal_assessments WHERE assessment_id = $1
SQL

binds = [
ActiveRecord::Relation::QueryAttribute.new(
"id",
assessment.get(:assessment_id),
ActiveRecord::Type::String.new,
),
]
delete_assessment = <<-SQL
DELETE FROM #{schema}assessments WHERE assessment_id = $1
SQL

ActiveRecord::Base.connection.exec_query delete_xml, "SQL", binds
delete_linked_assessment = <<-SQL
DELETE FROM #{schema}linked_assessments WHERE assessment_id = $1
SQL

green_deal_plan_ids =
ActiveRecord::Base.connection.exec_query green_deal_plan_id,
"SQL",
binds
binds = [
ActiveRecord::Relation::QueryAttribute.new(
"id",
assessment.get(:assessment_id),
ActiveRecord::Type::String.new,
),
]

ActiveRecord::Base.connection.exec_query delete_green_deal_assessment,
"SQL",
binds
ActiveRecord::Base.connection.exec_query delete_xml, "SQL", binds

ActiveRecord::Base.connection.exec_query delete_assessment,
"SQL",
binds
green_deal_plan_ids =
ActiveRecord::Base.connection.exec_query green_deal_plan_id,
"SQL",
binds

ActiveRecord::Base.connection.exec_query delete_address_id,
"SQL",
binds
ActiveRecord::Base.connection.exec_query delete_green_deal_assessment,
"SQL",
binds

ActiveRecord::Base.connection.exec_query delete_linked_assessment,
"SQL",
binds
ActiveRecord::Base.connection.exec_query delete_assessment,
"SQL",
binds

Assessment.create assessment.to_record
ActiveRecord::Base.connection.exec_query delete_address_id,
"SQL",
binds

reattach_green_deal_plans(green_deal_plan_ids, binds)
else
Assessment.create assessment.to_record
end
ActiveRecord::Base.connection.exec_query delete_linked_assessment,
"SQL",
binds

unless assessment.get(:related_rrn).nil?
add_linked_assessment assessment
end
if schema == "scotland."
AssessmentScotland.create assessment.to_record
else
Assessment.create assessment.to_record
end

# TODO: Need to test this
reattach_green_deal_plans(green_deal_plan_ids, binds, schema)
end

def check_valid_energy_ratings(assessment)
Expand All @@ -319,9 +336,9 @@ def check_valid_energy_ratings(assessment)
end
end

def reattach_green_deal_plans(green_deal_plan_ids, binds)
def reattach_green_deal_plans(green_deal_plan_ids, binds, schema)
add_green_deal_plan = <<-SQL
INSERT INTO green_deal_assessments (assessment_id, green_deal_plan_id)
INSERT INTO #{schema}green_deal_assessments (assessment_id, green_deal_plan_id)
VALUES ($1, $2)
SQL

Expand All @@ -340,9 +357,9 @@ def reattach_green_deal_plans(green_deal_plan_ids, binds)
end
end

def add_linked_assessment(assessment)
def add_linked_assessment(assessment, schema)
add_linked_assessment = <<-SQL
INSERT INTO linked_assessments (assessment_id, linked_assessment_id)
INSERT INTO #{schema}linked_assessments (assessment_id, linked_assessment_id)
VALUES ($1, $2)
SQL

Expand Down
8 changes: 6 additions & 2 deletions lib/gateway/assessments_xml_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ class AssessmentsXmlGateway
class AssessmentsXml < ActiveRecord::Base
end

def send_to_db(record)
AssessmentsXml.create(record)
class AssessmentsXmlScotland < ActiveRecord::Base
self.table_name = "scotland.assessments_xml"
end

def send_to_db(record, is_scottish)
is_scottish ? AssessmentsXmlScotland.create(record) : AssessmentsXml.create(record)
end

def fetch(assessment_id)
Expand Down
21 changes: 15 additions & 6 deletions lib/gateway/green_deal_plans_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ class GreenDealPlansGateway
class GreenDealPlan < ActiveRecord::Base
end

class GreenDealPlanScotland < ActiveRecord::Base
self.table_name = "scotland.green_deal_plan"
end

def exists?(green_deal_plan_id)
sql = <<-SQL
SELECT EXISTS (
Expand Down Expand Up @@ -33,9 +37,10 @@ def add(green_deal_plan, assessment_id)
link_green_deal_to_assessment green_deal_plan.green_deal_plan_id, assessment_id
end

def link_green_deal_to_assessment(green_deal_plan_id, assessment_id)
def link_green_deal_to_assessment(green_deal_plan_id, assessment_id, is_scottish: false)
schema = Helper::ScotlandHelper.select_schema(is_scottish)
sql = <<-SQL
INSERT INTO green_deal_assessments (green_deal_plan_id, assessment_id)
INSERT INTO #{schema}green_deal_assessments (green_deal_plan_id, assessment_id)
VALUES ($1, $2)
SQL

Expand Down Expand Up @@ -72,7 +77,8 @@ def update_end_date_and_charges(green_deal_plan_id:, end_date:, charges:)
GreenDealPlan.update(green_deal_plan_id, { end_date: end_date, charges: charges })
end

def fetch(assessment_id)
def fetch(assessment_id, is_scottish: false)
schema = Helper::ScotlandHelper.select_schema(is_scottish)
sql = <<-SQL
SELECT
b.green_deal_plan_id, b.start_date, b.end_date,
Expand All @@ -81,14 +87,17 @@ def fetch(assessment_id)
b.charge_uplift_date, b.cca_regulated, b.structure_changed,
b.measures_removed, b.charges, b.measures, b.savings
FROM
green_deal_assessments a
#{schema}green_deal_assessments a
INNER JOIN
green_deal_plans b ON a.green_deal_plan_id = b.green_deal_plan_id
#{schema}green_deal_plans b ON a.green_deal_plan_id = b.green_deal_plan_id
WHERE
assessment_id = $1
ORDER BY b.green_deal_plan_id
SQL
response = GreenDealPlan.connection.exec_query(

table = is_scottish ? GreenDealPlanScotland : GreenDealPlan

response = table.connection.exec_query(
sql,
"SQL",
[
Expand Down
Loading