Skip to content

Commit

Permalink
accounts: add SQL store implementation
Browse files Browse the repository at this point in the history
In this commit, we add the SQLStore type which implements the
accounts.Store interface. To demonstrate that it works as expected, we
also plug this implementation into all the account unit tests to show
that they pass against the sqlite and postgres backends.

One can use `make unit pkg=accounts tags=test_db_postgres` or
`make unit pkg=accounts tags=test_db_sqlite` to test locally.

Note that 2 small timestamp related changes are made to the unit tests.
This is to compensate for timestamp precision in postgres.
  • Loading branch information
ellemouton committed Jan 29, 2025
1 parent ac160bb commit 1dc119a
Show file tree
Hide file tree
Showing 6 changed files with 794 additions and 3 deletions.
27 changes: 27 additions & 0 deletions accounts/interface.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package accounts

import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -55,6 +57,31 @@ func ParseAccountID(idStr string) (*AccountID, error) {
return &id, nil
}

// ToInt64 converts an AccountID to its int64 representation.
func (a AccountID) ToInt64() (int64, error) {
var value int64
buf := bytes.NewReader(a[:])
if err := binary.Read(buf, byteOrder, &value); err != nil {
return 0, err
}

return value, nil
}

// AccountIDFromInt64 converts an int64 to an AccountID.
func AccountIDFromInt64(value int64) (AccountID, error) {
var (
a = AccountID{}
buf = new(bytes.Buffer)
)
if err := binary.Write(buf, binary.BigEndian, value); err != nil {
return a, err
}
copy(a[:], buf.Bytes())

return a, nil
}

// String returns the string representation of the AccountID.
func (a AccountID) String() string {
return hex.EncodeToString(a[:])
Expand Down
Loading

0 comments on commit 1dc119a

Please sign in to comment.