Skip to content

Commit

Permalink
✨ Add read only database to namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
julien040 committed Jun 15, 2024
1 parent 0c05f0a commit abb5757
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion namespace/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand Down

0 comments on commit abb5757

Please sign in to comment.