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

Feat: ORM supports table name prefix and singular #65

Merged
merged 1 commit into from
Mar 7, 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
13 changes: 11 additions & 2 deletions database/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
"github.com/spf13/cast"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"gorm.io/plugin/dbresolver"

contractsdatabase "github.com/goravel/framework/contracts/database"
contractsorm "github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/database/orm"
"github.com/goravel/framework/facades"
"github.com/goravel/framework/support/database"
supporttime "github.com/goravel/framework/support/time"
)

func New(connection string) (*gorm.DB, error) {
Expand All @@ -35,7 +37,7 @@ func New(connection string) (*gorm.DB, error) {
return nil, nil
}

instance, err := instance(dial)
instance, err := instance(connection, dial)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -65,7 +67,7 @@ func configurePool(db *gorm.DB) error {
return nil
}

func instance(dialector gorm.Dialector) (*gorm.DB, error) {
func instance(connection string, dialector gorm.Dialector) (*gorm.DB, error) {
var logLevel gormLogger.LogLevel
if facades.Config.GetBool("app.debug") {
logLevel = gormLogger.Info
Expand All @@ -84,6 +86,13 @@ func instance(dialector gorm.Dialector) (*gorm.DB, error) {
DisableForeignKeyConstraintWhenMigrating: true,
SkipDefaultTransaction: true,
Logger: logger.LogMode(logLevel),
NowFunc: func() time.Time {
return supporttime.Now()
},
NamingStrategy: schema.NamingStrategy{
TablePrefix: facades.Config.GetString(fmt.Sprintf("database.connections.%s.prefix", connection)),
SingularTable: facades.Config.GetBool(fmt.Sprintf("database.connections.%s.singular", connection)),
},
})
}

Expand Down
73 changes: 73 additions & 0 deletions database/gorm/gorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,79 @@ func TestReadWriteSeparate(t *testing.T) {
}
}

func TestTablePrefixAndSingular(t *testing.T) {
mysqlPool, mysqlResource, err := initMysqlDocker()
if err != nil {
log.Fatalf("Init mysql docker error: %s", err)
}
mockMysqlWithPrefixAndSingular(cast.ToInt(mysqlResource.GetPort("3306/tcp")))
mysqlDB, err := mysqlDockerDBWithPrefixAndSingular(mysqlPool)
if err != nil {
log.Fatalf("Init mysql error: %s", err)
}

postgresqlPool, postgresqlResource, err := initPostgresqlDocker()
if err != nil {
log.Fatalf("Init postgresql docker error: %s", err)
}
mockPostgresqlWithPrefixAndSingular(cast.ToInt(postgresqlResource.GetPort("5432/tcp")))
postgresqlDB, err := postgresqlDockerDBWithPrefixAndSingular(postgresqlPool)
if err != nil {
log.Fatalf("Init postgresql error: %s", err)
}

sqlitePool, _, err := initSqliteDocker()
if err != nil {
log.Fatalf("Init sqlite docker error: %s", err)
}
mockSqliteWithPrefixAndSingular(dbDatabase)
sqliteDB, err := sqliteDockerDBWithPrefixAndSingular(sqlitePool)
if err != nil {
log.Fatalf("Init sqlite error: %s", err)
}

sqlserverPool, sqlserverResource, err := initSqlserverDocker()
if err != nil {
log.Fatalf("Init sqlserver docker error: %s", err)
}
mockSqlserverWithPrefixAndSingular(cast.ToInt(sqlserverResource.GetPort("1433/tcp")))
sqlserverDB, err := sqlserverDockerDBWithPrefixAndSingular(sqlserverPool)
if err != nil {
log.Fatalf("Init sqlserver error: %s", err)
}

dbs := map[contractsorm.Driver]contractsorm.DB{
contractsorm.DriverMysql: mysqlDB,
contractsorm.DriverPostgresql: postgresqlDB,
contractsorm.DriverSqlite: sqliteDB,
contractsorm.DriverSqlserver: sqlserverDB,
}

for drive, db := range dbs {
t.Run(drive.String(), func(t *testing.T) {
user := User{Name: "user"}
assert.Nil(t, db.Create(&user))
assert.True(t, user.ID > 0)

var user1 User
assert.Nil(t, db.Find(&user1, user.ID))
assert.True(t, user1.ID > 0)
})
}

file.Remove(dbDatabase)

if err := mysqlPool.Purge(mysqlResource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
if err := postgresqlPool.Purge(postgresqlResource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
if err := sqlserverPool.Purge(sqlserverResource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
}

func paginator(page string, limit string) func(methods contractsorm.Query) contractsorm.Query {
return func(query contractsorm.Query) contractsorm.Query {
page, _ := strconv.Atoi(page)
Expand Down
Loading