Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/exqlite/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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, [])

Expand Down Expand Up @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions test/exqlite/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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