From abb57575e9c59c9ebc02a6763238cdf6c87180c7 Mon Sep 17 00:00:00 2001 From: julien040 <48369040+julien040@users.noreply.github.com> Date: Sat, 15 Jun 2024 14:02:52 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20read=20only=20database=20to?= =?UTF-8?q?=20namespace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- namespace/namespace.go | 5 +++++ namespace/namespace_test.go | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/namespace/namespace.go b/namespace/namespace.go index c90ee8f..a2a08ab 100644 --- a/namespace/namespace.go +++ b/namespace/namespace.go @@ -55,6 +55,9 @@ type NamespaceConfig struct { // The hclog logger to use from hashicorp/go-hclog Logger hclog.Logger + + // If ReadOnly is set to true, the database will be opened in read-only mode + ReadOnly bool } type Namespace struct { @@ -112,6 +115,8 @@ func (n *Namespace) Init(config NamespaceConfig) error { // Open the database in memory if needed if config.InMemory { connectionStringBuilder.WriteString("&mode=memory") + } else if config.ReadOnly { + connectionStringBuilder.WriteString("&mode=ro") } // Set the page cache size diff --git a/namespace/namespace_test.go b/namespace/namespace_test.go index 0202e86..5a3e72d 100644 --- a/namespace/namespace_test.go +++ b/namespace/namespace_test.go @@ -70,8 +70,29 @@ func TestNamespace(t *testing.T) { namespace.connectionString, "The connection string should be correct") }) + t.Run("The connection string is set correctly for a read-only file DB", func(t *testing.T) { + var err error + namespace, err = NewNamespace(NamespaceConfig{ + ReadOnly: true, + }) + require.NoError(t, err, "The namespace should be initialized") + require.Equal(t, "file:anyquery.db?cache=shared&mode=ro&_cache_size=-50000&_journal_mode=WAL&_synchronous=NORMAL&_foreign_keys=OFF", + namespace.connectionString, "The connection string should be correct") + }) + + t.Run("The read only flag is ignored for in-memory DB", func(t *testing.T) { + var err error + namespace, err = NewNamespace(NamespaceConfig{ + ReadOnly: true, + InMemory: true, + }) + require.NoError(t, err, "The namespace should be initialized") + require.Equal(t, "file:anyquery.db?cache=shared&mode=memory&_cache_size=-50000&_journal_mode=WAL&_synchronous=NORMAL&_foreign_keys=OFF", + namespace.connectionString, "The connection string should be correct") + }) + t.Run("The GetConnectionString method works", func(t *testing.T) { - require.Equal(t, "file:mytest.db?cache=shared&_foreign_keys=OFF", + require.Equal(t, "file:anyquery.db?cache=shared&mode=memory&_cache_size=-50000&_journal_mode=WAL&_synchronous=NORMAL&_foreign_keys=OFF", namespace.GetConnectionString(), "The connection string should be correct") require.Equal(t, namespace.connectionString, namespace.GetConnectionString(), "The connection string should be correct") })