Module: Basho::DB
- Defined in:
- lib/basho/db.rb,
lib/basho/db/city.rb,
lib/basho/db/prefecture.rb
Overview
ActiveRecordバックエンド(オプション)。 +basho_prefectures+ / +basho_cities+ テーブルへのアクセスとシードを提供する。
Defined Under Namespace
Classes: City, Prefecture
Class Method Summary collapse
-
.seed! ⇒ Hash{Symbol => Integer}
JSONデータをDBに一括投入する。冪等(何度実行しても同じ結果)。 upsert_allで既存レコードを保持しつつ更新し、gemデータから消えた市区町村は論理削除する。.
-
.seed_fresh? ⇒ Boolean
DBのアクティブな市区町村件数がgemの同梱データと一致するか判定する。 市区町村の合併・分割で件数が変わるため、不一致はシード更新が必要なサイン。.
Class Method Details
.seed! ⇒ Hash{Symbol => Integer}
JSONデータをDBに一括投入する。冪等(何度実行しても同じ結果)。 upsert_allで既存レコードを保持しつつ更新し、gemデータから消えた市区町村は論理削除する。
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/basho/db.rb', line 15 def self.seed! prefs = prefecture_rows cities = city_rows ::ActiveRecord::Base.transaction do Prefecture.upsert_all(prefs, unique_by: :code) upsert_cities(cities) deprecate_removed_cities(cities) end { prefectures: prefs.size, cities: cities.size } end |
.seed_fresh? ⇒ Boolean
DBのアクティブな市区町村件数がgemの同梱データと一致するか判定する。 市区町村の合併・分割で件数が変わるため、不一致はシード更新が必要なサイン。
32 33 34 35 36 37 |
# File 'lib/basho/db.rb', line 32 def self.seed_fresh? expected = (1..47).sum { |code| Data::Loader.cities(code).size } City.active.count == expected rescue ::ActiveRecord::ActiveRecordError false end |