Browse Source
Import using the new rake task which mimics La Rent Range import from csv rake data_import:sales_ranges\[2022,"/Users/mohseeadmin/development/submit-social-housing-lettings-and-sales-data/config/sales_range_data/2022.csv"\] "Created/updated 1328 LA Sales Range records"CLDC-858-update-sales-property-information
Mo Seedat
2 years ago
5 changed files with 1386 additions and 1 deletions
@ -0,0 +1,2 @@
|
||||
class LaSalesRange < ApplicationRecord |
||||
end |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,15 @@
|
||||
class CreateLaSalesRanges < ActiveRecord::Migration[7.0] |
||||
def change |
||||
create_table :la_sales_ranges do |t| |
||||
t.string :la |
||||
t.string :la_name |
||||
t.integer :beds |
||||
t.integer :soft_min, null: false |
||||
t.integer :soft_max, null: false |
||||
t.integer :start_year, null: false |
||||
|
||||
t.index %i[start_year beds la], unique: true |
||||
t.timestamps |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,27 @@
|
||||
require "csv" |
||||
|
||||
namespace :data_import do |
||||
desc "Import annual sales range data" |
||||
task :sales_ranges, %i[start_year path] => :environment do |_task, args| |
||||
start_year = args[:start_year] |
||||
path = args[:path] |
||||
count = 0 |
||||
|
||||
raise "Usage: rake data_import:sales_ranges[start_year,'path/to/csv_file']" if path.blank? || start_year.blank? |
||||
|
||||
CSV.foreach(path, headers: true) do |row| |
||||
LaSalesRange.upsert( |
||||
{ |
||||
beds: row["beds"], |
||||
start_year: start_year, |
||||
la: row["la"], |
||||
soft_min: row["soft_min"], |
||||
soft_max: row["soft_max"] |
||||
}, |
||||
unique_by: %i[start_year beds la], |
||||
) |
||||
count += 1 |
||||
end |
||||
pp "Created/updated #{count} LA Sales Range records" unless Rails.env.test? |
||||
end |
||||
end |
Loading…
Reference in new issue