Skip to content

Commit

Permalink
Add database pool (#37)
Browse files Browse the repository at this point in the history
* Add database pool
  • Loading branch information
hwbrzzl authored Feb 13, 2023
1 parent 72f6226 commit bde8fe6
Show file tree
Hide file tree
Showing 10 changed files with 759 additions and 701 deletions.
32 changes: 16 additions & 16 deletions contracts/filesystem/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ type Storage interface {

//go:generate mockery --name=Driver
type Driver interface {
WithContext(ctx context.Context) Driver
AllDirectories(path string) ([]string, error)
AllFiles(path string) ([]string, error)
Copy(oldFile, newFile string) error
Delete(file ...string) error
DeleteDirectory(directory string) error
Directories(path string) ([]string, error)
// Download(path string)
Exists(file string) bool
Files(path string) ([]string, error)
Get(file string) (string, error)
MakeDirectory(directory string) error
Missing(file string) bool
Move(oldFile, newFile string) error
Path(file string) string
Put(file, content string) error
PutFile(path string, source File) (string, error)
PutFileAs(path string, source File, name string) (string, error)
Get(file string) (string, error)
Size(file string) (int64, error)
Path(file string) string
Exists(file string) bool
Missing(file string) bool
// Download(path string)
Url(file string) string
TemporaryUrl(file string, time time.Time) (string, error)
Copy(oldFile, newFile string) error
Move(oldFile, newFile string) error
Delete(file ...string) error
Files(path string) ([]string, error)
AllFiles(path string) ([]string, error)
Directories(path string) ([]string, error)
AllDirectories(path string) ([]string, error)
MakeDirectory(directory string) error
DeleteDirectory(directory string) error
WithContext(ctx context.Context) Driver
Url(file string) string
}

//go:generate mockery --name=File
Expand Down
10 changes: 10 additions & 0 deletions contracts/http/mocks/Response.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions contracts/http/mocks/ResponseSuccess.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions database/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,31 @@ func New(connection string) (*gorm.DB, error) {
return nil, err
}

if err := configurePool(instance); err != nil {
return nil, err
}

if err := readWriteSeparate(connection, instance, readConfigs, writeConfigs); err != nil {
return nil, err
}

return instance, err
}

func configurePool(db *gorm.DB) error {
sqlDB, err := db.DB()
if err != nil {
return err
}

sqlDB.SetMaxIdleConns(facades.Config.GetInt("database.pool.max_idle_conns", 10))
sqlDB.SetMaxOpenConns(facades.Config.GetInt("database.pool.max_open_conns", 100))
sqlDB.SetConnMaxIdleTime(time.Duration(facades.Config.GetInt("database.pool.conn_max_idletime", 3600)) * time.Second)
sqlDB.SetConnMaxLifetime(time.Duration(facades.Config.GetInt("database.pool.conn_max_lifetime", 3600)) * time.Second)

return nil
}

func instance(dialector gorm.Dialector) (*gorm.DB, error) {
var logLevel gormLogger.LogLevel
if facades.Config.GetBool("app.debug") {
Expand Down
174 changes: 95 additions & 79 deletions database/gorm/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ory/dockertest/v3"
"github.com/spf13/cast"

configmocks "github.com/goravel/framework/contracts/config/mocks"
"github.com/goravel/framework/contracts/database"
contractsorm "github.com/goravel/framework/contracts/database/orm"
testingdocker "github.com/goravel/framework/testing/docker"
Expand Down Expand Up @@ -85,6 +86,13 @@ func SqlserverDocker() (*dockertest.Pool, *dockertest.Resource, contractsorm.DB,
return pool, resource, db, nil
}

func mockPool(mockConfig *configmocks.Config) {
mockConfig.On("GetInt", "database.pool.max_idle_conns", 10).Return(10)
mockConfig.On("GetInt", "database.pool.max_open_conns", 100).Return(100)
mockConfig.On("GetInt", "database.pool.conn_max_idletime", 3600).Return(3600)
mockConfig.On("GetInt", "database.pool.conn_max_lifetime", 3600).Return(3600)
}

func mockSingleMysql(port int) {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.mysql.read").Return(nil)
Expand All @@ -98,6 +106,7 @@ func mockSingleMysql(port int) {
mockConfig.On("GetString", "database.connections.mysql.loc").Return("Local")
mockConfig.On("GetString", "database.connections.mysql.database").Return("mysql")
mockConfig.On("GetInt", "database.connections.mysql.port").Return(port)
mockPool(mockConfig)
}

func mockReadWriteMysql(readPort, writePort int) {
Expand All @@ -114,6 +123,92 @@ func mockReadWriteMysql(readPort, writePort int) {
mockConfig.On("GetString", "database.connections.mysql.loc").Return("Local")
mockConfig.On("GetString", "database.connections.mysql.database").Return("mysql")
mockConfig.On("GetString", "database.connections.mysql.database").Return(dbDatabase)
mockPool(mockConfig)
}

func mockSinglePostgresql(port int) {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.postgresql.read").Return(nil)
mockConfig.On("Get", "database.connections.postgresql.write").Return(nil)
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.postgresql.driver").Return(contractsorm.DriverPostgresql.String())
mockConfig.On("GetString", "database.connections.postgresql.host").Return("localhost")
mockConfig.On("GetString", "database.connections.postgresql.username").Return(dbUser)
mockConfig.On("GetString", "database.connections.postgresql.password").Return(dbPassword)
mockConfig.On("GetString", "database.connections.postgresql.sslmode").Return("disable")
mockConfig.On("GetString", "database.connections.postgresql.timezone").Return("UTC")
mockConfig.On("GetString", "database.connections.postgresql.database").Return("postgres")
mockConfig.On("GetInt", "database.connections.postgresql.port").Return(port)
mockPool(mockConfig)
}

func mockReadWritePostgresql(readPort, writePort int) {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.postgresql.read").Return([]database.Config{
{Host: "localhost", Port: readPort, Username: dbUser, Password: dbPassword},
})
mockConfig.On("Get", "database.connections.postgresql.write").Return([]database.Config{
{Host: "localhost", Port: writePort, Username: dbUser, Password: dbPassword},
})
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.postgresql.driver").Return(contractsorm.DriverPostgresql.String())
mockConfig.On("GetString", "database.connections.postgresql.sslmode").Return("disable")
mockConfig.On("GetString", "database.connections.postgresql.timezone").Return("UTC")
mockConfig.On("GetString", "database.connections.postgresql.database").Return("postgres")
mockPool(mockConfig)
}

func mockSingleSqlite(dbName string) {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.sqlite.read").Return(nil)
mockConfig.On("Get", "database.connections.sqlite.write").Return(nil)
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.sqlite.driver").Return(contractsorm.DriverSqlite.String())
mockConfig.On("GetString", "database.connections.sqlite.database").Return(dbName)
mockPool(mockConfig)
}

func mockReadWriteSqlite() {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.sqlite.read").Return([]database.Config{
{Database: dbDatabase},
})
mockConfig.On("Get", "database.connections.sqlite.write").Return([]database.Config{
{Database: dbDatabase1},
})
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.sqlite.driver").Return(contractsorm.DriverSqlite.String())
mockPool(mockConfig)
}

func mockSingleSqlserver(port int) {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.sqlserver.read").Return(nil)
mockConfig.On("Get", "database.connections.sqlserver.write").Return(nil)
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.sqlserver.driver").Return(contractsorm.DriverSqlserver.String())
mockConfig.On("GetString", "database.connections.sqlserver.host").Return("localhost")
mockConfig.On("GetString", "database.connections.sqlserver.username").Return(dbUser1)
mockConfig.On("GetString", "database.connections.sqlserver.password").Return(dbPassword)
mockConfig.On("GetString", "database.connections.sqlserver.database").Return("msdb")
mockConfig.On("GetString", "database.connections.sqlserver.charset").Return("utf8mb4")
mockConfig.On("GetInt", "database.connections.sqlserver.port").Return(port)
mockPool(mockConfig)
}

func mockReadWriteSqlserver(readPort, writePort int) {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.sqlserver.read").Return([]database.Config{
{Host: "localhost", Port: readPort, Username: dbUser1, Password: dbPassword},
})
mockConfig.On("Get", "database.connections.sqlserver.write").Return([]database.Config{
{Host: "localhost", Port: writePort, Username: dbUser1, Password: dbPassword},
})
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.sqlserver.driver").Return(contractsorm.DriverSqlserver.String())
mockConfig.On("GetString", "database.connections.sqlserver.database").Return("msdb")
mockConfig.On("GetString", "database.connections.sqlserver.charset").Return("utf8mb4")
mockPool(mockConfig)
}

