From 93c84538819b8800b9b83514e127d22e616a41b2 Mon Sep 17 00:00:00 2001 From: Philip Giuliani Date: Wed, 21 Dec 2022 15:37:55 +0100 Subject: [PATCH] Use Connection.connect instead of Sqlite3.open --- lib/ecto/adapters/sqlite3.ex | 48 ++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/lib/ecto/adapters/sqlite3.ex b/lib/ecto/adapters/sqlite3.ex index 603f737..058ceda 100644 --- a/lib/ecto/adapters/sqlite3.ex +++ b/lib/ecto/adapters/sqlite3.ex @@ -209,10 +209,27 @@ defmodule Ecto.Adapters.SQLite3 do @impl Ecto.Adapter.Storage def storage_up(options) do - storage_up_with_path( - Keyword.get(options, :database), - Keyword.get(options, :journal_mode, :wal) - ) + database = Keyword.get(options, :database) + + cond do + is_nil(database) -> + raise ArgumentError, + """ + No SQLite database path specified. Please check the configuration for your Repo. + Your config/*.exs file should have something like this in it: + + config :my_app, MyApp.Repo, + adapter: Ecto.Adapters.SQLite3, + database: "/path/to/sqlite/database" + """ + + File.exists?(database) -> + {:error, :already_up} + + true -> + {:ok, state} = Exqlite.Connection.connect(options) + :ok = Exqlite.Connection.disconnect(:normal, state) + end end @impl Ecto.Adapter.Migration @@ -446,29 +463,6 @@ defmodule Ecto.Adapters.SQLite3 do ## HELPERS ## - defp storage_up_with_path(nil, _) do - raise ArgumentError, - """ - No SQLite database path specified. Please check the configuration for your Repo. - Your config/*.exs file should have something like this in it: - - config :my_app, MyApp.Repo, - adapter: Ecto.Adapters.SQLite3, - database: "/path/to/sqlite/database" - """ - end - - defp storage_up_with_path(db_path, journal_mode) do - if File.exists?(db_path) do - {:error, :already_up} - else - db_path |> Path.dirname() |> File.mkdir_p!() - {:ok, db} = Exqlite.Sqlite3.open(db_path) - :ok = Exqlite.Sqlite3.execute(db, "PRAGMA JOURNAL_MODE = #{journal_mode}") - :ok = Exqlite.Sqlite3.close(db) - end - end - defp dump_versions(config) do table = config[:migration_source] || "schema_migrations"