From 3f1059b7631115d5f48329d26c36c3f3f99a3d10 Mon Sep 17 00:00:00 2001 From: Adrian Kumpf <8999358+adriankumpf@users.noreply.github.com> Date: Sat, 9 Jan 2021 00:08:36 +0100 Subject: [PATCH] Use built-in Ecto enum type --- lib/teslamate/locations/geo_fence.ex | 6 +----- lib/teslamate/log/state.ex | 3 +-- lib/teslamate/log/state/state.ex | 3 --- lib/teslamate/settings/global_settings.ex | 8 +++----- lib/teslamate/settings/range.ex | 3 --- lib/teslamate/settings/units/length.ex | 3 --- lib/teslamate/settings/units/temperature.ex | 3 --- mix.exs | 3 +-- mix.lock | 5 ++--- .../20190330180000_create_states.exs | 14 ++++---------- ...10105216_unit_of_legnth_and_temperature.exs | 18 +++++------------- .../20190913165850_add_range_enum.exs | 6 ++---- ...30_use_rated_as_default_preferred_range.exs | 4 ++-- .../20200528163852_cost_by_minute.exs | 18 ++++++++++++------ 14 files changed, 33 insertions(+), 64 deletions(-) delete mode 100644 lib/teslamate/log/state/state.ex delete mode 100644 lib/teslamate/settings/range.ex delete mode 100644 lib/teslamate/settings/units/length.ex delete mode 100644 lib/teslamate/settings/units/temperature.ex diff --git a/lib/teslamate/locations/geo_fence.ex b/lib/teslamate/locations/geo_fence.ex index 14be6cd054..028a441df9 100644 --- a/lib/teslamate/locations/geo_fence.ex +++ b/lib/teslamate/locations/geo_fence.ex @@ -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 diff --git a/lib/teslamate/log/state.ex b/lib/teslamate/log/state.ex index 4d09e9a59e..858925cbe9 100644 --- a/lib/teslamate/log/state.ex +++ b/lib/teslamate/log/state.ex @@ -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 diff --git a/lib/teslamate/log/state/state.ex b/lib/teslamate/log/state/state.ex deleted file mode 100644 index cc85adc6e5..0000000000 --- a/lib/teslamate/log/state/state.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule TeslaMate.Log.State.State do - use EctoEnum.Postgres, type: :states_status, enums: [:online, :offline, :asleep] -end diff --git a/lib/teslamate/settings/global_settings.ex b/lib/teslamate/settings/global_settings.ex index 3dfba98336..c06ae5cd04 100644 --- a/lib/teslamate/settings/global_settings.ex +++ b/lib/teslamate/settings/global_settings.ex @@ -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 diff --git a/lib/teslamate/settings/range.ex b/lib/teslamate/settings/range.ex deleted file mode 100644 index c5e8eab713..0000000000 --- a/lib/teslamate/settings/range.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule TeslaMate.Settings.Range do - use EctoEnum.Postgres, type: :range, enums: [:ideal, :rated] -end diff --git a/lib/teslamate/settings/units/length.ex b/lib/teslamate/settings/units/length.ex deleted file mode 100644 index bcf492c587..0000000000 --- a/lib/teslamate/settings/units/length.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule TeslaMate.Settings.Units.Length do - use EctoEnum.Postgres, type: :unit_of_length, enums: [:km, :mi] -end diff --git a/lib/teslamate/settings/units/temperature.ex b/lib/teslamate/settings/units/temperature.ex deleted file mode 100644 index 1c65a331ac..0000000000 --- a/lib/teslamate/settings/units/temperature.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule TeslaMate.Settings.Units.Temperature do - use EctoEnum.Postgres, type: :unit_of_temperature, enums: [:C, :F] -end diff --git a/mix.exs b/mix.exs index 04e6880d1a..0101f64e9a 100644 --- a/mix.exs +++ b/mix.exs @@ -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"}, diff --git a/mix.lock b/mix.lock index 5e5acb715a..0baf78fe54 100644 --- a/mix.lock +++ b/mix.lock @@ -10,7 +10,6 @@ "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"}, @@ -18,7 +17,7 @@ "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"}, @@ -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"}, diff --git a/priv/repo/migrations/20190330180000_create_states.exs b/priv/repo/migrations/20190330180000_create_states.exs index 8d5d31bbc8..686325b87a 100644 --- a/priv/repo/migrations/20190330180000_create_states.exs +++ b/priv/repo/migrations/20190330180000_create_states.exs @@ -1,13 +1,12 @@ 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) @@ -15,9 +14,4 @@ defmodule TeslaMate.Repo.Migrations.CreateStates do add(:car_id, references(:cars), null: false) end end - - def down do - drop(table(:states)) - execute("DROP TYPE states_status") - end end diff --git a/priv/repo/migrations/20190810105216_unit_of_legnth_and_temperature.exs b/priv/repo/migrations/20190810105216_unit_of_legnth_and_temperature.exs index e7beb9c0f1..4f774c2de6 100644 --- a/priv/repo/migrations/20190810105216_unit_of_legnth_and_temperature.exs +++ b/priv/repo/migrations/20190810105216_unit_of_legnth_and_temperature.exs @@ -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 @@ -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 diff --git a/priv/repo/migrations/20190913165850_add_range_enum.exs b/priv/repo/migrations/20190913165850_add_range_enum.exs index d8436df8ca..3af7c5e0b3 100644 --- a/priv/repo/migrations/20190913165850_add_range_enum.exs +++ b/priv/repo/migrations/20190913165850_add_range_enum.exs @@ -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 diff --git a/priv/repo/migrations/20200216121330_use_rated_as_default_preferred_range.exs b/priv/repo/migrations/20200216121330_use_rated_as_default_preferred_range.exs index e38c578b35..0161517768 100644 --- a/priv/repo/migrations/20200216121330_use_rated_as_default_preferred_range.exs +++ b/priv/repo/migrations/20200216121330_use_rated_as_default_preferred_range.exs @@ -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 diff --git a/priv/repo/migrations/20200528163852_cost_by_minute.exs b/priv/repo/migrations/20200528163852_cost_by_minute.exs index 4eb6d0dcae..1f82e5895b 100644 --- a/priv/repo/migrations/20200528163852_cost_by_minute.exs +++ b/priv/repo/migrations/20200528163852_cost_by_minute.exs @@ -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