Skip to content
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

Support context based transaction for create/update only #48

Merged
merged 1 commit into from
Sep 19, 2023
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
53 changes: 39 additions & 14 deletions build/install_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,51 @@ set -eux

source $(dirname "$0")/env.sh

FORCE=false
GODLV_VERSION=${GODLV_VERSION:=1.21.0}
if go version | grep 1.2; then
GOLNT_VERSION=${GOLNT_VERSION:=v1.52.2}
GOLNT_VERSION=${GOLNT_VERSION:=1.52.2}
else
GOLNT_VERSION=${GOLNT_VERSION:=v1.50.1}
GOLNT_VERSION=${GOLNT_VERSION:=1.50.1}
fi
GOMRK_VERSION=v1.1.0
SHFMT_VERSION=v3.6.0
TYPOS_VERSION=1.13.6
GOMRK_VERSION=${GOMRK_VERSION:=1.1.0}
SHFMT_VERSION=${SHFMT_VERSION:=3.6.0}
GOIMP_VERSION=${GOIMP_VERSION:=0.5.0}
GOIMPORTS_REVISER_VERSION=${GOIMPORTS_REVISER_VERSION:=3.4.2}
TYPOS_VERSION=${TYPOS_VERSION:=1.13.6}
ADDLICENSE_VERSION=${ADDLICENSE_VERSION:=1.1.1}
CODEOWNERS_VALIDATOR_VERSION=${CODEOWNERS_VALIDATOR_VERSION:=0.7.4}
YAMLFMT_VERSION=${YAMLFMT_VERSION:=0.9.0}

go-licenser -version || go install github.com/elastic/go-licenser@latest
$(go env GOPATH)/bin/shfmt -version | grep ${SHFMT_VERSION} || go install mvdan.cc/sh/v3/cmd/shfmt@${SHFMT_VERSION}
$(go env GOPATH)/bin/gomarkdoc --version | grep ${GOMRK_VERSION} || go install github.com/princjef/gomarkdoc/cmd/gomarkdoc@${GOMRK_VERSION}
$(go env GOPATH)/bin/golangci-lint version | grep ${GOLNT_VERSION} || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLNT_VERSION}
go install -v github.com/incu6us/goimports-reviser/v3@latest
# With version check
go_install() {
([ ${FORCE} = false ] && $1 | grep $2) || go install $3@v$2
}

if [[ "${OSTYPE}" == "linux"* ]]; then
# Without version check
go_install_any() {
([ ${FORCE} = false ] && $1 >/dev/null 2>&1) || go install $2@$3
}

go version | grep '1.19\|1.20\|1.21' || (
echo "Install supported version (>=1.19) of golang to use saas-ci"
exit 1
)
go_install "dlv version" ${GODLV_VERSION} github.com/go-delve/delve/cmd/dlv
go_install "golangci-lint version" ${GOLNT_VERSION} github.com/golangci/golangci-lint/cmd/golangci-lint
go_install "gomarkdoc --version" ${GOMRK_VERSION} github.com/princjef/gomarkdoc/cmd/gomarkdoc
go_install "shfmt -version" ${SHFMT_VERSION} mvdan.cc/sh/v3/cmd/shfmt
go_install_any "goimports -h" golang.org/x/tools/cmd/goimports v${GOIMP_VERSION}
go_install_any "goimports-reviser -h" "github.com/incu6us/goimports-reviser/v3" v${GOIMPORTS_REVISER_VERSION}
go_install_any "golicenser -version" github.com/elastic/go-licenser latest
go_install_any "addlicense -h" github.com/google/addlicense v${ADDLICENSE_VERSION}
go_install_any "codeowners-validator -v" github.com/mszostok/codeowners-validator v${CODEOWNERS_VALIDATOR_VERSION}
go_install_any "yamlfmt -h" github.com/google/yamlfmt/cmd/yamlfmt v${YAMLFMT_VERSION}

OS_TYPE=$(uname -s)
if [ "${OS_TYPE}" = "Linux" ]; then
/tmp/typos --version | grep ${TYPOS_VERSION} || wget -qO- https://github.com/crate-ci/typos/releases/download/v${TYPOS_VERSION}/typos-v${TYPOS_VERSION}-x86_64-unknown-linux-musl.tar.gz | tar -zxf - -C /tmp/ ./typos
sudo snap install mdl
elif [[ "${OSTYPE}" == "darwin"* ]]; then
elif [ "${OS_TYPE}" = "Darwin" ]; then
/tmp/typos --version | grep ${TYPOS_VERSION} || wget -qO- https://github.com/crate-ci/typos/releases/download/v${TYPOS_VERSION}/typos-v${TYPOS_VERSION}-x86_64-apple-darwin.tar.gz | tar -zxf - -C /tmp/ ./typos
fi

pip3 install addlicense mdv yamllint
86 changes: 79 additions & 7 deletions docs/DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ import "github.com/vmware-labs/multi-tenant-persistence-for-saas/pkg/authorizer"
- [type SimpleInstancer](<#SimpleInstancer>)
- [func \(s SimpleInstancer\) GetInstanceId\(ctx context.Context\) \(string, error\)](<#SimpleInstancer.GetInstanceId>)
- [func \(s SimpleInstancer\) WithInstanceId\(ctx context.Context, instanceId string\) context.Context](<#SimpleInstancer.WithInstanceId>)
- [type SimpleTransactionFetcher](<#SimpleTransactionFetcher>)
- [func \(s SimpleTransactionFetcher\) GetTransactionCtx\(ctx context.Context\) \*gorm.DB](<#SimpleTransactionFetcher.GetTransactionCtx>)
- [func \(s SimpleTransactionFetcher\) IsTransactionCtx\(ctx context.Context\) bool](<#SimpleTransactionFetcher.IsTransactionCtx>)
- [func \(s SimpleTransactionFetcher\) WithTransactionCtx\(ctx context.Context, tx \*gorm.DB\) context.Context](<#SimpleTransactionFetcher.WithTransactionCtx>)
- [type Tenancer](<#Tenancer>)
- [type TransactionContextKey](<#TransactionContextKey>)
- [type TransactionFetcher](<#TransactionFetcher>)


## Constants
Expand Down Expand Up @@ -55,6 +61,14 @@ const (
)
```

<a name="TransactionCtx"></a>

```go
const (
TransactionCtx = TransactionContextKey("DB_TRANSACTION")
)
```

<a name="Authorizer"></a>
## type [Authorizer](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/authorizer/authorizer.go#L30-L36>)

Expand Down Expand Up @@ -172,6 +186,42 @@ func (s SimpleInstancer) WithInstanceId(ctx context.Context, instanceId string)



<a name="SimpleTransactionFetcher"></a>
## type [SimpleTransactionFetcher](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/authorizer/transaction.go#L21>)



```go
type SimpleTransactionFetcher struct{}
```

<a name="SimpleTransactionFetcher.GetTransactionCtx"></a>
### func \(SimpleTransactionFetcher\) [GetTransactionCtx](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/authorizer/transaction.go#L23>)

```go
func (s SimpleTransactionFetcher) GetTransactionCtx(ctx context.Context) *gorm.DB
```



<a name="SimpleTransactionFetcher.IsTransactionCtx"></a>
### func \(SimpleTransactionFetcher\) [IsTransactionCtx](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/authorizer/transaction.go#L36>)

```go
func (s SimpleTransactionFetcher) IsTransactionCtx(ctx context.Context) bool
```



<a name="SimpleTransactionFetcher.WithTransactionCtx"></a>
### func \(SimpleTransactionFetcher\) [WithTransactionCtx](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/authorizer/transaction.go#L32>)

```go
func (s SimpleTransactionFetcher) WithTransactionCtx(ctx context.Context, tx *gorm.DB) context.Context
```



<a name="Tenancer"></a>
## type [Tenancer](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/authorizer/authorizer.go#L38-L40>)

Expand All @@ -183,6 +233,28 @@ type Tenancer interface {
}
```

<a name="TransactionContextKey"></a>
## type [TransactionContextKey](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/authorizer/transaction.go#L9>)



```go
type TransactionContextKey string
```

<a name="TransactionFetcher"></a>
## type [TransactionFetcher](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/authorizer/transaction.go#L15-L19>)



```go
type TransactionFetcher interface {
IsTransactionCtx(ctx context.Context) bool
GetTransactionCtx(ctx context.Context) *gorm.DB
WithTransactionCtx(ctx context.Context, tx *gorm.DB) context.Context
}
```

# datastore

```go
Expand Down Expand Up @@ -226,8 +298,8 @@ DataStore interface exposes basic methods like Find/FindAll/Upsert/Delete. For r
- [type DBConfig](<#DBConfig>)
- [func ConfigFromEnv\(dbName string\) DBConfig](<#ConfigFromEnv>)
- [type DataStore](<#DataStore>)
- [func FromConfig\(l \*logrus.Entry, authorizer authorizer.Authorizer, instancer authorizer.Instancer, cfg DBConfig\) \(d DataStore, err error\)](<#FromConfig>)
- [func FromEnv\(l \*logrus.Entry, authorizer authorizer.Authorizer, instancer authorizer.Instancer\) \(d DataStore, err error\)](<#FromEnv>)
- [func FromConfig\(l \*logrus.Entry, a authorizer.Authorizer, instancer authorizer.Instancer, cfg DBConfig\) \(d DataStore, err error\)](<#FromConfig>)
- [func FromEnv\(l \*logrus.Entry, a authorizer.Authorizer, instancer authorizer.Instancer\) \(d DataStore, err error\)](<#FromEnv>)
- [type Helper](<#Helper>)
- [type Pagination](<#Pagination>)
- [func DefaultPagination\(\) \*Pagination](<#DefaultPagination>)
Expand Down Expand Up @@ -318,7 +390,7 @@ var TRACE = func(format string, v ...any) {
```

<a name="DBCreate"></a>
## func [DBCreate](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L219>)
## func [DBCreate](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L220>)

```go
func DBCreate(cfg DBConfig) error
Expand All @@ -327,7 +399,7 @@ func DBCreate(cfg DBConfig) error
Create a Postgres DB using the provided config if it doesn't exist.

<a name="DBExists"></a>
## func [DBExists](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L241>)
## func [DBExists](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L242>)

```go
func DBExists(cfg DBConfig) bool
Expand Down Expand Up @@ -790,7 +862,7 @@ err != nil - true
### func [FromConfig](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L104>)

```go
func FromConfig(l *logrus.Entry, authorizer authorizer.Authorizer, instancer authorizer.Instancer, cfg DBConfig) (d DataStore, err error)
func FromConfig(l *logrus.Entry, a authorizer.Authorizer, instancer authorizer.Instancer, cfg DBConfig) (d DataStore, err error)
```


Expand All @@ -799,7 +871,7 @@ func FromConfig(l *logrus.Entry, authorizer authorizer.Authorizer, instancer aut
### func [FromEnv](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L100>)

```go
func FromEnv(l *logrus.Entry, authorizer authorizer.Authorizer, instancer authorizer.Instancer) (d DataStore, err error)
func FromEnv(l *logrus.Entry, a authorizer.Authorizer, instancer authorizer.Instancer) (d DataStore, err error)
```


Expand Down Expand Up @@ -879,7 +951,7 @@ func GetRecordInstanceFromSlice(x interface{}) Record


<a name="TenancyInfo"></a>
## type [TenancyInfo](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/database.go#L72-L76>)
## type [TenancyInfo](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/database.go#L73-L77>)



Expand Down
38 changes: 38 additions & 0 deletions pkg/authorizer/transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package authorizer
krishnamiriyala marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"

"gorm.io/gorm"
)

type TransactionContextKey string

const (
TransactionCtx = TransactionContextKey("DB_TRANSACTION")
)

type TransactionFetcher interface {
IsTransactionCtx(ctx context.Context) bool
GetTransactionCtx(ctx context.Context) *gorm.DB
WithTransactionCtx(ctx context.Context, tx *gorm.DB) context.Context
}

type SimpleTransactionFetcher struct{}

func (s SimpleTransactionFetcher) GetTransactionCtx(ctx context.Context) *gorm.DB {
if v := ctx.Value(TransactionCtx); v != nil {
if dbTx, ok := v.(*gorm.DB); ok {
return dbTx
}
}
return nil
}

func (s SimpleTransactionFetcher) WithTransactionCtx(ctx context.Context, tx *gorm.DB) context.Context {
return context.WithValue(ctx, TransactionCtx, tx)
}

func (s SimpleTransactionFetcher) IsTransactionCtx(ctx context.Context) bool {
return s.GetTransactionCtx(ctx) != nil
}
Loading