func mysqlDockerDB(pool *dockertest.Pool, createTable bool) (contractsorm.DB, error) {
Expand Down Expand Up @@ -169,36 +264,6 @@ func initMysql(pool *dockertest.Pool) (contractsorm.DB, error) {
return db, nil
}

func mockSinglePostgresql(port int) {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.postgresql.read").Return(nil)
mockConfig.On("Get", "database.connections.postgresql.write").Return(nil)
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.postgresql.driver").Return(contractsorm.DriverPostgresql.String())
mockConfig.On("GetString", "database.connections.postgresql.host").Return("localhost")
mockConfig.On("GetString", "database.connections.postgresql.username").Return(dbUser)
mockConfig.On("GetString", "database.connections.postgresql.password").Return(dbPassword)
mockConfig.On("GetString", "database.connections.postgresql.sslmode").Return("disable")
mockConfig.On("GetString", "database.connections.postgresql.timezone").Return("UTC")
mockConfig.On("GetString", "database.connections.postgresql.database").Return("postgres")
mockConfig.On("GetInt", "database.connections.postgresql.port").Return(port)
}

func mockReadWritePostgresql(readPort, writePort int) {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.postgresql.read").Return([]database.Config{
{Host: "localhost", Port: readPort, Username: dbUser, Password: dbPassword},
})
mockConfig.On("Get", "database.connections.postgresql.write").Return([]database.Config{
{Host: "localhost", Port: writePort, Username: dbUser, Password: dbPassword},
})
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.postgresql.driver").Return(contractsorm.DriverPostgresql.String())
mockConfig.On("GetString", "database.connections.postgresql.sslmode").Return("disable")
mockConfig.On("GetString", "database.connections.postgresql.timezone").Return("UTC")
mockConfig.On("GetString", "database.connections.postgresql.database").Return("postgres")
}

