Skip to content

Commit

Permalink
fix: metadata override (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored Mar 5, 2025
1 parent f76e49b commit 523ac1c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
5 changes: 2 additions & 3 deletions internal/storage/ledger/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ func (store *Store) UpdateAccountsMetadata(ctx context.Context, m map[string]met
ret, err := store.db.NewInsert().
Model(&accounts).
ModelTableExpr(store.GetPrefixedRelationName("accounts")).
On("CONFLICT (ledger, address) DO UPDATE").
Set("metadata = excluded.metadata || accounts.metadata").
On("conflict (ledger, address) do update").
Set("metadata = accounts.metadata || excluded.metadata").
Set("updated_at = excluded.updated_at").
Where("not accounts.metadata @> excluded.metadata").
Exec(ctx)

if err != nil {
return postgres.ResolveError(err)
}
Expand Down
37 changes: 36 additions & 1 deletion test/e2e/api_accounts_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
. "github.com/onsi/gomega"
)

var _ = Context("Ledger accounts list API tests", func() {
var _ = Context("Ledger accounts metadata API tests", func() {
var (
db = UseTemplatedDatabase()
ctx = logging.TestingContext()
Expand Down Expand Up @@ -74,6 +74,41 @@ var _ = Context("Ledger accounts list API tests", func() {
Metadata: metadata,
}))
})
Context("Then updating the metadata", func() {
var (
newMetadata = map[string]string{
"clientType": "silver",
}
)
JustBeforeEach(func() {
err := AddMetadataToAccount(
ctx,
testServer.GetValue(),
operations.V2AddMetadataToAccountRequest{
RequestBody: newMetadata,
Address: "foo",
Ledger: "default",
},
)
Expect(err).ToNot(HaveOccurred())
})
It("should update the metadata", func() {
response, err := GetAccount(
ctx,
testServer.GetValue(),
operations.V2GetAccountRequest{
Address: "foo",
Ledger: "default",
},
)
Expect(err).ToNot(HaveOccurred())

Expect(*response).Should(Equal(components.V2Account{
Address: "foo",
Metadata: newMetadata,
}))
})
})
It("should trigger a new event", func() {
Eventually(events).Should(Receive(Event(ledgerevents.EventTypeSavedMetadata)))
})
Expand Down
37 changes: 37 additions & 0 deletions test/e2e/api_transactions_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,43 @@ var _ = Context("Ledger transactions create API tests", func() {
// Create a transaction
rsp, err = CreateTransaction(ctx, testServer.GetValue(), req)
})
Context("overriding an account metadata", func() {
BeforeEach(func() {
err := AddMetadataToAccount(ctx, testServer.GetValue(), operations.V2AddMetadataToAccountRequest{
Address: "alice",
Ledger: "default",
RequestBody: map[string]string{
"clientType": "gold",
},
})
Expect(err).ToNot(HaveOccurred())

req.V2PostTransaction.Script = &components.V2PostTransactionScript{
Plain: `
send [USD 100] (
source = @world
destination = @alice
)
set_account_meta(@alice, "clientType", "silver")
`,
}
})
It("should override account metadata", func() {
Expect(err).To(BeNil())

account, err := GetAccount(ctx, testServer.GetValue(), operations.V2GetAccountRequest{
Address: "alice",
Ledger: "default",
})
Expect(err).ToNot(HaveOccurred())
Expect(*account).Should(Equal(components.V2Account{
Address: "alice",
Metadata: map[string]string{
"clientType": "silver",
},
}))
})
})
Context("with valid data", func() {
BeforeEach(func() {
req = operations.V2CreateTransactionRequest{
Expand Down

0 comments on commit 523ac1c

Please sign in to comment.