Skip to content

Commit abb5757

Browse files
committed
✨ Add read only database to namespace
1 parent 0c05f0a commit abb5757

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

namespace/namespace.go

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ type NamespaceConfig struct {
5555

5656
// The hclog logger to use from hashicorp/go-hclog
5757
Logger hclog.Logger
58+
59+
// If ReadOnly is set to true, the database will be opened in read-only mode
60+
ReadOnly bool
5861
}
5962

6063
type Namespace struct {
@@ -112,6 +115,8 @@ func (n *Namespace) Init(config NamespaceConfig) error {
112115
// Open the database in memory if needed
113116
if config.InMemory {
114117
connectionStringBuilder.WriteString("&mode=memory")
118+
} else if config.ReadOnly {
119+
connectionStringBuilder.WriteString("&mode=ro")
115120
}
116121

117122
// Set the page cache size

namespace/namespace_test.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,29 @@ func TestNamespace(t *testing.T) {
7070
namespace.connectionString, "The connection string should be correct")
7171
})
7272

73+
t.Run("The connection string is set correctly for a read-only file DB", func(t *testing.T) {
74+
var err error
75+
namespace, err = NewNamespace(NamespaceConfig{
76+
ReadOnly: true,
77+
})
78+
require.NoError(t, err, "The namespace should be initialized")
79+
require.Equal(t, "file:anyquery.db?cache=shared&mode=ro&_cache_size=-50000&_journal_mode=WAL&_synchronous=NORMAL&_foreign_keys=OFF",
80+
namespace.connectionString, "The connection string should be correct")
81+
})
82+
83+
t.Run("The read only flag is ignored for in-memory DB", func(t *testing.T) {
84+
var err error
85+
namespace, err = NewNamespace(NamespaceConfig{
86+
ReadOnly: true,
87+
InMemory: true,
88+
})
89+
require.NoError(t, err, "The namespace should be initialized")
90+
require.Equal(t, "file:anyquery.db?cache=shared&mode=memory&_cache_size=-50000&_journal_mode=WAL&_synchronous=NORMAL&_foreign_keys=OFF",
91+
namespace.connectionString, "The connection string should be correct")
92+
})
93+
7394
t.Run("The GetConnectionString method works", func(t *testing.T) {
74-
require.Equal(t, "file:mytest.db?cache=shared&_foreign_keys=OFF",
95+
require.Equal(t, "file:anyquery.db?cache=shared&mode=memory&_cache_size=-50000&_journal_mode=WAL&_synchronous=NORMAL&_foreign_keys=OFF",
7596
namespace.GetConnectionString(), "The connection string should be correct")
7697
require.Equal(t, namespace.connectionString, namespace.GetConnectionString(), "The connection string should be correct")
7798
})

0 commit comments

Comments
 (0)