From 15b59c12fa7bd6bd86e5300e74c52ca160910763 Mon Sep 17 00:00:00 2001 From: Matt Pope Date: Thu, 25 Sep 2025 08:40:26 -0500 Subject: [PATCH] feat: Add `serialized` as an option to connect. --- lib/exqlite/connection.ex | 11 ++++++++++- test/exqlite/integration_test.exs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/exqlite/connection.ex b/lib/exqlite/connection.ex index 7400a90..4d623c3 100644 --- a/lib/exqlite/connection.ex +++ b/lib/exqlite/connection.ex @@ -142,6 +142,7 @@ defmodule Exqlite.Connection do * `:soft_heap_limit` - The size limit in bytes for the heap limit. * `:hard_heap_limit` - The size limit in bytes for the heap. * `:custom_pragmas` - A list of custom pragmas to set on the connection, for example to configure extensions. + * `:serialized` - A SQLite database which was previously serialized, to load into the database after connection. * `:load_extensions` - A list of paths identifying extensions to load. Defaults to `[]`. The provided list will be merged with the global extensions list, set on `:exqlite, :load_extensions`. Be aware that the path should handle pointing to a library compiled for the current architecture. @@ -510,6 +511,13 @@ defmodule Exqlite.Connection do set_pragma(db, "busy_timeout", Pragma.busy_timeout(options)) end + defp deserialize(db, options) do + case Keyword.get(options, :serialized, nil) do + nil -> :ok + serialized -> Sqlite3.deserialize(db, serialized) + end + end + defp load_extensions(db, options) do global_extensions = Application.get_env(:exqlite, :load_extensions, []) @@ -555,7 +563,8 @@ defmodule Exqlite.Connection do :ok <- set_journal_size_limit(db, options), :ok <- set_soft_heap_limit(db, options), :ok <- set_hard_heap_limit(db, options), - :ok <- load_extensions(db, options) do + :ok <- load_extensions(db, options), + :ok <- deserialize(db, options) do state = %__MODULE__{ db: db, default_transaction_mode: diff --git a/test/exqlite/integration_test.exs b/test/exqlite/integration_test.exs index cd90e08..0ecf478 100644 --- a/test/exqlite/integration_test.exs +++ b/test/exqlite/integration_test.exs @@ -243,4 +243,35 @@ defmodule Exqlite.IntegrationTest do File.rm(path) end + + test "can load a serialized database at startup" do + {:ok, path} = Temp.path() + {:ok, conn} = Sqlite3.open(path) + + :ok = + Sqlite3.execute(conn, "create table test(id integer primary key, stuff text)") + + assert :ok = + Sqlite3.execute(conn, "insert into test(id, stuff) values (1, 'hello')") + + assert {:ok, binary} = Sqlite3.serialize(conn, "main") + assert is_binary(binary) + Sqlite3.close(conn) + File.rm(path) + + {:ok, conn} = + DBConnection.start_link(Connection, + idle_interval: 5_000, + database: :memory, + journal_mode: :wal, + cache_size: -64_000, + temp_store: :memory, + serialized: binary + ) + + query = %Query{statement: "select id, stuff from test"} + {:ok, _, result} = DBConnection.execute(conn, query, []) + assert result.columns == ["id", "stuff"] + assert result.rows == [[1, "hello"]] + end end