Skip to content

Commit

Permalink
refactor(contrib): update database connection logic and remove unused…
Browse files Browse the repository at this point in the history
… files

- Update database connection logic in contrib/database/database.go
- Add error handling and connection settings
- Remove unused files and templates related to ent ORM
- Update import paths and versions in go.mod files
  • Loading branch information
godcong committed Nov 28, 2024
1 parent c19d71e commit c7194ed
Show file tree
Hide file tree
Showing 29 changed files with 1,745 additions and 425 deletions.
2 changes: 1 addition & 1 deletion .github/scripts/git/git_tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#

# shellcheck disable=SC1091
source "$(pwd)"/scripts/git/git_cmd.sh
source "$(pwd)"/.github/scripts/git/git_cmd.sh

function_checks() {
# Check if get_current_commit_hash is defined
Expand Down
2 changes: 1 addition & 1 deletion .github/scripts/git/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#

# shellcheck disable=SC1091
source "$(pwd)"/scripts/git/git_tag.sh
source "$(pwd)"/.github/scripts/git/git_tag.sh

"$@"
4 changes: 2 additions & 2 deletions .github/scripts/tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#

# shellcheck disable=SC1091
source "$(pwd)"/scripts/git/git_tag.sh
source "$(pwd)"/.github/scripts/git/git_tag.sh
# shellcheck disable=SC1091
source "$(pwd)"/scripts/git/git_cmd.sh
source "$(pwd)"/.github/scripts/git/git_cmd.sh

# Record the original working directory
ORIGINAL_DIR=$(pwd)
Expand Down
4 changes: 2 additions & 2 deletions .github/scripts/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ check_go_mod_and_act() {
if [ -f "$go_mod_name" ]; then
# If it exists, perform specific operations
echo "Updating packages in: $dir"
go get -u ./...
# go get -u ./...
go mod tidy

go test -race ./... || true
go test -race ./... || cd "$ORIGINAL_DIR" || return 1
local test_status=$?
# Only mark as updated if tests pass
if [ $test_status -eq 0 ]; then
Expand Down
2 changes: 1 addition & 1 deletion consul/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

"github.com/goexts/generic/settings"
"github.com/hashicorp/consul/api"
"github.com/origadmin/toolkits/errors"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"

"github.com/origadmin/runtime"
"github.com/origadmin/runtime/config"
configv1 "github.com/origadmin/runtime/gen/go/config/v1"

)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion consul/config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/consul/api"

"github.com/origadmin/runtime/config"
"github.com/origadmin/toolkits/context"
"github.com/origadmin/runtime/context"
)

const (
Expand Down
34 changes: 28 additions & 6 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,44 @@ package database
import (
"database/sql"

configv1 "github.com/origadmin/runtime/gen/go/config/v1"
"github.com/origadmin/toolkits/errors"

"github.com/origadmin/contrib/database/internal/mysql"
)

func Open(driverName, dataSourceName string) (*sql.DB, error) {
switch driverName {
func Open(database *configv1.Data_Database) (*sql.DB, error) {
if database == nil {
return nil, errors.New("database is nil")
}
switch database.Driver {
case "mysql":
err := mysql.CreateDatabase(dataSourceName, "")
err := mysql.CreateDatabase(database.Source, "")
if err != nil {
return nil, err
return nil, errors.Wrap(err, "create database error")
}
break
case "pgx":
driverName = "postgres"
database.Driver = "postgres"
break
default:

}
return sql.Open(driverName, dataSourceName)
db, err := sql.Open(database.Driver, database.Source)
if err != nil {
return nil, errors.Wrap(err, "open database error")
}
if database.MaxIdleConnections > 0 {
db.SetMaxIdleConns(int(database.MaxIdleConnections))
}
if database.MaxOpenConnections > 0 {
db.SetMaxOpenConns(int(database.MaxOpenConnections))
}
if t := database.ConnectionMaxLifetime.AsDuration(); t > 0 {
db.SetConnMaxLifetime(t)
}
if t := database.ConnectionMaxIdleTime.AsDuration(); t > 0 {
db.SetConnMaxIdleTime(t)
}
return db, nil
}
4 changes: 4 additions & 0 deletions database/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v1.14.24
github.com/origadmin/runtime v0.0.1
github.com/origadmin/toolkits/errors v0.0.11
github.com/sqlite3ent/sqlite3 v1.34.1
)

Expand All @@ -24,12 +25,15 @@ require (
github.com/google/gnostic v0.7.0 // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
Expand Down
8 changes: 8 additions & 0 deletions database/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,11 @@ github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
Expand Down Expand Up @@ -846,13 +851,16 @@ github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdh
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/origadmin/runtime v0.0.1 h1:SqgVbD4k/xgviItdYBxyWLSx0euWsuwxLp36fEr31Uk=
github.com/origadmin/runtime v0.0.1/go.mod h1:qjKsw3rZoT+/uhjEvQzUjdUK2CjU9I5RmbXDI962V8E=
github.com/origadmin/toolkits/errors v0.0.11 h1:0V0HcCrnhnHxNX8LgZpASYtnWZfpxBph0x9fCV01+Ko=
github.com/origadmin/toolkits/errors v0.0.11/go.mod h1:6DM+lY0GU/UWobF66Fh/zeLJl3zF6Ll9+8ujHjSCwFQ=
github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=
github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/phpdave11/gofpdi v1.0.13/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
Expand Down
22 changes: 0 additions & 22 deletions dbo/ent/builds.go

This file was deleted.

20 changes: 20 additions & 0 deletions dbo/ent/db.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
/*
* Copyright (c) 2024 OrigAdmin. All rights reserved.
*/

// Package ent implements the functions, types, and interfaces for the module.
package ent

import (
"database/sql"

entsql "entgo.io/ent/dialect/sql"
configv1 "github.com/origadmin/runtime/gen/go/config/v1"
)

func OpenDB(cfg *configv1.Data_Database) (*entsql.Driver, error) {
db, err := sql.Open(cfg.Driver, cfg.Source)
if err != nil {
return nil, err
}
entdb := entsql.OpenDB(cfg.Driver, db)
return entdb, nil
}
40 changes: 0 additions & 40 deletions dbo/ent/templates/crud.tpl

This file was deleted.

20 changes: 0 additions & 20 deletions dbo/ent/templates/crud_create.tpl

This file was deleted.

48 changes: 0 additions & 48 deletions dbo/ent/templates/crud_query.tpl

This file was deleted.

20 changes: 0 additions & 20 deletions dbo/ent/templates/crud_update.tpl

This file was deleted.

34 changes: 0 additions & 34 deletions dbo/ent/templates/crud_update_one.tpl

This file was deleted.

Loading

0 comments on commit c7194ed

Please sign in to comment.