From 3e057e0558b34bfdedc7bcf755805cef9c63cccd Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Sun, 11 May 2025 00:34:35 +0200 Subject: [PATCH] Change SQL adapter to Postgres --- config/dev.exs | 2 -- config/runtime.exs | 6 +----- config/test.exs | 2 +- lib/jenot/notes.ex | 2 +- lib/jenot/repo.ex | 2 +- mix.exs | 4 +++- mix.lock | 2 +- .../20241117180924_initial_schema.exs | 20 +++++++++---------- ...20241215203400_update_reminders_schema.exs | 4 ++-- test/jenot/notes_test.exs | 2 +- 10 files changed, 21 insertions(+), 25 deletions(-) diff --git a/config/dev.exs b/config/dev.exs index a4fdd3f..314ea01 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -4,5 +4,3 @@ config :plug_live_reload, patterns: [ ~r"priv/static/.*(html|js|css|png|jpeg|jpg|gif|svg)$" ] - -config :jenot, Jenot.Repo, database: "priv/db_dev.sqlite" diff --git a/config/runtime.exs b/config/runtime.exs index 775696a..4b8e916 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -15,8 +15,4 @@ config :web_push_elixir, vapid_private_key: System.fetch_env!("VAPID_PRIVATE_KEY"), vapid_subject: System.fetch_env!("VAPID_SUBJECT") -config :jenot, Jenot.Repo, key: System.fetch_env!("EXQLITE_SECRET") - -if config_env() == :prod do - config :jenot, Jenot.Repo, database: System.get_env("EXQLITE_PATH") -end +config :jenot, Jenot.Repo, url: System.get_env("DATABASE_URL") diff --git a/config/test.exs b/config/test.exs index db3eb6b..42a7ea6 100644 --- a/config/test.exs +++ b/config/test.exs @@ -1,7 +1,7 @@ import Config config :jenot, Jenot.Repo, - database: "priv/db_test.sqlite", + url: "postgres://postgres:postgres@localhost/jenot_test", pool: Ecto.Adapters.SQL.Sandbox config :logger, :default_handler, level: :warning diff --git a/lib/jenot/notes.ex b/lib/jenot/notes.ex index 7611db1..69f7718 100644 --- a/lib/jenot/notes.ex +++ b/lib/jenot/notes.ex @@ -251,7 +251,7 @@ defmodule Jenot.Notes do defp upsert_note(changeset, opts) do conflict_target = Keyword.fetch!(opts, :target) - type = Ecto.Changeset.get_field(changeset, :type) || :note + type = to_string(Ecto.Changeset.get_field(changeset, :type) || :note) title = Ecto.Changeset.get_field(changeset, :title) || "" content = Ecto.Changeset.get_field(changeset, :content) || "" deleted_at = Ecto.Changeset.get_field(changeset, :deleted_at) diff --git a/lib/jenot/repo.ex b/lib/jenot/repo.ex index 51412dc..55e58b7 100644 --- a/lib/jenot/repo.ex +++ b/lib/jenot/repo.ex @@ -1,3 +1,3 @@ defmodule Jenot.Repo do - use Ecto.Repo, otp_app: :jenot, adapter: Ecto.Adapters.SQLite3 + use Ecto.Repo, otp_app: :jenot, adapter: Ecto.Adapters.Postgres end diff --git a/mix.exs b/mix.exs index 67a6146..01b3f1c 100644 --- a/mix.exs +++ b/mix.exs @@ -47,7 +47,9 @@ defmodule Jenot.MixProject do {:bcrypt_elixir, "~> 3.0"}, {:hahash, "~> 0.2.0"}, {:web_push_elixir, "~> 0.4.0"}, - {:ecto_sqlite3, "~> 0.17"}, + {:ecto, "~> 3.10"}, + {:ecto_sql, "~> 3.0"}, + {:postgrex, ">= 0.0.0"}, {:plug_live_reload, "~> 0.1.0", only: :dev} ] end diff --git a/mix.lock b/mix.lock index 249941e..f27e1de 100644 --- a/mix.lock +++ b/mix.lock @@ -12,7 +12,6 @@ "decimal": {:hex, :decimal, "2.2.0", "df3d06bb9517e302b1bd265c1e7f16cda51547ad9d99892049340841f3e15836", [:mix], [], "hexpm", "af8daf87384b51b7e611fb1a1f2c4d4876b65ef968fa8bd3adf44cff401c7f21"}, "ecto": {:hex, :ecto, "3.12.4", "267c94d9f2969e6acc4dd5e3e3af5b05cdae89a4d549925f3008b2b7eb0b93c3", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ef04e4101688a67d061e1b10d7bc1fbf00d1d13c17eef08b71d070ff9188f747"}, "ecto_sql": {:hex, :ecto_sql, "3.12.1", "c0d0d60e85d9ff4631f12bafa454bc392ce8b9ec83531a412c12a0d415a3a4d0", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.12", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "aff5b958a899762c5f09028c847569f7dfb9cc9d63bdb8133bff8a5546de6bf5"}, - "ecto_sqlite3": {:hex, :ecto_sqlite3, "0.17.4", "48dd9c6d0fc10875a64545d04f0478b142898b6f0e73ae969becf5726f834d22", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.12", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.12", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:exqlite, "~> 0.22", [hex: :exqlite, repo: "hexpm", optional: false]}], "hexpm", "f67372e0eae5e5cbdd1d145e78e670fc5064d5810adf99d104d364cb920e306a"}, "elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"}, "exqlite": {:hex, :exqlite, "0.27.0", "2ef6021862e74c6253d1fb1f5701bd47e4e779b035d34daf2a13ec83945a05ba", [:make, :mix], [{:cc_precompiler, "~> 0.1", [hex: :cc_precompiler, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.8", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "b947b9db15bb7aad11da6cd18a0d8b78f7fcce89508a27a5b9be18350fe12c59"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, @@ -35,6 +34,7 @@ "plug_cowboy": {:hex, :plug_cowboy, "2.7.2", "fdadb973799ae691bf9ecad99125b16625b1c6039999da5fe544d99218e662e4", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "245d8a11ee2306094840c000e8816f0cbed69a23fc0ac2bcf8d7835ae019bb2f"}, "plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"}, "plug_live_reload": {:hex, :plug_live_reload, "0.1.1", "5ddf1a08b0dcc9f3623e87492d8a517cb107f9aacb1f3524267cd142e90184b5", [:mix], [{:cowboy, "~> 2.9", [hex: :cowboy, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.1", [hex: :file_system, repo: "hexpm", optional: false]}, {:plug, "~> 1.12", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "ac91b75077b9a454ef035c6a9f58cc1708a27c6a97532aa910f11224e1df1615"}, + "postgrex": {:hex, :postgrex, "0.20.0", "363ed03ab4757f6bc47942eff7720640795eb557e1935951c1626f0d303a3aed", [:mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "d36ef8b36f323d29505314f704e21a1a038e2dc387c6409ee0cd24144e187c0f"}, "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, "req": {:hex, :req, "0.5.7", "b722680e03d531a2947282adff474362a48a02aa54b131196fbf7acaff5e4cee", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "c6035374615120a8923e8089d0c21a3496cf9eda2d287b806081b8f323ceee29"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, diff --git a/priv/repo/migrations/20241117180924_initial_schema.exs b/priv/repo/migrations/20241117180924_initial_schema.exs index 5088c6f..88606f7 100644 --- a/priv/repo/migrations/20241117180924_initial_schema.exs +++ b/priv/repo/migrations/20241117180924_initial_schema.exs @@ -7,7 +7,7 @@ defmodule Jenot.Repo.Migrations.InitialSchema do add :name, :text, null: false add :code_digest, :text, null: false - timestamps(type: :datetime_usec) + timestamps(type: :utc_datetime_usec) end create table(:notes, primary_key: false) do @@ -16,12 +16,12 @@ defmodule Jenot.Repo.Migrations.InitialSchema do add :type, :text, null: false add :title, :text, null: false, default: "" add :content, :text, null: false, default: "" - add :deleted_at, :datetime_usec + add :deleted_at, :utc_datetime_usec - add :account_id, references(:accounts, on_delete: :delete_all), null: false + add :account_id, references(:accounts, on_delete: :delete_all, type: :uuid), null: false - add :server_updated_at, :datetime_usec, null: false - timestamps(type: :datetime_usec) + add :server_updated_at, :utc_datetime_usec, null: false + timestamps(type: :utc_datetime_usec) end create index(:notes, [:account_id]) @@ -36,9 +36,9 @@ defmodule Jenot.Repo.Migrations.InitialSchema do add :hash, :text, null: false - add :account_id, references(:accounts, on_delete: :delete_all), null: false + add :account_id, references(:accounts, on_delete: :delete_all, type: :uuid), null: false - timestamps(type: :datetime_usec) + timestamps(type: :utc_datetime_usec) end create index(:subscriptions, [:account_id]) @@ -53,11 +53,11 @@ defmodule Jenot.Repo.Migrations.InitialSchema do add :repeat_period, :text add :repeat_count, :integer - add :deleted_at, :datetime_usec + add :deleted_at, :utc_datetime_usec - add :note_id, references(:note, on_delete: :delete_all), null: false + add :note_id, references(:notes, on_delete: :delete_all, type: :uuid), null: false - timestamps(type: :datetime_usec) + timestamps(type: :utc_datetime_usec) end create index(:reminders, [:note_id]) diff --git a/priv/repo/migrations/20241215203400_update_reminders_schema.exs b/priv/repo/migrations/20241215203400_update_reminders_schema.exs index 85aeba9..58ff6e5 100644 --- a/priv/repo/migrations/20241215203400_update_reminders_schema.exs +++ b/priv/repo/migrations/20241215203400_update_reminders_schema.exs @@ -11,9 +11,9 @@ defmodule Jenot.Repo.Migrations.UpdateRemindersSchema do add :unit, :text, null: true add :enabled, :boolean, null: false - add :note_id, references(:notes, on_delete: :delete_all, type: :binary_id), primary_key: true + add :note_id, references(:notes, on_delete: :delete_all, type: :uuid), primary_key: true - timestamps(type: :datetime_usec) + timestamps(type: :utc_datetime_usec) end end diff --git a/test/jenot/notes_test.exs b/test/jenot/notes_test.exs index eba0167..c293665 100644 --- a/test/jenot/notes_test.exs +++ b/test/jenot/notes_test.exs @@ -35,7 +35,7 @@ defmodule Jenot.NotesTest do assert {:ok, note} = Notes.add(account, %{ "id" => "id_#{timestamp}", - "type" => "tasklist", + "type" => :tasklist, "content" => [ %{checked: true, content: "First task"}, %{checked: false, content: "Second task"}