Browse Source

Update field name and required fields

pull/1395/head
Kat 3 years ago
parent
commit
5893fce7a1
  1. 2
      app/services/imports/local_authorities_service.rb
  2. 2
      config/local_authorities_data/initial_local_authorities.csv
  3. 6
      db/migrate/20230308101826_create_local_authorities.rb
  4. 6
      db/schema.rb
  5. 2
      spec/fixtures/files/local_authorities.csv
  6. 2
      spec/lib/tasks/local_authorities_import_spec.rb

2
app/services/imports/local_authorities_service.rb

@ -13,7 +13,7 @@ module Imports
CSV.foreach(path, headers: true) do |row| CSV.foreach(path, headers: true) do |row|
LocalAuthority.upsert( LocalAuthority.upsert(
{ code: row["code"], { code: row["code"],
la_name: row["la_name"], name: row["name"],
start_date: Time.zone.local(row["start_year"], 4, 1), start_date: Time.zone.local(row["start_year"], 4, 1),
end_date: (Time.zone.local(row["end_year"], 3, 31) if row["end_year"]), end_date: (Time.zone.local(row["end_year"], 3, 31) if row["end_year"]),
previous_location_only: row["previous_location_only"] || false }, previous_location_only: row["previous_location_only"] || false },

2
config/local_authorities_data/initial_local_authorities.csv

@ -1,4 +1,4 @@
code,la_name,start_year,end_year,previous_location_only code,name,start_year,end_year,previous_location_only
S12000033,Aberdeen City,2021,,true S12000033,Aberdeen City,2021,,true
S12000034,Aberdeenshire,2021,,true S12000034,Aberdeenshire,2021,,true
E07000223,Adur,2021,, E07000223,Adur,2021,,

1 code la_name name start_year end_year previous_location_only
2 S12000033 Aberdeen City 2021 true
3 S12000034 Aberdeenshire 2021 true
4 E07000223 Adur 2021

6
db/migrate/20230308101826_create_local_authorities.rb

@ -1,9 +1,9 @@
class CreateLocalAuthorities < ActiveRecord::Migration[7.0] class CreateLocalAuthorities < ActiveRecord::Migration[7.0]
def change def change
create_table :local_authorities do |t| create_table :local_authorities do |t|
t.string :code t.string :code, null: false
t.string :la_name t.string :name, null: false
t.datetime :start_date t.datetime :start_date, null: false
t.datetime :end_date t.datetime :end_date
t.boolean :previous_location_only, default: false t.boolean :previous_location_only, default: false
t.index %w[code], name: "index_local_authority_code", unique: true t.index %w[code], name: "index_local_authority_code", unique: true

6
db/schema.rb

@ -290,9 +290,9 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_08_101826) do
end end
create_table "local_authorities", force: :cascade do |t| create_table "local_authorities", force: :cascade do |t|
t.string "code" t.string "code", null: false
t.string "la_name" t.string "name", null: false
t.datetime "start_date" t.datetime "start_date", null: false
t.datetime "end_date" t.datetime "end_date"
t.boolean "previous_location_only", default: false t.boolean "previous_location_only", default: false
t.datetime "created_at", null: false t.datetime "created_at", null: false

2
spec/fixtures/files/local_authorities.csv vendored

@ -1,4 +1,4 @@
code,la_name,start_year,end_year,previous_location_only code,name,start_year,end_year,previous_location_only
S12000033,Aberdeen City,2021,,true S12000033,Aberdeen City,2021,,true
S12000034,Aberdeenshire,2021,,true S12000034,Aberdeenshire,2021,,true
E07000223,Adur,2021,, E07000223,Adur,2021,,

1 code la_name name start_year end_year previous_location_only
2 S12000033 Aberdeen City Aberdeen City 2021 true
3 S12000034 Aberdeenshire Aberdeenshire 2021 true
4 E07000223 Adur Adur 2021

2
spec/lib/tasks/local_authorities_import_spec.rb

@ -29,7 +29,7 @@ RSpec.describe "data_import" do
end end
context "when a record already exists with a matching code index" do context "when a record already exists with a matching code index" do
let!(:local_authority) { LocalAuthority.create(code: "S12000041", la_name: "Angus", start_date: Time.zone.local(2021, 4, 1), previous_location_only: false) } let!(:local_authority) { LocalAuthority.create(code: "S12000041", name: "Angus", start_date: Time.zone.local(2021, 4, 1), previous_location_only: false) }
it "updates local authority if the record is matched on code" do it "updates local authority if the record is matched on code" do
task.invoke(local_authorities_file_path) task.invoke(local_authorities_file_path)

Loading…
Cancel
Save