Skip to content

Commit

Permalink
feature: support in-memory database
Browse files Browse the repository at this point in the history
In some cases, static user data is sufficient and changes to the users
do not need to be persisted. This change introduces a special file path
`:memory:` (similar to sqlite3) that allows the user to specify that the
database is in-memory only. Changes will not be persisted on the file
system.
  • Loading branch information
oncilla committed Oct 16, 2024
1 parent 12d1bf7 commit f605230
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
17 changes: 12 additions & 5 deletions pkg/identity/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type Database struct {
refID map[string]*User
refAPIKey map[string]*User
path string
inMemory bool
}

// NewDatabase return an instance of Database.
Expand All @@ -127,14 +128,17 @@ func NewDatabase(fp string) (*Database, error) {
refID: make(map[string]*User),
refEmailAddress: make(map[string]*User),
refAPIKey: make(map[string]*User),
inMemory: fp == ":memory:",
}
fileInfo, err := os.Stat(fp)
if err != nil {
if !os.IsNotExist(err) {
return nil, errors.ErrNewDatabase.WithArgs(fp, err)
}
if err := os.MkdirAll(filepath.Dir(fp), 0700); err != nil {
return nil, errors.ErrNewDatabase.WithArgs(fp, err)
if !db.inMemory {
if !os.IsNotExist(err) {
return nil, errors.ErrNewDatabase.WithArgs(fp, err)
}
if err := os.MkdirAll(filepath.Dir(fp), 0700); err != nil {
return nil, errors.ErrNewDatabase.WithArgs(fp, err)
}
}
db.Version = app.Version
db.enforceDefaultPolicy()
Expand Down Expand Up @@ -485,6 +489,9 @@ func (db *Database) commit() error {
if err != nil {
return errors.ErrDatabaseCommit.WithArgs(db.path, err)
}
if db.inMemory {
return nil
}

if err := os.WriteFile(db.path, []byte(data), 0600); err != nil {
return errors.ErrDatabaseCommit.WithArgs(db.path, err)
Expand Down
27 changes: 23 additions & 4 deletions pkg/identity/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ package identity

import (
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/greenpau/go-authcrunch/internal/tests"
"github.com/greenpau/go-authcrunch/pkg/errors"
"github.com/greenpau/go-authcrunch/pkg/requests"
"path"
"path/filepath"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/greenpau/go-authcrunch/internal/tests"
"github.com/greenpau/go-authcrunch/pkg/errors"
"github.com/greenpau/go-authcrunch/pkg/requests"
)

var (
Expand Down Expand Up @@ -117,6 +118,24 @@ func TestNewDatabase(t *testing.T) {
"user_count": 0,
},
},
{
name: "test create new in-memory database",
path: ":memory:",
req: &requests.Request{
User: requests.User{
Username: "jsmith",
Password: passwd,
Email: "[email protected]",
FullName: "Smith, John",
Roles: []string{"viewer", "editor", "admin"},
},
},
backup: filepath.Join(tmpDir, "user_db_backup.json"),
want: map[string]interface{}{
"path": ":memory:",
"user_count": 0,
},
},
{
name: "test new database is directory",
path: tmpDir,
Expand Down

0 comments on commit f605230

Please sign in to comment.