Skip to content

Commit

Permalink
refactor(i): Remove datastore.Txn from private db methods (#2518)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #2517 

## Description

This PR removes the `datastore.Txn` parameter from private db methods.

## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

`make test`

Specify the platform(s) on which this was tested:
- MacOS
  • Loading branch information
nasdf authored Apr 15, 2024
1 parent ef228b8 commit 7cb065b
Show file tree
Hide file tree
Showing 18 changed files with 240 additions and 218 deletions.
13 changes: 6 additions & 7 deletions db/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
acpIdentity "github.com/sourcenetwork/defradb/acp/identity"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/datastore"
)

func (db *db) basicImport(ctx context.Context, txn datastore.Txn, filepath string) (err error) {
func (db *db) basicImport(ctx context.Context, filepath string) (err error) {
f, err := os.Open(filepath)
if err != nil {
return NewErrOpenFile(err, filepath)
Expand All @@ -50,7 +49,7 @@ func (db *db) basicImport(ctx context.Context, txn datastore.Txn, filepath strin
return err
}
colName := t.(string)
col, err := db.getCollectionByName(ctx, txn, colName)
col, err := db.getCollectionByName(ctx, colName)
if err != nil {
return NewErrFailedToGetCollection(colName, err)
}
Expand Down Expand Up @@ -119,19 +118,19 @@ func (db *db) basicImport(ctx context.Context, txn datastore.Txn, filepath strin
return nil
}

func (db *db) basicExport(ctx context.Context, txn datastore.Txn, config *client.BackupConfig) (err error) {
func (db *db) basicExport(ctx context.Context, config *client.BackupConfig) (err error) {
// old key -> new Key
keyChangeCache := map[string]string{}

cols := []client.Collection{}
if len(config.Collections) == 0 {
cols, err = db.getCollections(ctx, txn, client.CollectionFetchOptions{})
cols, err = db.getCollections(ctx, client.CollectionFetchOptions{})
if err != nil {
return NewErrFailedToGetAllCollections(err)
}
} else {
for _, colName := range config.Collections {
col, err := db.getCollectionByName(ctx, txn, colName)
col, err := db.getCollectionByName(ctx, colName)
if err != nil {
return NewErrFailedToGetCollection(colName, err)
}
Expand Down Expand Up @@ -233,7 +232,7 @@ func (db *db) basicExport(ctx context.Context, txn datastore.Txn, config *client
refFieldName = field.Name + request.RelatedObjectID
}
} else {
foreignCol, err := db.getCollectionByName(ctx, txn, field.Kind.Underlying())
foreignCol, err := db.getCollectionByName(ctx, field.Kind.Underlying())
if err != nil {
return NewErrFailedToGetCollection(field.Kind.Underlying(), err)
}
Expand Down
40 changes: 28 additions & 12 deletions db/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ func TestBasicExport_WithNormalFormatting_NoError(t *testing.T) {

txn, err := db.NewTxn(ctx, true)
require.NoError(t, err)

ctx = SetContextTxn(ctx, txn)
defer txn.Discard(ctx)

filepath := t.TempDir() + "/test.json"
err = db.basicExport(ctx, txn, &client.BackupConfig{Filepath: filepath})
err = db.basicExport(ctx, &client.BackupConfig{Filepath: filepath})
require.NoError(t, err)

b, err := os.ReadFile(filepath)
Expand Down Expand Up @@ -126,10 +128,12 @@ func TestBasicExport_WithPrettyFormatting_NoError(t *testing.T) {

txn, err := db.NewTxn(ctx, true)
require.NoError(t, err)

ctx = SetContextTxn(ctx, txn)
defer txn.Discard(ctx)

filepath := t.TempDir() + "/test.json"
err = db.basicExport(ctx, txn, &client.BackupConfig{Filepath: filepath, Pretty: true})
err = db.basicExport(ctx, &client.BackupConfig{Filepath: filepath, Pretty: true})
require.NoError(t, err)

b, err := os.ReadFile(filepath)
Expand Down Expand Up @@ -188,10 +192,12 @@ func TestBasicExport_WithSingleCollection_NoError(t *testing.T) {

txn, err := db.NewTxn(ctx, true)
require.NoError(t, err)

ctx = SetContextTxn(ctx, txn)
defer txn.Discard(ctx)

filepath := t.TempDir() + "/test.json"
err = db.basicExport(ctx, txn, &client.BackupConfig{Filepath: filepath, Collections: []string{"Address"}})
err = db.basicExport(ctx, &client.BackupConfig{Filepath: filepath, Collections: []string{"Address"}})
require.NoError(t, err)

b, err := os.ReadFile(filepath)
Expand Down Expand Up @@ -262,10 +268,12 @@ func TestBasicExport_WithMultipleCollectionsAndUpdate_NoError(t *testing.T) {

txn, err := db.NewTxn(ctx, true)
require.NoError(t, err)

ctx = SetContextTxn(ctx, txn)
defer txn.Discard(ctx)

filepath := t.TempDir() + "/test.json"
err = db.basicExport(ctx, txn, &client.BackupConfig{Filepath: filepath})
err = db.basicExport(ctx, &client.BackupConfig{Filepath: filepath})
require.NoError(t, err)

b, err := os.ReadFile(filepath)
Expand Down Expand Up @@ -324,6 +332,8 @@ func TestBasicExport_EnsureFileOverwrite_NoError(t *testing.T) {

txn, err := db.NewTxn(ctx, true)
require.NoError(t, err)

ctx = SetContextTxn(ctx, txn)
defer txn.Discard(ctx)

filepath := t.TempDir() + "/test.json"
Expand All @@ -335,7 +345,7 @@ func TestBasicExport_EnsureFileOverwrite_NoError(t *testing.T) {
)
require.NoError(t, err)

err = db.basicExport(ctx, txn, &client.BackupConfig{Filepath: filepath, Collections: []string{"Address"}})
err = db.basicExport(ctx, &client.BackupConfig{Filepath: filepath, Collections: []string{"Address"}})
require.NoError(t, err)

b, err := os.ReadFile(filepath)
Expand Down Expand Up @@ -370,6 +380,7 @@ func TestBasicImport_WithMultipleCollectionsAndObjects_NoError(t *testing.T) {

txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)
ctx = SetContextTxn(ctx, txn)

filepath := t.TempDir() + "/test.json"

Expand All @@ -380,23 +391,24 @@ func TestBasicImport_WithMultipleCollectionsAndObjects_NoError(t *testing.T) {
)
require.NoError(t, err)

err = db.basicImport(ctx, txn, filepath)
err = db.basicImport(ctx, filepath)
require.NoError(t, err)
err = txn.Commit(ctx)
require.NoError(t, err)

txn, err = db.NewTxn(ctx, true)
require.NoError(t, err)
ctx = SetContextTxn(ctx, txn)

col1, err := db.getCollectionByName(ctx, txn, "Address")
col1, err := db.getCollectionByName(ctx, "Address")
require.NoError(t, err)

key1, err := client.NewDocIDFromString("bae-8096f2c1-ea4c-5226-8ba5-17fc4b68ac1f")
require.NoError(t, err)
_, err = col1.Get(ctx, acpIdentity.NoIdentity, key1, false)
require.NoError(t, err)

col2, err := db.getCollectionByName(ctx, txn, "User")
col2, err := db.getCollectionByName(ctx, "User")
require.NoError(t, err)

key2, err := client.NewDocIDFromString("bae-b94880d1-e6d2-542f-b9e0-5a369fafd0df")
Expand Down Expand Up @@ -429,6 +441,7 @@ func TestBasicImport_WithJSONArray_ReturnError(t *testing.T) {

txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)
ctx = SetContextTxn(ctx, txn)

filepath := t.TempDir() + "/test.json"

Expand All @@ -439,7 +452,7 @@ func TestBasicImport_WithJSONArray_ReturnError(t *testing.T) {
)
require.NoError(t, err)

err = db.basicImport(ctx, txn, filepath)
err = db.basicImport(ctx, filepath)
require.ErrorIs(t, err, ErrExpectedJSONObject)
err = txn.Commit(ctx)
require.NoError(t, err)
Expand All @@ -464,6 +477,7 @@ func TestBasicImport_WithObjectCollection_ReturnError(t *testing.T) {

txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)
ctx = SetContextTxn(ctx, txn)

filepath := t.TempDir() + "/test.json"

Expand All @@ -474,7 +488,7 @@ func TestBasicImport_WithObjectCollection_ReturnError(t *testing.T) {
)
require.NoError(t, err)

err = db.basicImport(ctx, txn, filepath)
err = db.basicImport(ctx, filepath)
require.ErrorIs(t, err, ErrExpectedJSONArray)
err = txn.Commit(ctx)
require.NoError(t, err)
Expand All @@ -499,6 +513,7 @@ func TestBasicImport_WithInvalidFilepath_ReturnError(t *testing.T) {

txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)
ctx = SetContextTxn(ctx, txn)

filepath := t.TempDir() + "/test.json"

Expand All @@ -510,7 +525,7 @@ func TestBasicImport_WithInvalidFilepath_ReturnError(t *testing.T) {
require.NoError(t, err)

wrongFilepath := t.TempDir() + "/some/test.json"
err = db.basicImport(ctx, txn, wrongFilepath)
err = db.basicImport(ctx, wrongFilepath)
require.ErrorIs(t, err, os.ErrNotExist)
err = txn.Commit(ctx)
require.NoError(t, err)
Expand All @@ -535,6 +550,7 @@ func TestBasicImport_WithInvalidCollection_ReturnError(t *testing.T) {

txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)
ctx = SetContextTxn(ctx, txn)

filepath := t.TempDir() + "/test.json"

Expand All @@ -545,7 +561,7 @@ func TestBasicImport_WithInvalidCollection_ReturnError(t *testing.T) {
)
require.NoError(t, err)

err = db.basicImport(ctx, txn, filepath)
err = db.basicImport(ctx, filepath)
require.ErrorIs(t, err, ErrFailedToGetCollection)
err = txn.Commit(ctx)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 7cb065b

Please sign in to comment.