Skip to content

Commit a5beca3

Browse files
committed
feature: Add large_user_postcode flag and index
- relevant index used by postcode collection worker will be checking for active and small postcodes (ie retired and large_user_postcode = false), and ordered by updated_at. - Default to large_user_postcode: false, set to true in a rake task from the existing ONSPD data. Only ONSPD records will ever be large_user_postcode: true, so we only need to check them, but they must be set before the code to use them is added, and since this will timeout a migration, we have to do it as a 2-phase deploy (1: migrate, update data, 2: update code.)
1 parent 2f31732 commit a5beca3

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class UpdatePostcodesAddLupFlag < ActiveRecord::Migration[8.0]
2+
def up
3+
add_column :postcodes, :large_user_postcode, :boolean, default: false, null: false
4+
5+
add_index :postcodes, %i[retired large_user_postcode updated_at]
6+
end
7+
8+
def down
9+
remove_index :postcodes, %i[retired large_user_postcode updated_at]
10+
11+
remove_column :postcodes, :large_user_postcode, :boolean, default: false, null: false
12+
end
13+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/tasks/update_lup_flag.rake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
desc "Update all "
2+
task update_lup_flag: :environment do
3+
Postcode.onspd.find_in_batches(batch_size: 50) do |group|
4+
group.select { |r| r.results.first["ONS"]["TYPE"] == "L" }.each do |r|
5+
r.update(large_user_postcode: true)
6+
end
7+
end
8+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require "spec_helper"
2+
require "rake"
3+
4+
RSpec.describe "update_lup_flag task" do
5+
let(:task) { Rake::Task["update_lup_flag"] }
6+
7+
context "when the update_lup_flag task is invoked" do
8+
before do
9+
Postcode.create(postcode: "E12AA", source: "onspd", updated_at: 3.days.ago, results: [{ "ONS" => { "TYPE" => "L" } }])
10+
Postcode.create(postcode: "E13AA", source: "onspd", updated_at: 3.days.ago, results: [{ "ONS" => { "TYPE" => "S" } }])
11+
end
12+
13+
it "sets the large_user_postcode flag true for LUP postcodes, but not for small ones" do
14+
task.reenable
15+
task.invoke
16+
17+
expect(Postcode.onspd.where(postcode: "E12AA", large_user_postcode: true).count).to eq(1)
18+
expect(Postcode.onspd.where(postcode: "E13AA", large_user_postcode: false).count).to eq(1)
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)