func postgresqlDockerDB(pool *dockertest.Pool, createTable bool) (contractsorm.DB, error) {
db, err := initPostgresql(pool)
if err != nil {
Expand Down Expand Up @@ -254,27 +319,6 @@ func initPostgresql(pool *dockertest.Pool) (contractsorm.DB, error) {
return db, nil
}

func mockSingleSqlite(dbName string) {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.sqlite.read").Return(nil)
mockConfig.On("Get", "database.connections.sqlite.write").Return(nil)
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.sqlite.driver").Return(contractsorm.DriverSqlite.String())
mockConfig.On("GetString", "database.connections.sqlite.database").Return(dbName)
}

func mockReadWriteSqlite() {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.sqlite.read").Return([]database.Config{
{Database: dbDatabase},
})
mockConfig.On("Get", "database.connections.sqlite.write").Return([]database.Config{
{Database: dbDatabase1},
})
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.sqlite.driver").Return(contractsorm.DriverSqlite.String())
}

func sqliteDockerDB(pool *dockertest.Pool, createTable bool) (contractsorm.DB, error) {
db, err := initSqlite(pool)
if err != nil {
Expand Down Expand Up @@ -323,34 +367,6 @@ func initSqlite(pool *dockertest.Pool) (contractsorm.DB, error) {
return db, nil
}

func mockSingleSqlserver(port int) {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.sqlserver.read").Return(nil)
mockConfig.On("Get", "database.connections.sqlserver.write").Return(nil)
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.sqlserver.driver").Return(contractsorm.DriverSqlserver.String())
mockConfig.On("GetString", "database.connections.sqlserver.host").Return("localhost")
mockConfig.On("GetString", "database.connections.sqlserver.username").Return(dbUser1)
mockConfig.On("GetString", "database.connections.sqlserver.password").Return(dbPassword)
mockConfig.On("GetString", "database.connections.sqlserver.database").Return("msdb")
mockConfig.On("GetString", "database.connections.sqlserver.charset").Return("utf8mb4")
mockConfig.On("GetInt", "database.connections.sqlserver.port").Return(port)
}

func mockReadWriteSqlserver(readPort, writePort int) {
mockConfig := mock.Config()
mockConfig.On("Get", "database.connections.sqlserver.read").Return([]database.Config{
{Host: "localhost", Port: readPort, Username: dbUser1, Password: dbPassword},
})
mockConfig.On("Get", "database.connections.sqlserver.write").Return([]database.Config{
{Host: "localhost", Port: writePort, Username: dbUser1, Password: dbPassword},
})
mockConfig.On("GetBool", "app.debug").Return(true)
mockConfig.On("GetString", "database.connections.sqlserver.driver").Return(contractsorm.DriverSqlserver.String())
mockConfig.On("GetString", "database.connections.sqlserver.database").Return("msdb")
mockConfig.On("GetString", "database.connections.sqlserver.charset").Return("utf8mb4")
}

func sqlserverDockerDB(pool *dockertest.Pool, createTable bool) (contractsorm.DB, error) {
db, err := initSqlserver(pool)
if err != nil {
Expand Down
Loading

0 comments on commit bde8fe6

Please sign in to comment.