Skip to content

Commit

Permalink
Use built-in Ecto enum type
Browse files Browse the repository at this point in the history
  • Loading branch information
adriankumpf committed Jan 10, 2021
1 parent c637d46 commit 3f1059b
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 64 deletions.
6 changes: 1 addition & 5 deletions lib/teslamate/locations/geo_fence.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ defmodule TeslaMate.Locations.GeoFence do

import Ecto.Changeset

defmodule BillingType do
use EctoEnum.Postgres, type: :billing_type, enums: [:per_kwh, :per_minute]
end

schema "geofences" do
field :name, :string
field :latitude, :decimal, read_after_writes: true
field :longitude, :decimal, read_after_writes: true
field :radius, :integer

field :billing_type, BillingType, read_after_writes: true
field :billing_type, Ecto.Enum, values: [:per_kwh, :per_minute], read_after_writes: true
field :cost_per_unit, :decimal, read_after_writes: true
field :session_fee, :decimal, read_after_writes: true

Expand Down
3 changes: 1 addition & 2 deletions lib/teslamate/log/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ defmodule TeslaMate.Log.State do
use Ecto.Schema
import Ecto.Changeset

alias __MODULE__.State
alias TeslaMate.Log.Car

schema "states" do
field :state, State
field :state, Ecto.Enum, values: [:online, :offline, :asleep]

field :start_date, :utc_datetime_usec
field :end_date, :utc_datetime_usec
Expand Down
3 changes: 0 additions & 3 deletions lib/teslamate/log/state/state.ex

This file was deleted.

8 changes: 3 additions & 5 deletions lib/teslamate/settings/global_settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ defmodule TeslaMate.Settings.GlobalSettings do
use Ecto.Schema
import Ecto.Changeset

alias TeslaMate.Settings.{Units, Range}

schema "settings" do
field :unit_of_length, Units.Length
field :unit_of_temperature, Units.Temperature
field :unit_of_length, Ecto.Enum, values: [:km, :mi]
field :unit_of_temperature, Ecto.Enum, values: [:C, :F]

field :preferred_range, Range
field :preferred_range, Ecto.Enum, values: [:ideal, :rated]

field :base_url, :string
field :grafana_url, :string
Expand Down
3 changes: 0 additions & 3 deletions lib/teslamate/settings/range.ex

This file was deleted.

3 changes: 0 additions & 3 deletions lib/teslamate/settings/units/length.ex

This file was deleted.

3 changes: 0 additions & 3 deletions lib/teslamate/settings/units/temperature.ex

This file was deleted.

3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ defmodule TeslaMate.MixProject do
{:gettext, "~> 0.11"},
{:jason, "~> 1.0"},
{:plug_cowboy, "~> 2.0"},
{:gen_state_machine, "~> 2.0"},
{:ecto_enum, "~> 1.0"},
{:gen_state_machine, "~> 3.0", override: true},
{:phoenix_live_view, "~> 0.1"},
{:floki, "~> 0.23"},
{:tortoise, "~> 0.9.4"},
Expand Down
5 changes: 2 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
"db_connection": {:hex, :db_connection, "2.3.1", "4c9f3ed1ef37471cbdd2762d6655be11e38193904d9c5c1c9389f1b891a3088e", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "abaab61780dde30301d840417890bd9f74131041afd02174cf4e10635b3a63f5"},
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
"ecto": {:hex, :ecto, "3.5.5", "48219a991bb86daba6e38a1e64f8cea540cded58950ff38fbc8163e062281a07", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "98dd0e5e1de7f45beca6130d13116eae675db59adfa055fb79612406acf6f6f1"},
"ecto_enum": {:hex, :ecto_enum, "1.4.0", "d14b00e04b974afc69c251632d1e49594d899067ee2b376277efd8233027aec8", [:mix], [{:ecto, ">= 3.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "> 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "8fb55c087181c2b15eee406519dc22578fa60dd82c088be376d0010172764ee4"},
"ecto_sql": {:hex, :ecto_sql, "3.5.3", "1964df0305538364b97cc4661a2bd2b6c89d803e66e5655e4e55ff1571943efd", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.5.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0 or ~> 0.4.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d2f53592432ce17d3978feb8f43e8dc0705e288b0890caf06d449785f018061c"},
"ex_cldr": {:hex, :ex_cldr, "2.18.2", "c0557145c234a4d31ff450a0438c5a70e786ccba9449a9f9f895809be20bed7d", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:cldr_utils, "~> 2.12", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.13", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "ac28055ae6df438b72f98703c842c2c0d969af6bd68662a8f586862a9a0ddc79"},
"excoveralls": {:hex, :excoveralls, "0.13.4", "7b0baee01fe150ef81153e6ffc0fc68214737f54570dc257b3ca4da8e419b812", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "faae00b3eee35cdf0342c10b669a7c91f942728217d2a7c7f644b24d391e6190"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"finch": {:hex, :finch, "0.6.0", "b2d5dc82603ee88bcd05ad06ee57ad2b3a07e0f9fdcd2a28087d8fb61e09aaf4", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.2", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "668692f629943711e935acc5c426d7ff5bbc391b162b27775dabbf0fbbb31dd6"},
"floki": {:hex, :floki, "0.29.0", "b1710d8c93a2f860dc2d7adc390dd808dc2fb8f78ee562304457b75f4c640881", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "008585ce64b9f74c07d32958ec9866f4b8a124bf4da1e2941b28e41384edaaad"},
"fuse": {:hex, :fuse, "2.4.2", "9106b08db8793a34cc156177d7e24c41bd638ee1b28463cb76562fde213e8ced", [:rebar3], [], "hexpm", "d733fe913ba3e61ca51f78a3958237e5a24295a03d2ee358288d53bd947f7788"},
"gen_state_machine": {:hex, :gen_state_machine, "2.1.0", "a38b0e53fad812d29ec149f0d354da5d1bc0d7222c3711f3a0bd5aa608b42992", [:mix], [], "hexpm", "ae367038808db25cee2f2c4b8d0531522ea587c4995eb6f96ee73410a60fa06b"},
"gen_state_machine": {:hex, :gen_state_machine, "3.0.0", "1e57f86a494e5c6b14137ebef26a7eb342b3b0070c7135f2d6768ed3f6b6cdff", [:mix], [], "hexpm", "0a59652574bebceb7309f6b749d2a41b45fdeda8dbb4da0791e355dd19f0ed15"},
"gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"},
"hackney": {:hex, :hackney, "1.17.0", "717ea195fd2f898d9fe9f1ce0afcc2621a41ecfe137fae57e7fe6e9484b9aa99", [:rebar3], [{:certifi, "~>2.5", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "64c22225f1ea8855f584720c0e5b3cd14095703af1c9fbc845ba042811dc671c"},
"html_entities": {:hex, :html_entities, "0.5.1", "1c9715058b42c35a2ab65edc5b36d0ea66dd083767bef6e3edb57870ef556549", [:mix], [], "hexpm", "30efab070904eb897ff05cd52fa61c1025d7f8ef3a9ca250bc4e6513d16c32de"},
Expand All @@ -34,7 +33,7 @@
"nimble_options": {:hex, :nimble_options, "0.3.5", "a4f6820cdcb4ee444afd78635f323e58e8a5ddf2fbbe9b9d283a99f972034bae", [:mix], [], "hexpm", "f5507cc90033a8d12769522009c80aa9164af6bab245dbd4ad421d008455f1e1"},
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
"nimble_pool": {:hex, :nimble_pool, "0.2.3", "4b84df87cf8b40c7363782a99faad6aa2bb0811bcd3d275b5402ae4bab1f1251", [:mix], [], "hexpm", "a6bf677d3499ef1639c42bf16b8a72bf490f5fed70206d5851d43dd750c7eaca"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"phoenix": {:hex, :phoenix, "1.5.7", "2923bb3af924f184459fe4fa4b100bd25fa6468e69b2803dfae82698269aa5e0", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "774cd64417c5a3788414fdbb2be2eb9bcd0c048d9e6ad11a0c1fd67b7c0d0978"},
"phoenix_ecto": {:hex, :phoenix_ecto, "4.2.1", "13f124cf0a3ce0f1948cf24654c7b9f2347169ff75c1123f44674afee6af3b03", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 2.15", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "478a1bae899cac0a6e02be1deec7e2944b7754c04e7d4107fc5a517f877743c0"},
"phoenix_html": {:hex, :phoenix_html, "2.14.3", "51f720d0d543e4e157ff06b65de38e13303d5778a7919bcc696599e5934271b8", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "efd697a7fff35a13eeeb6b43db884705cba353a1a41d127d118fda5f90c8e80f"},
Expand Down
14 changes: 4 additions & 10 deletions priv/repo/migrations/20190330180000_create_states.exs
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
defmodule TeslaMate.Repo.Migrations.CreateStates do
use Ecto.Migration

alias TeslaMate.Log.State.State

def up do
State.create_type()
def change do
execute "CREATE TYPE states_status AS ENUM ('online', 'offline', 'asleep')",
"DROP TYPE states_status "

create table(:states) do
add(:state, State.type(), null: false)
add(:state, :states_status, null: false)

add(:start_date, :utc_datetime, null: false)
add(:end_date, :utc_datetime)

add(:car_id, references(:cars), null: false)
end
end

def down do
drop(table(:states))
execute("DROP TYPE states_status")
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,17 @@ defmodule TeslaMate.Repo.Migrations.UnitOfLegnthAndTemperature do

import Ecto.Query

defmodule Units.Length do
use EctoEnum.Postgres, type: :length, enums: [:km, :mi]
end

defmodule Units.Temperature do
use EctoEnum.Postgres, type: :temperature, enums: [:C, :F]
end

def up do
[use_imperial_units?] =
from(s in "settings", select: s.use_imperial_units)
|> Repo.all()

Units.Length.create_type()
Units.Temperature.create_type()
execute("CREATE TYPE length AS ENUM ('km', 'mi')")
execute("CREATE TYPE temperature AS ENUM ('C', 'F')")

alter table(:settings) do
add(:unit_of_length, Units.Length.type(), default: "km", null: false)
add(:unit_of_temperature, Units.Temperature.type(), default: "C", null: false)
add(:unit_of_length, :length, default: "km", null: false)
add(:unit_of_temperature, :temperature, default: "C", null: false)
remove(:use_imperial_units)
end

Expand Down Expand Up @@ -53,7 +45,7 @@ defmodule TeslaMate.Repo.Migrations.UnitOfLegnthAndTemperature do
add(:use_imperial_units, :boolean, default: false, null: false)
end

execute("DROP TYPE Length")
execute("DROP TYPE length")
execute("DROP TYPE temperature")
end
end
6 changes: 2 additions & 4 deletions priv/repo/migrations/20190913165850_add_range_enum.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
defmodule TeslaMate.Repo.Migrations.AddRangeEnum do
use Ecto.Migration

alias TeslaMate.Settings.Range

def change do
Range.create_type()
execute "CREATE TYPE range AS ENUM ('ideal', 'rated')", "DROP TYPE range"

alter table(:settings) do
add(:preferred_range, Range.type(), default: "rated", null: false)
add :preferred_range, :range, default: "rated", null: false
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ defmodule TeslaMate.Repo.Migrations.UseRatedAsDefaultPreferredRange do

def up do
alter table(:settings) do
modify(:preferred_range, Range.type(), default: "rated", null: false)
modify :preferred_range, :range, default: "rated", null: false
end
end

def down do
alter table(:settings) do
modify(:preferred_range, Range.type(), default: "ideal", null: false)
modify :preferred_range, :range, default: "ideal", null: false
end
end
end
18 changes: 12 additions & 6 deletions priv/repo/migrations/20200528163852_cost_by_minute.exs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
defmodule TeslaMate.Repo.Migrations.CostByMinute do
use Ecto.Migration

defmodule BillingType do
use EctoEnum.Postgres, type: :billing_type, enums: [:per_kwh, :per_minute]
def up do
execute "CREATE TYPE billing_type AS ENUM ('per_kwh', 'per_minute')"

alter table(:geofences) do
add :billing_type, :billing_type, null: false, default: "per_kwh"
end

rename table(:geofences), :cost_per_kwh, to: :cost_per_unit
end

def change do
BillingType.create_type()
def down do
rename table(:geofences), :cost_per_unit, to: :cost_per_kwh

alter table(:geofences) do
add(:billing_type, BillingType.type(), null: false, default: "per_kwh")
remove :billing_type
end

rename table(:geofences), :cost_per_kwh, to: :cost_per_unit
execute "DROP TYPE billing_type"
end
end

0 comments on commit 3f1059b

Please sign in to comment.