Skip to content

Commit

Permalink
Database read write separate (#35)
Browse files Browse the repository at this point in the history
* Database read write sperate
  • Loading branch information
hwbrzzl authored Feb 12, 2023
1 parent 6c56e0d commit 9d41423
Show file tree
Hide file tree
Showing 27 changed files with 2,134 additions and 1,548 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Golang developers quickly build their own applications.

## Roadmap

- [ ] Database read-write separation
- [ ] Optimize unit tests

## Documentation

Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Goravel 是一个功能完备、具有良好扩展能力的 Web 应用程序框

## 路线图

- [ ] 数据库读写分离
- [ ] 完善单元测试

## 文档

Expand Down
10 changes: 4 additions & 6 deletions config/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"testing"

"github.com/gookit/color"
"github.com/stretchr/testify/suite"

"github.com/goravel/framework/support/file"
Expand All @@ -15,14 +14,13 @@ type ApplicationTestSuite struct {
}

func TestApplicationTestSuite(t *testing.T) {
if !file.Exists("../.env") {
color.Redln("No config tests run, need create .env based on .env.example, then initialize it")
return
}
file.Create(".env", "")

suite.Run(t, &ApplicationTestSuite{
config: NewApplication("../.env"),
config: NewApplication(".env"),
})

file.Remove(".env")
}

func (s *ApplicationTestSuite) SetupTest() {
Expand Down
8 changes: 8 additions & 0 deletions contracts/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ type Transaction interface {
SelectContext(ctx context.Context, dest any, query string, args ...any) error
GetContext(ctx context.Context, dest any, query string, args ...any) error
}

type Config struct {
Host string
Port int
Database string
Username string
Password string
}
18 changes: 18 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.

14 changes: 14 additions & 0 deletions contracts/route/mocks/Engine.go

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

19 changes: 10 additions & 9 deletions database/console/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@ import (
"github.com/golang-migrate/migrate/v4/database/sqlserver"

"github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/database/support"
"github.com/goravel/framework/database/gorm"
"github.com/goravel/framework/facades"
)

func getMigrate() (*migrate.Migrate, error) {
connection := facades.Config.GetString("database.default")
driver := facades.Config.GetString("database.connections." + connection + ".driver")
dir := "file://./database/migrations"
_, writeConfigs, err := gorm.Configs(connection)
if err != nil {
return nil, err
}

switch orm.Driver(driver) {
case orm.DriverMysql:
dsn := support.GetMysqlDsn(connection)
dsn := gorm.MysqlDsn(connection, writeConfigs[0])
if dsn == "" {
return nil, nil
}
Expand All @@ -31,10 +36,6 @@ func getMigrate() (*migrate.Migrate, error) {
return nil, err
}

//if err := db.Ping(); err != nil {
// return nil, errors.New("Could not ping to database: " + err.Error())
//}

instance, err := mysql.WithInstance(db, &mysql.Config{
MigrationsTable: facades.Config.GetString("database.migrations"),
})
Expand All @@ -44,7 +45,7 @@ func getMigrate() (*migrate.Migrate, error) {

return migrate.NewWithDatabaseInstance(dir, "mysql", instance)
case orm.DriverPostgresql:
dsn := support.GetPostgresqlDsn(connection)
dsn := gorm.PostgresqlDsn(connection, writeConfigs[0])
if dsn == "" {
return nil, nil
}
Expand All @@ -63,7 +64,7 @@ func getMigrate() (*migrate.Migrate, error) {

return migrate.NewWithDatabaseInstance(dir, "postgres", instance)
case orm.DriverSqlite:
dsn := support.GetSqliteDsn(connection)
dsn := gorm.SqliteDsn(writeConfigs[0])
if dsn == "" {
return nil, nil
}
Expand All @@ -82,7 +83,7 @@ func getMigrate() (*migrate.Migrate, error) {

return migrate.NewWithDatabaseInstance(dir, "sqlite3", instance)
case orm.DriverSqlserver:
dsn := support.GetSqlserverDsn(connection)
dsn := gorm.SqlserverDsn(connection, writeConfigs[0])
if dsn == "" {
return nil, nil
}
Expand Down
109 changes: 0 additions & 109 deletions database/db.go

This file was deleted.

7 changes: 0 additions & 7 deletions database/errors.go

This file was deleted.

Loading

0 comments on commit 9d41423

Please sign in to comment.