Passed
Push — master ( 529ae6...944870 )
by Tolga
06:00 queued 02:43
created

internal/storage/memory/schema_writer.go   A

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 34
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A memory.init 0 2 1
A memory.NewSchemaWriter 0 3 1
A memory.*SchemaWriter.WriteSchema 0 22 3
1
package memory
2
3
import (
4
	"context"
5
	"errors"
6
	"sync"
7
8
	"github.com/Permify/permify/internal/storage"
9
	"github.com/Permify/permify/internal/storage/memory/constants"
10
	db "github.com/Permify/permify/pkg/database/memory"
11
	base "github.com/Permify/permify/pkg/pb/base/v1"
12
)
13
14
var (
15
	headVersion map[string]string
16
	mu          sync.Mutex
17
)
18
19
func init() {
20
	headVersion = make(map[string]string)
21
}
22
23
// SchemaWriter - Structure for Schema Writer
24
type SchemaWriter struct {
25
	database *db.Memory
26
}
27
28
// NewSchemaWriter creates a new SchemaWriter
29
func NewSchemaWriter(database *db.Memory) *SchemaWriter {
30
	return &SchemaWriter{
31
		database: database,
32
	}
33
}
34
35
// WriteSchema - Write Schema to repository
36
func (w *SchemaWriter) WriteSchema(_ context.Context, definitions []storage.SchemaDefinition) error {
37
	var err error
38
	txn := w.database.DB.Txn(true)
39
	defer txn.Abort()
40
41
	var tenantID string
42
	var version string
43
44
	for _, definition := range definitions {
45
		if err = txn.Insert(constants.SchemaDefinitionsTable, definition); err != nil {
46
			return errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
47
		}
48
		tenantID = definition.TenantID
49
		version = definition.Version
50
	}
51
	txn.Commit()
52
53
	mu.Lock()
54
	headVersion[tenantID] = version
55
	mu.Unlock()
56
57
	return nil
58
}
59