Commit 11c5feb9 by Trịnh Hoàng Phúc

Merge branch 'feature/rake_task_import_csv' into 'master'

Rake task import csv jobs, change encoding mysql

See merge request !10
parents 550a2c5c 8f9bc6ed
Pipeline #579 failed with stages
in 0 seconds
default: &default
adapter: mysql2
encoding: utf8
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: dev
password: dev
......
......@@ -12,7 +12,7 @@
ActiveRecord::Schema.define(version: 2020_04_08_025325) do
create_table "applies", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "applies", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "job_id", null: false
t.datetime "created_at", precision: 6, null: false
......@@ -21,20 +21,20 @@ ActiveRecord::Schema.define(version: 2020_04_08_025325) do
t.index ["user_id"], name: "index_applies_on_user_id"
end
create_table "cities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "cities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "title"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "cities_jobs", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "cities_jobs", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "city_id", null: false
t.bigint "job_id", null: false
t.index ["city_id", "job_id"], name: "index_cities_jobs_on_city_id_and_job_id"
t.index ["job_id", "city_id"], name: "index_cities_jobs_on_job_id_and_city_id"
end
create_table "companies", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "companies", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "title"
t.string "address"
t.string "logo"
......@@ -43,7 +43,7 @@ ActiveRecord::Schema.define(version: 2020_04_08_025325) do
t.datetime "updated_at", precision: 6, null: false
end
create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "job_id", null: false
t.datetime "created_at", precision: 6, null: false
......@@ -52,20 +52,20 @@ ActiveRecord::Schema.define(version: 2020_04_08_025325) do
t.index ["user_id"], name: "index_favorites_on_user_id"
end
create_table "industries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "industries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "title"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "industries_jobs", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "industries_jobs", id: false, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "industry_id", null: false
t.bigint "job_id", null: false
t.index ["industry_id", "job_id"], name: "index_industries_jobs_on_industry_id_and_job_id"
t.index ["job_id", "industry_id"], name: "index_industries_jobs_on_job_id_and_industry_id"
end
create_table "jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "title"
t.string "updated_date_job"
t.string "level"
......@@ -79,7 +79,7 @@ ActiveRecord::Schema.define(version: 2020_04_08_025325) do
t.index ["company_id"], name: "index_jobs_on_company_id"
end
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
......
This source diff could not be displayed because it is too large. You can view the blob instead.
require 'csv'
namespace :import do
desc "TODO"
task csv: :environment do
csv_text = File.read('jobs.csv')
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
# Job attributes
job_attributes = {
title: row[9],
level: row[8],
salary: row[11],
job_description: row[7],
company_id: nil,
}
# Company attributes
company_attributes = {
title: row[5],
address: row[2],
logo: "https://via.placeholder.com/66x38?text=Logo",
description: row[14]
}
# Check exist or create company
job_attributes[:company_id] = check_exist_or_create_company(company_attributes)
# Create job
job = check_exist_or_create_job(job_attributes)
# Industry
industry = check_exist_or_create_industry(row[1])
# Industry job
job.industries << industry
# City
city = check_exist_or_create_city(row[16].gsub('["',"").gsub('"]',""))
# City job
job.cities << city
end
end
def check_exist_or_create_company(company_attributes)
find_company = Company.find_or_create_by(company_attributes)
return find_company.id
end
def check_exist_or_create_city(city_title)
find_city = City.find_or_create_by(title: city_title)
return find_city
end
def check_exist_or_create_industry(industry_title)
find_industry = Industry.find_or_create_by(title: industry_title)
return find_industry
end
def check_exist_or_create_job(job_attributes)
job = Job.find_or_create_by(job_attributes)
return job
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment