Skip to content

Commit

Permalink
change opengauss to postgres
Browse files Browse the repository at this point in the history
Signed-off-by: ligjn <[email protected]>
  • Loading branch information
ligjn committed Aug 1, 2023
1 parent 5af913e commit eaec58e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 32 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/server-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ jobs:
HOST_PORT: 15432
CONTAINER_PORT: 5432

- name: Set up Postgres
uses: Harmon758/postgresql-action@v1
with:
postgresql version: '11'
postgresql db: 'kubevela'
postgresql user: 'kubevela'
postgresql password: 'Kubevela-123'

- name: Start MongoDB
uses: supercharge/mongodb-github-action@d26215f71b2ce60420a2a3776a25893d11a65f85 # 1.9.0
with:
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func NewConfig() *Config {
func (s *Config) Validate() []error {
var errs []error

if s.Datastore.Type != "mongodb" && s.Datastore.Type != "kubeapi" && s.Datastore.Type != "opengauss" {
if s.Datastore.Type != "mongodb" && s.Datastore.Type != "kubeapi" && s.Datastore.Type != "postgres" {
errs = append(errs, fmt.Errorf("not support datastore type %s", s.Datastore.Type))
}

Expand Down
26 changes: 24 additions & 2 deletions pkg/server/infrastructure/datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore/kubeapi"
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore/mongodb"
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore/mysql"
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore/opengauss"
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore/postgres"
)

func initMysqlTestDs() (datastore.DataStore, error) {
Expand All @@ -69,6 +69,28 @@ func initMysqlTestDs() (datastore.DataStore, error) {
return mysqlDriver, nil
}

func initPostgresTestDs() (datastore.DataStore, error) {

Check failure on line 72 in pkg/server/infrastructure/datastore/datastore_test.go

View workflow job for this annotation

GitHub Actions / check

func initPostgresTestDs is unused (U1000)
db, err := gorm.Open(postgresorm.Open("postgres://postgres:[email protected]:5432/kubevela?sslmode=disable&client_encoding=UTF-8&connect_timeout=1"), &gorm.Config{})
if err != nil {
return nil, err
}
for _, v := range model.GetRegisterModels() {
err := db.Migrator().DropTable(&v)
if err != nil {
return nil, err
}
}
postgresDriver, err := postgres.New(context.TODO(), datastore.Config{
URL: "postgres://postgres:[email protected]:5432/kubevela?sslmode=disable&client_encoding=UTF-8&connect_timeout=1",
Database: "kubevela",
})
if err != nil {
return nil, err
}
return postgresDriver, nil
}

// initOpenGaussTestDs Postgres Driver is also compatible with OpenGaussian databases
func initOpenGaussTestDs() (datastore.DataStore, error) {
db, err := gorm.Open(postgresorm.Open("postgres://gaussdb:[email protected]:15432/kubevela?sslmode=disable&client_encoding=UTF-8&connect_timeout=1"), &gorm.Config{})
if err != nil {
Expand All @@ -80,7 +102,7 @@ func initOpenGaussTestDs() (datastore.DataStore, error) {
return nil, err
}
}
openGaussDriver, err := opengauss.New(context.TODO(), datastore.Config{
openGaussDriver, err := postgres.New(context.TODO(), datastore.Config{
URL: "postgres://gaussdb:[email protected]:15432/kubevela?sslmode=disable&client_encoding=UTF-8&connect_timeout=1",
Database: "kubevela",
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package opengauss
package postgres

import (
"context"
Expand All @@ -34,12 +34,12 @@ import (
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore/sqlnamer"
)

type opengauss struct {
type postgres struct {
client gorm.DB
database string
}

// New opengauss datastore instance
// New postgres datastore instance
func New(ctx context.Context, cfg datastore.Config) (datastore.DataStore, error) {

db, err := gorm.Open(postgresorm.Open(cfg.URL), &gorm.Config{
Expand All @@ -58,15 +58,15 @@ func New(ctx context.Context, cfg datastore.Config) (datastore.DataStore, error)
}
}

m := &opengauss{
m := &postgres{
client: *db.WithContext(ctx),
database: cfg.Database,
}
return m, nil
}

// Add add data model
func (m *opengauss) Add(ctx context.Context, entity datastore.Entity) error {
func (m *postgres) Add(ctx context.Context, entity datastore.Entity) error {
if entity.PrimaryKey() == "" {
return datastore.ErrPrimaryEmpty
}
Expand All @@ -86,7 +86,7 @@ func (m *opengauss) Add(ctx context.Context, entity datastore.Entity) error {
}

// BatchAdd batch add entity, this operation has some atomicity.
func (m *opengauss) BatchAdd(ctx context.Context, entities []datastore.Entity) error {
func (m *postgres) BatchAdd(ctx context.Context, entities []datastore.Entity) error {
notRollback := make(map[string]bool)
for i, saveEntity := range entities {
if err := m.Add(ctx, saveEntity); err != nil {
Expand All @@ -109,7 +109,7 @@ func (m *opengauss) BatchAdd(ctx context.Context, entities []datastore.Entity) e
}

// Get get data model
func (m *opengauss) Get(ctx context.Context, entity datastore.Entity) error {
func (m *postgres) Get(ctx context.Context, entity datastore.Entity) error {
if entity.PrimaryKey() == "" {
return datastore.ErrPrimaryEmpty
}
Expand All @@ -127,7 +127,7 @@ func (m *opengauss) Get(ctx context.Context, entity datastore.Entity) error {
}

// Put update data model
func (m *opengauss) Put(ctx context.Context, entity datastore.Entity) error {
func (m *postgres) Put(ctx context.Context, entity datastore.Entity) error {
if entity.PrimaryKey() == "" {
return datastore.ErrPrimaryEmpty
}
Expand All @@ -145,7 +145,7 @@ func (m *opengauss) Put(ctx context.Context, entity datastore.Entity) error {
}

// IsExist determine whether data exists.
func (m *opengauss) IsExist(ctx context.Context, entity datastore.Entity) (bool, error) {
func (m *postgres) IsExist(ctx context.Context, entity datastore.Entity) (bool, error) {
if entity.PrimaryKey() == "" {
return false, datastore.ErrPrimaryEmpty
}
Expand All @@ -164,7 +164,7 @@ func (m *opengauss) IsExist(ctx context.Context, entity datastore.Entity) (bool,
}

// Delete delete data
func (m *opengauss) Delete(ctx context.Context, entity datastore.Entity) error {
func (m *postgres) Delete(ctx context.Context, entity datastore.Entity) error {
if entity.PrimaryKey() == "" {
return datastore.ErrPrimaryEmpty
}
Expand Down Expand Up @@ -216,7 +216,7 @@ func _applyFilterOptions(clauses []clause.Expression, filterOptions datastore.Fi
}

// List list entity function
func (m *opengauss) List(ctx context.Context, entity datastore.Entity, op *datastore.ListOptions) ([]datastore.Entity, error) {
func (m *postgres) List(ctx context.Context, entity datastore.Entity, op *datastore.ListOptions) ([]datastore.Entity, error) {
if entity.TableName() == "" {
return nil, datastore.ErrTableNameEmpty
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func (m *opengauss) List(ctx context.Context, entity datastore.Entity, op *datas
}

// Count counts entities
func (m *opengauss) Count(ctx context.Context, entity datastore.Entity, filterOptions *datastore.FilterOptions) (int64, error) {
func (m *postgres) Count(ctx context.Context, entity datastore.Entity, filterOptions *datastore.FilterOptions) (int64, error) {
if entity.TableName() == "" {
return 0, datastore.ErrTableNameEmpty
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package opengauss
package postgres

import (
"context"
Expand All @@ -26,15 +26,15 @@ import (
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore"
)

func TestOpenGauss(t *testing.T) {
func TestPostgres(t *testing.T) {
_, err := New(context.TODO(), datastore.Config{
URL: "postgres://gaussdb:[email protected]:15432/kubevela?sslmode=disable&client_encoding=UTF-8&connect_timeout=1",
URL: "postgres://postgres:[email protected]:5432/kubevela?sslmode=disable&client_encoding=UTF-8&connect_timeout=1",
Database: "kubevela",
})
if err != nil {
t.Fatal(err)
}

RegisterFailHandler(Fail)
RunSpecs(t, "OpenGauss Suite")
RunSpecs(t, "Postgres Suite")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package opengauss
package postgres

/*
Copyright 2021 The KubeVela Authors.
Expand All @@ -23,28 +23,28 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
opengaussgorm "gorm.io/driver/postgres"
postgresgorm "gorm.io/driver/postgres"
"gorm.io/gorm"

"github.com/kubevela/velaux/pkg/server/domain/model"
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore"
)

var opengaussDriver datastore.DataStore
var postgresDriver datastore.DataStore
var _ = BeforeSuite(func(ctx SpecContext) {
rand.Seed(time.Now().UnixNano())
By("bootstrapping opengauss test environment")
db, err := gorm.Open(opengaussgorm.Open("postgres://gaussdb:[email protected]:15432/kubevela?sslmode=disable&client_encoding=UTF-8&connect_timeout=1"), &gorm.Config{})
By("bootstrapping postgres test environment")
db, err := gorm.Open(postgresgorm.Open("postgres://postgres:[email protected]:5432/kubevela?sslmode=disable&client_encoding=UTF-8&connect_timeout=1"), &gorm.Config{})
Expect(err).ToNot(HaveOccurred())
for _, v := range model.GetRegisterModels() {
err := db.Migrator().DropTable(&v)
Expect(err).ToNot(HaveOccurred())
}
opengaussDriver, err = New(context.TODO(), datastore.Config{
URL: "postgres://gaussdb:[email protected]:15432/kubevela?sslmode=disable&client_encoding=UTF-8&connect_timeout=1",
postgresDriver, err = New(context.TODO(), datastore.Config{
URL: "postgres://postgres:[email protected]:5432/kubevela?sslmode=disable&client_encoding=UTF-8&connect_timeout=1",
Database: "kubevela",
})
Expect(err).ToNot(HaveOccurred())
Expect(opengaussDriver).ToNot(BeNil())
By("create opengauss driver success")
Expect(postgresDriver).ToNot(BeNil())
By("create postgres driver success")
}, NodeTimeout(2*time.Minute))
8 changes: 4 additions & 4 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import (
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore/kubeapi"
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore/mongodb"
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore/mysql"
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore/opengauss"
"github.com/kubevela/velaux/pkg/server/infrastructure/datastore/postgres"
"github.com/kubevela/velaux/pkg/server/interfaces/api"
"github.com/kubevela/velaux/pkg/server/utils"
"github.com/kubevela/velaux/pkg/server/utils/bcode"
Expand Down Expand Up @@ -148,10 +148,10 @@ func (s *restServer) buildIoCContainer() error {
if err != nil {
return fmt.Errorf("create mysql datastore instance failure %w", err)
}
case "opengauss":
ds, err = opengauss.New(context.Background(), s.cfg.Datastore)
case "postgres":
ds, err = postgres.New(context.Background(), s.cfg.Datastore)
if err != nil {
return fmt.Errorf("create opengauss datastore instance failure %w", err)
return fmt.Errorf("create postgres datastore instance failure %w", err)
}
default:
return fmt.Errorf("not support datastore type %s", s.cfg.Datastore.Type)
Expand Down

0 comments on commit eaec58e

Please sign in to comment.