From ce8d96333131ca83dcad75bfe040fe28f10bf250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Tue, 12 Aug 2025 13:30:01 +0200 Subject: [PATCH] build(phoenix): use sqlite instead of postgres --- .gitignore | 4 ++++ config/config.exs | 2 +- config/dev.exs | 11 ++++------- config/runtime.exs | 18 ++++++------------ config/test.exs | 11 ++++------- lib/learning_phoenix/application.ex | 7 +++++++ lib/learning_phoenix/repo.ex | 2 +- lib/learning_phoenix_web/endpoint.ex | 2 +- mix.exs | 2 +- mix.lock | 3 ++- 10 files changed, 31 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index d8cba90..e1f42fc 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,7 @@ learning_phoenix-*.tar npm-debug.log /assets/node_modules/ +# Database files +*.db +*.db-* + diff --git a/config/config.exs b/config/config.exs index f9797c9..c126fae 100644 --- a/config/config.exs +++ b/config/config.exs @@ -20,7 +20,7 @@ config :learning_phoenix, LearningPhoenixWeb.Endpoint, layout: false ], pubsub_server: LearningPhoenix.PubSub, - live_view: [signing_salt: "6KnRBd1u"] + live_view: [signing_salt: "U5f+cEa0"] # Configures the mailer # diff --git a/config/dev.exs b/config/dev.exs index 4e60486..a82009c 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -2,13 +2,10 @@ import Config # Configure your database config :learning_phoenix, LearningPhoenix.Repo, - username: "postgres", - password: "postgres", - hostname: "localhost", - database: "learning_phoenix_dev", + database: Path.expand("../learning_phoenix_dev.db", __DIR__), + pool_size: 5, stacktrace: true, - show_sensitive_data_on_connection_error: true, - pool_size: 10 + show_sensitive_data_on_connection_error: true # For development, we disable any cache and enable # debugging and code reloading. @@ -23,7 +20,7 @@ config :learning_phoenix, LearningPhoenixWeb.Endpoint, check_origin: false, code_reloader: true, debug_errors: true, - secret_key_base: "PHGloVHjG6wlc7H7AHlnsKEaz7lpSvLf4xktetauKxkuVHp0ILosEQcBDMUilq1f", + secret_key_base: "R8eKg8o2XOUxMSUrfzLxQ3qDFrBpaJxiYW1xRo8BnT1fGZiTWsAzNJjsIWGwhj6X", watchers: [ esbuild: {Esbuild, :install_and_run, [:learning_phoenix, ~w(--sourcemap=inline --watch)]}, tailwind: {Tailwind, :install_and_run, [:learning_phoenix, ~w(--watch)]} diff --git a/config/runtime.exs b/config/runtime.exs index 0e20475..bbb9842 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -21,22 +21,16 @@ if System.get_env("PHX_SERVER") do end if config_env() == :prod do - database_url = - System.get_env("DATABASE_URL") || + database_path = + System.get_env("DATABASE_PATH") || raise """ - environment variable DATABASE_URL is missing. - For example: ecto://USER:PASS@HOST/DATABASE + environment variable DATABASE_PATH is missing. + For example: /etc/learning_phoenix/learning_phoenix.db """ - maybe_ipv6 = if System.get_env("ECTO_IPV6") in ~w(true 1), do: [:inet6], else: [] - config :learning_phoenix, LearningPhoenix.Repo, - # ssl: true, - url: database_url, - pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"), - # For machines with several cores, consider starting multiple pools of `pool_size` - # pool_count: 4, - socket_options: maybe_ipv6 + database: database_path, + pool_size: String.to_integer(System.get_env("POOL_SIZE") || "5") # The secret key base is used to sign/encrypt cookies and other secrets. # A default value is used in config/dev.exs and config/test.exs but you diff --git a/config/test.exs b/config/test.exs index 5263418..882882c 100644 --- a/config/test.exs +++ b/config/test.exs @@ -6,18 +6,15 @@ import Config # to provide built-in test partitioning in CI environment. # Run `mix help test` for more information. config :learning_phoenix, LearningPhoenix.Repo, - username: "postgres", - password: "postgres", - hostname: "localhost", - database: "learning_phoenix_test#{System.get_env("MIX_TEST_PARTITION")}", - pool: Ecto.Adapters.SQL.Sandbox, - pool_size: System.schedulers_online() * 2 + database: Path.expand("../learning_phoenix_test.db", __DIR__), + pool_size: 5, + pool: Ecto.Adapters.SQL.Sandbox # We don't run a server during test. If one is required, # you can enable the server option below. config :learning_phoenix, LearningPhoenixWeb.Endpoint, http: [ip: {127, 0, 0, 1}, port: 4002], - secret_key_base: "T/9ib796V1QpkkDHld7PxiBA/5GCmREIb0d9uYUAM6DNftWek9K/SVSelfjqBy3Z", + secret_key_base: "Htm54SqKYNSmzPpyH9Ifs7JKcCm41Xt4DYIEJuqHOocaM+IYHfNFYb4q+tOBPgQ4", server: false # In test we don't send emails diff --git a/lib/learning_phoenix/application.ex b/lib/learning_phoenix/application.ex index 609086c..9067fea 100644 --- a/lib/learning_phoenix/application.ex +++ b/lib/learning_phoenix/application.ex @@ -10,6 +10,8 @@ defmodule LearningPhoenix.Application do children = [ LearningPhoenixWeb.Telemetry, LearningPhoenix.Repo, + {Ecto.Migrator, + repos: Application.fetch_env!(:learning_phoenix, :ecto_repos), skip: skip_migrations?()}, {DNSCluster, query: Application.get_env(:learning_phoenix, :dns_cluster_query) || :ignore}, {Phoenix.PubSub, name: LearningPhoenix.PubSub}, # Start a worker by calling: LearningPhoenix.Worker.start_link(arg) @@ -31,4 +33,9 @@ defmodule LearningPhoenix.Application do LearningPhoenixWeb.Endpoint.config_change(changed, removed) :ok end + + defp skip_migrations?() do + # By default, sqlite migrations are run when using a release + System.get_env("RELEASE_NAME") == nil + end end diff --git a/lib/learning_phoenix/repo.ex b/lib/learning_phoenix/repo.ex index c944ef4..03fbf46 100644 --- a/lib/learning_phoenix/repo.ex +++ b/lib/learning_phoenix/repo.ex @@ -1,5 +1,5 @@ defmodule LearningPhoenix.Repo do use Ecto.Repo, otp_app: :learning_phoenix, - adapter: Ecto.Adapters.Postgres + adapter: Ecto.Adapters.SQLite3 end diff --git a/lib/learning_phoenix_web/endpoint.ex b/lib/learning_phoenix_web/endpoint.ex index 9bcefba..455e6e1 100644 --- a/lib/learning_phoenix_web/endpoint.ex +++ b/lib/learning_phoenix_web/endpoint.ex @@ -7,7 +7,7 @@ defmodule LearningPhoenixWeb.Endpoint do @session_options [ store: :cookie, key: "_learning_phoenix_key", - signing_salt: "RuSasXTJ", + signing_salt: "FPy3oQ90", same_site: "Lax" ] diff --git a/mix.exs b/mix.exs index e7d9245..20c4b00 100644 --- a/mix.exs +++ b/mix.exs @@ -43,7 +43,7 @@ defmodule LearningPhoenix.MixProject do {:phoenix, "~> 1.8.0"}, {:phoenix_ecto, "~> 4.5"}, {:ecto_sql, "~> 3.13"}, - {:postgrex, ">= 0.0.0"}, + {:ecto_sqlite3, ">= 0.0.0"}, {:phoenix_html, "~> 4.1"}, {:phoenix_live_reload, "~> 1.2", only: :dev}, {:phoenix_live_view, "~> 1.1.0"}, diff --git a/mix.lock b/mix.lock index c9765b4..176c653 100644 --- a/mix.lock +++ b/mix.lock @@ -6,9 +6,11 @@ "dns_cluster": {:hex, :dns_cluster, "0.2.0", "aa8eb46e3bd0326bd67b84790c561733b25c5ba2fe3c7e36f28e88f384ebcb33", [:mix], [], "hexpm", "ba6f1893411c69c01b9e8e8f772062535a4cf70f3f35bcc964a324078d8c8240"}, "ecto": {:hex, :ecto, "3.13.2", "7d0c0863f3fc8d71d17fc3ad3b9424beae13f02712ad84191a826c7169484f01", [: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", "669d9291370513ff56e7b7e7081b7af3283d02e046cf3d403053c557894a0b3e"}, "ecto_sql": {:hex, :ecto_sql, "3.13.2", "a07d2461d84107b3d037097c822ffdd36ed69d1cf7c0f70e12a3d1decf04e2e1", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.13.0", [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", "539274ab0ecf1a0078a6a72ef3465629e4d6018a3028095dc90f60a19c371717"}, + "ecto_sqlite3": {:hex, :ecto_sqlite3, "0.21.0", "8531f5044fb08289b3aacd21e383a9fb187e5a78981b9ed6d0929a78a25c2341", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.13.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.13.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:exqlite, "~> 0.22", [hex: :exqlite, repo: "hexpm", optional: false]}], "hexpm", "9c3e90ea33099ca0ddd160c8d9eaf80d7d4a9b110d325fa6ed0409858a714606"}, "elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"}, "esbuild": {:hex, :esbuild, "0.10.0", "b0aa3388a1c23e727c5a3e7427c932d89ee791746b0081bbe56103e9ef3d291f", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "468489cda427b974a7cc9f03ace55368a83e1a7be12fba7e30969af78e5f8c70"}, "expo": {:hex, :expo, "1.1.0", "f7b9ed7fb5745ebe1eeedf3d6f29226c5dd52897ac67c0f8af62a07e661e5c75", [:mix], [], "hexpm", "fbadf93f4700fb44c331362177bdca9eeb8097e8b0ef525c9cc501cb9917c960"}, + "exqlite": {:hex, :exqlite, "0.33.0", "2cc96c4227fbb2d0864716def736dff18afb9949b1eaa74630822a0865b4b342", [: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", "8a7c2792e567bbebb4dafe96f6397f1c527edd7039d74f508a603817fbad2844"}, "file_system": {:hex, :file_system, "1.1.0", "08d232062284546c6c34426997dd7ef6ec9f8bbd090eb91780283c9016840e8f", [:mix], [], "hexpm", "bfcf81244f416871f2a2e15c1b515287faa5db9c6bcf290222206d120b3d43f6"}, "finch": {:hex, :finch, "0.20.0", "5330aefb6b010f424dcbbc4615d914e9e3deae40095e73ab0c1bb0968933cadf", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2658131a74d051aabfcba936093c903b8e89da9a1b63e430bee62045fa9b2ee2"}, "fine": {:hex, :fine, "0.1.3", "5809ac079cc62cd16a30b9a015d4f667418239f3ff77631bf8c9462ec22be404", [:mix], [], "hexpm", "4d8b38ec0d2f23ba2ed3c3b65d0ca7d914298336eeea8c591e89372083241505"}, @@ -31,7 +33,6 @@ "phoenix_template": {:hex, :phoenix_template, "1.0.4", "e2092c132f3b5e5b2d49c96695342eb36d0ed514c5b252a77048d5969330d639", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "2c0c81f0e5c6753faf5cca2f229c9709919aba34fab866d3bc05060c9c444206"}, "plug": {:hex, :plug, "1.18.1", "5067f26f7745b7e31bc3368bc1a2b818b9779faa959b49c934c17730efc911cf", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "57a57db70df2b422b564437d2d33cf8d33cd16339c1edb190cd11b1a3a546cc2"}, "plug_crypto": {:hex, :plug_crypto, "2.1.1", "19bda8184399cb24afa10be734f84a16ea0a2bc65054e23a62bb10f06bc89491", [:mix], [], "hexpm", "6470bce6ffe41c8bd497612ffde1a7e4af67f36a15eea5f921af71cf3e11247c"}, - "postgrex": {:hex, :postgrex, "0.21.1", "2c5cc830ec11e7a0067dd4d623c049b3ef807e9507a424985b8dcf921224cd88", [: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", "27d8d21c103c3cc68851b533ff99eef353e6a0ff98dc444ea751de43eb48bdac"}, "req": {:hex, :req, "0.5.15", "662020efb6ea60b9f0e0fac9be88cd7558b53fe51155a2d9899de594f9906ba9", [: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", "a6513a35fad65467893ced9785457e91693352c70b58bbc045b47e5eb2ef0c53"}, "swoosh": {:hex, :swoosh, "1.19.5", "5abd71be78302ba21be56a2b68d05c9946ff1f1bd254f949efef09d253b771ac", [:mix], [{:bandit, ">= 1.0.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:cowboy, "~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:ex_aws, "~> 2.1", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:finch, "~> 0.6", [hex: :finch, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13 or ~> 1.0", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mua, "~> 0.2.3", [hex: :mua, repo: "hexpm", optional: true]}, {:multipart, "~> 0.4", [hex: :multipart, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:req, "~> 0.5.10 or ~> 0.6 or ~> 1.0", [hex: :req, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c953f51ee0a8b237e0f4307c9cefd3eb1eb751c35fcdda2a8bccb991766473be"}, "tailwind": {:hex, :tailwind, "0.3.1", "a89d2835c580748c7a975ad7dd3f2ea5e63216dc16d44f9df492fbd12c094bed", [:mix], [], "hexpm", "98a45febdf4a87bc26682e1171acdedd6317d0919953c353fcd1b4f9f4b676a2"},