Skip to content

Commit

Permalink
Refacto db schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Calyhre committed May 20, 2017
1 parent fea5fc9 commit 0f9df63
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

def prices
games = Game.all.includes(:prices).sort_by(&:title).group_by(&:parsed_game_code)
games = Game.includes(:prices).by_game_code
countries = Price.pluck(:country).uniq.sort
currency = Price.pluck(:currency).include?(params[:currency]) ? params[:currency] : nil

Expand All @@ -26,7 +26,7 @@ def prices
end

def best_deals
games = Game.all.includes(:prices).sort_by(&:title).group_by(&:parsed_game_code)
games = Game.includes(:prices).by_game_code
currencies = Price.pluck(:currency).uniq.sort

csv = CSV.generate(headers: true) do |rows|
Expand Down
7 changes: 5 additions & 2 deletions app/models/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ class Game < ApplicationRecord
REGIONS = %w(americas asia europe)

attribute :game_code, :string
attribute :parsed_game_code, :string
attribute :nsuid, :string
attribute :region, :string
attribute :raw_game_code, :string
attribute :nsuid, :string
attribute :title, :string
attribute :release_date, :datetime
attribute :cover_url, :string

has_many :prices

scope :by_title, -> { order('LOWER(title)', :region) }
scope :by_game_code, -> { by_title.group_by(&:game_code) }
end
15 changes: 15 additions & 0 deletions db/migrate/20170520131614_change_game_columns_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ChangeGameColumnsName < ActiveRecord::Migration[5.1]
def change
# Undo previous indexes, we are gonna replace them
remove_index :games, column: [:game_code, :nsuid]
remove_index :games, column: :parsed_game_code

# Rename actual columns
rename_column :games, :game_code, :raw_game_code
rename_column :games, :parsed_game_code, :game_code

# Rebuild better indexes
add_index :games, :game_code
add_index :games, [:game_code, :region], unique: true
end
end
8 changes: 4 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170515170820) do
ActiveRecord::Schema.define(version: 20170520131614) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "games", force: :cascade do |t|
t.string "raw_game_code"
t.string "game_code"
t.string "parsed_game_code"
t.string "nsuid"
t.string "region"
t.string "title"
t.datetime "release_date"
t.string "cover_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["game_code", "nsuid"], name: "index_games_on_game_code_and_nsuid", unique: true
t.index ["game_code", "region"], name: "index_games_on_game_code_and_region", unique: true
t.index ["game_code"], name: "index_games_on_game_code"
t.index ["nsuid"], name: "index_games_on_nsuid"
t.index ["parsed_game_code"], name: "index_games_on_parsed_game_code"
end

create_table "prices", force: :cascade do |t|
Expand Down
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
GAME_CSV_FILE = Rails.root.join('db', 'seeds', 'games.csv')

CSV.foreach(GAME_CSV_FILE, headers: true) do |row|
Game.find_or_create_by(nsuid: row['nsuid'], game_code: row['game_code']).update_attributes(row.to_hash)
Game.find_or_create_by(region: row['region'], game_code: row['game_code']).update_attributes(row.to_hash)
end
2 changes: 1 addition & 1 deletion db/seeds/games.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title,game_code,parsed_game_code,nsuid,region,release_date,cover_url
title,raw_game_code,game_code,nsuid,region,release_date,cover_url
1-2-Switch(ワンツースイッチ),HACAACCA,AACC,70010000000027,asia,2017-03-03,https://www.nintendo.co.jp/software/switch/img/launch_panel_12switch.jpg
Minecraft: Nintendo Switch Edition,HACPAB3NA,AB3N,70010000000412,asia,2017-05-12,https://www.nintendo.co.jp/software/switch/img/HACPAB3NA.jpg
THUMPER リズム・バイオレンスゲーム,HACACGNA,ACGN,70010000000321,asia,2017-05-18,https://www.nintendo.co.jp/software/switch/img/HACACGNA.jpg
Expand Down
8 changes: 4 additions & 4 deletions lib/eshop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def self.list_europe
return games.map do |game|
{
region: 'europe',
game_code: game.dig(:product_code_txt, 0),
parsed_game_code: game.dig(:product_code_txt, 0).match(/\AHAC.(.{4}).\Z/)[1],
raw_game_code: game.dig(:product_code_txt, 0),
game_code: game.dig(:product_code_txt, 0).match(/\AHAC\w?(\w{4})\w\Z/)[1],
title: game[:title],
release_date: Date.parse(game[:date_from]),
nsuid: game.dig(:nsuid_txt, 0),
Expand Down Expand Up @@ -99,8 +99,8 @@ def self.list_americas
next unless game[:game_code] && game[:nsuid]
{
region: 'americas',
game_code: game[:game_code],
parsed_game_code: game[:game_code].match(/\AHAC.(.{4}).\Z/)[1],
raw_game_code: game[:game_code],
game_code: game[:game_code].match(/\AHAC\w?(\w{4})\w\Z/)[1],
title: game[:title],
release_date: Date.parse(game[:release_date]),
nsuid: game[:nsuid],
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/eshop.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace :eshop do
desc 'Get all games from eShop API'
task retrieve_games: :environment do
Eshop::Games.list.map do |raw_game|
Game.find_or_create_by(nsuid: raw_game[:nsuid], game_code: raw_game[:game_code]).update_attributes!(raw_game)
Game.find_or_create_by(region: raw_game[:region], game_code: raw_game[:game_code]).update_attributes!(raw_game)
end
end

Expand Down

0 comments on commit 0f9df63

Please sign in to comment.