-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2bc47e6
commit c718c14
Showing
7 changed files
with
326 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
defmodule HamsterTravel.Geo.City do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "cities" do | ||
field :name, :string | ||
field :name_ru, :string | ||
field :geonames_id, :string | ||
field :lat, :float | ||
field :lon, :float | ||
field :population, :integer | ||
|
||
belongs_to :country, HamsterTravel.Geo.Country, | ||
foreign_key: :country_code, | ||
references: :iso, | ||
type: :string | ||
|
||
# TODO: will this work???? | ||
belongs_to :region, HamsterTravel.Geo.Region, | ||
foreign_key: :region_code, | ||
references: :region_code, | ||
type: :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(city, attrs) do | ||
city | ||
|> cast(attrs, [ | ||
:name, | ||
:name_ru, | ||
:region_code, | ||
:geonames_id, | ||
:country_code, | ||
:lat, | ||
:lon, | ||
:population | ||
]) | ||
|> validate_required([ | ||
:name, | ||
:name_ru, | ||
:region_code, | ||
:geonames_id, | ||
:country_code, | ||
:lat, | ||
:lon, | ||
:population | ||
]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule HamsterTravel.Geo.Region do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "regions" do | ||
field :name, :string | ||
field :name_ru, :string | ||
field :region_code, :string | ||
field :geonames_id, :string | ||
field :lat, :float | ||
field :lon, :float | ||
|
||
belongs_to :country, HamsterTravel.Geo.Country, | ||
foreign_key: :country_code, | ||
references: :iso, | ||
type: :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(region, attrs) do | ||
region | ||
|> cast(attrs, [:name, :name_ru, :region_code, :geonames_id, :country_code, :lat, :lon]) | ||
|> validate_required([:name, :name_ru, :region_code, :geonames_id, :country_code, :lat, :lon]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule HamsterTravel.Repo.Migrations.CreateRegions do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:regions) do | ||
add :name, :string, null: false | ||
add :name_ru, :string | ||
add :region_code, :string, null: false | ||
add :geonames_id, :string, null: false | ||
add :country_code, references("countries", column: :iso, type: :string), null: false | ||
add :lat, :float | ||
add :lon, :float | ||
|
||
timestamps() | ||
end | ||
|
||
create unique_index(:regions, [:geonames_id]) | ||
create unique_index(:regions, [:country_code, :region_code]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule HamsterTravel.Repo.Migrations.CreateCities do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:cities) do | ||
add :name, :string, null: false | ||
add :name_ru, :string | ||
|
||
add :region_code, | ||
references("regions", | ||
column: :region_code, | ||
type: :string, | ||
with: [country_code: :country_code] | ||
), | ||
null: false | ||
|
||
add :geonames_id, :string, null: false | ||
add :country_code, references("countries", column: :iso, type: :string), null: false | ||
add :lat, :float | ||
add :lon, :float | ||
add :population, :integer, null: false | ||
|
||
timestamps() | ||
end | ||
|
||
execute "CREATE EXTENSION IF NOT EXISTS pg_trgm;" | ||
|
||
execute """ | ||
CREATE INDEX cities_name_gin_trgm_idx | ||
ON cities | ||
USING gin (name gin_trgm_ops); | ||
""" | ||
|
||
execute """ | ||
CREATE INDEX cities_name_ru_gin_trgm_idx | ||
ON cities | ||
USING gin (name_ru gin_trgm_ops); | ||
""" | ||
|
||
create unique_index(:cities, [:geonames_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters