From f9fdfa5c2a0888906b292fbfa142f0980e37a5d6 Mon Sep 17 00:00:00 2001 From: mdwaud Date: Thu, 5 Oct 2023 10:45:50 -0400 Subject: [PATCH] Raise if using an in memory database with pool_size != 1 (#128) --- CHANGELOG.md | 2 ++ lib/ecto/adapters/sqlite3.ex | 6 ++++++ test/ecto/adapters/sqlite3_test.exs | 14 ++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index de11682..dcd3538 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ project adheres to [Semantic Versioning][semver]. ## Unreleased +- changed: raise if an in memory database is opened with a pool_size != 1 + ## v0.11.0 - added: Support for DDL transactions. diff --git a/lib/ecto/adapters/sqlite3.ex b/lib/ecto/adapters/sqlite3.ex index 9365d36..69c065a 100644 --- a/lib/ecto/adapters/sqlite3.ex +++ b/lib/ecto/adapters/sqlite3.ex @@ -210,6 +210,7 @@ defmodule Ecto.Adapters.SQLite3 do @impl Ecto.Adapter.Storage def storage_up(options) do database = Keyword.get(options, :database) + pool_size = Keyword.get(options, :pool_size) cond do is_nil(database) -> @@ -226,6 +227,11 @@ defmodule Ecto.Adapters.SQLite3 do File.exists?(database) -> {:error, :already_up} + database == ":memory:" && pool_size != 1 -> + raise ArgumentError, """ + In memory databases must have a pool_size of 1 + """ + true -> {:ok, state} = Exqlite.Connection.connect(options) :ok = Exqlite.Connection.disconnect(:normal, state) diff --git a/test/ecto/adapters/sqlite3_test.exs b/test/ecto/adapters/sqlite3_test.exs index 20b0b1a..087eb0c 100644 --- a/test/ecto/adapters/sqlite3_test.exs +++ b/test/ecto/adapters/sqlite3_test.exs @@ -47,6 +47,20 @@ defmodule Ecto.Adapters.SQLite3ConnTest do fn -> SQLite3.storage_up(mumble: "no database here") == :ok end ) end + + test "can create an in memory database" do + assert SQLite3.storage_up(database: ":memory:", pool_size: 1) == :ok + end + + test "fails if in memory database does not have a pool size of 1" do + assert_raise( + ArgumentError, + """ + In memory databases must have a pool_size of 1 + """, + fn -> SQLite3.storage_up(database: ":memory:", pool_size: 2) end + ) + end end describe ".storage_down/2" do