Skip to content

fix(v2.2): metadata override #732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -72,6 +72,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 @@ -78,6 +78,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
Loading