Skip to content

Commit 285af4d

Browse files
author
Matt Pope
committed
feat: Add serialized as an option to connect.
1 parent cd25745 commit 285af4d

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/exqlite/connection.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ defmodule Exqlite.Connection do
142142
* `:soft_heap_limit` - The size limit in bytes for the heap limit.
143143
* `:hard_heap_limit` - The size limit in bytes for the heap.
144144
* `:custom_pragmas` - A list of custom pragmas to set on the connection, for example to configure extensions.
145+
* `:serialized` - A SQLite database which was previously serialized, to load into the database after connection.
145146
* `:load_extensions` - A list of paths identifying extensions to load. Defaults to `[]`.
146147
The provided list will be merged with the global extensions list, set on `:exqlite, :load_extensions`.
147148
Be aware that the path should handle pointing to a library compiled for the current architecture.
@@ -510,6 +511,13 @@ defmodule Exqlite.Connection do
510511
set_pragma(db, "busy_timeout", Pragma.busy_timeout(options))
511512
end
512513

514+
defp deserialize(db, options) do
515+
case Keyword.get(options, :serialized, nil) do
516+
nil -> :ok
517+
serialized -> Sqlite3.deserialize(db, serialized)
518+
end
519+
end
520+
513521
defp load_extensions(db, options) do
514522
global_extensions = Application.get_env(:exqlite, :load_extensions, [])
515523

@@ -555,7 +563,8 @@ defmodule Exqlite.Connection do
555563
:ok <- set_journal_size_limit(db, options),
556564
:ok <- set_soft_heap_limit(db, options),
557565
:ok <- set_hard_heap_limit(db, options),
558-
:ok <- load_extensions(db, options) do
566+
:ok <- load_extensions(db, options),
567+
:ok <- deserialize(db, options) do
559568
state = %__MODULE__{
560569
db: db,
561570
default_transaction_mode:

test/exqlite/integration_test.exs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,35 @@ defmodule Exqlite.IntegrationTest do
243243

244244
File.rm(path)
245245
end
246+
247+
test "can load a serialized database at startup" do
248+
{:ok, path} = Temp.path()
249+
{:ok, conn} = Sqlite3.open(path)
250+
251+
:ok =
252+
Sqlite3.execute(conn, "create table test(id integer primary key, stuff text)")
253+
254+
assert :ok =
255+
Sqlite3.execute(conn, "insert into test(id, stuff) values (1, 'hello')")
256+
257+
assert {:ok, binary} = Sqlite3.serialize(conn, "main")
258+
assert is_binary(binary)
259+
Sqlite3.close(conn)
260+
File.rm(path)
261+
262+
{:ok, conn} =
263+
DBConnection.start_link(Connection,
264+
idle_interval: 5_000,
265+
database: :memory,
266+
journal_mode: :wal,
267+
cache_size: -64_000,
268+
temp_store: :memory,
269+
serialized: binary
270+
)
271+
272+
query = %Query{statement: "select id, stuff from test"}
273+
{:ok, _, result} = DBConnection.execute(conn, query, [])
274+
assert result.columns == ["id", "stuff"]
275+
assert result.rows == [[1, "hello"]]
276+
end
246277
end

0 commit comments

Comments
 (0)