Skip to content

Commit

Permalink
chore: remove wire (#660)
Browse files Browse the repository at this point in the history
* chore: remove wire

* remove driver

* fix test

* fix test

* fix test

* fix test

* remove global variable

* optimize based on ai

* chore: update mocks

* fix test

* fix test

---------

Co-authored-by: hwbrzzl <hwbrzzl@users.noreply.github.com>
  • Loading branch information
hwbrzzl and hwbrzzl authored Oct 1, 2024
1 parent cd17022 commit aeaf75d
Show file tree
Hide file tree
Showing 63 changed files with 1,486 additions and 1,508 deletions.
32 changes: 32 additions & 0 deletions contracts/database/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
package database

const (
DriverMysql Driver = "mysql"
DriverPostgres Driver = "postgres"
DriverSqlite Driver = "sqlite"
DriverSqlserver Driver = "sqlserver"
)

type Driver string

func (d Driver) String() string {
return string(d)
}

// Config Used in config/database.go
type Config struct {
Host string
Port int
Database string
Username string
Password string
}

// FullConfig Fill the default value for Config
type FullConfig struct {
Config
Driver Driver
Connection string
Prefix string
Singular bool
Charset string // Mysql, Sqlserver
Loc string // Mysql
Sslmode string // Postgres
Timezone string // Postgres
}

type ConfigBuilder interface {
Reads() []FullConfig
Writes() []FullConfig
}
19 changes: 0 additions & 19 deletions contracts/database/gorm/wire_interface.go

This file was deleted.

14 changes: 0 additions & 14 deletions contracts/database/orm/constants.go

This file was deleted.

6 changes: 5 additions & 1 deletion contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package orm
import (
"context"
"database/sql"

"github.com/goravel/framework/contracts/database"
)

type Orm interface {
Expand All @@ -16,6 +18,8 @@ type Orm interface {
Factory() Factory
// Observe registers an observer with the Orm.
Observe(model any, observer Observer)
// Refresh resets the Orm instance.
Refresh()
// Transaction runs a callback wrapped in a database transaction.
Transaction(txFunc func(tx Query) error) error
// WithContext sets the context to be used by the Orm.
Expand All @@ -40,7 +44,7 @@ type Query interface {
// Distinct specifies distinct fields to query.
Distinct(args ...any) Query
// Driver gets the driver for the query.
Driver() Driver
Driver() database.Driver
// Exec executes raw sql
Exec(sql string, values ...any) (*Result, error)
// Exists returns true if matching records exist; otherwise, it returns false.
Expand Down
2 changes: 2 additions & 0 deletions contracts/foundation/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ type Application interface {
MakeSeeder() seeder.Facade
// MakeWith resolves the given type with the given parameters from the container.
MakeWith(key any, parameters map[string]any) (any, error)
// Refresh an instance on the given target.
Refresh(key any)
// Singleton registers a shared binding in the container.
Singleton(key any, callback func(app Application) (any, error))
}
4 changes: 2 additions & 2 deletions contracts/testing/testing.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package testing

import (
"github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/database"
"github.com/goravel/framework/contracts/database/seeder"
)

Expand Down Expand Up @@ -31,7 +31,7 @@ type DatabaseDriver interface {
// Image gets the database image.
Image(image Image)
// Driver gets the database driver name.
Driver() orm.Driver
Driver() database.Driver
// Stop the database.
Stop() error
}
Expand Down
30 changes: 13 additions & 17 deletions database/console/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/golang-migrate/migrate/v4/database/sqlserver"

"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/database"
"github.com/goravel/framework/database/console/driver"
"github.com/goravel/framework/database/db"
databasedb "github.com/goravel/framework/database/db"
"github.com/goravel/framework/support"
)

Expand All @@ -25,16 +25,15 @@ func getMigrate(config config.Config) (*migrate.Migrate, error) {
dir = fmt.Sprintf("file://%s/database/migrations", support.RelativePath)
}

gormConfig := db.NewConfigImpl(config, connection)
writeConfigs := gormConfig.Writes()
configBuilder := databasedb.NewConfigBuilder(config, connection)
writeConfigs := configBuilder.Writes()
if len(writeConfigs) == 0 {
return nil, errors.New("not found database configuration")
}

switch orm.Driver(driver) {
case orm.DriverMysql:
dsn := db.NewDsnImpl(config, connection)
mysqlDsn := dsn.Mysql(writeConfigs[0])
switch database.Driver(driver) {
case database.DriverMysql:
mysqlDsn := databasedb.Dsn(writeConfigs[0])
if mysqlDsn == "" {
return nil, nil
}
Expand All @@ -52,9 +51,8 @@ func getMigrate(config config.Config) (*migrate.Migrate, error) {
}

return migrate.NewWithDatabaseInstance(dir, "mysql", instance)
case orm.DriverPostgres:
dsn := db.NewDsnImpl(config, connection)
postgresDsn := dsn.Postgres(writeConfigs[0])
case database.DriverPostgres:
postgresDsn := databasedb.Dsn(writeConfigs[0])
if postgresDsn == "" {
return nil, nil
}
Expand All @@ -72,9 +70,8 @@ func getMigrate(config config.Config) (*migrate.Migrate, error) {
}

return migrate.NewWithDatabaseInstance(dir, "postgres", instance)
case orm.DriverSqlite:
dsn := db.NewDsnImpl(config, "")
sqliteDsn := dsn.Sqlite(writeConfigs[0])
case database.DriverSqlite:
sqliteDsn := databasedb.Dsn(writeConfigs[0])
if sqliteDsn == "" {
return nil, nil
}
Expand All @@ -92,9 +89,8 @@ func getMigrate(config config.Config) (*migrate.Migrate, error) {
}

return migrate.NewWithDatabaseInstance(dir, "sqlite3", instance)
case orm.DriverSqlserver:
dsn := db.NewDsnImpl(config, connection)
sqlserverDsn := dsn.Sqlserver(writeConfigs[0])
case database.DriverSqlserver:
sqlserverDsn := databasedb.Dsn(writeConfigs[0])
if sqlserverDsn == "" {
return nil, nil
}
Expand Down
10 changes: 5 additions & 5 deletions database/console/migrate_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/database"
"github.com/goravel/framework/database/migration"
"github.com/goravel/framework/support/carbon"
"github.com/goravel/framework/support/file"
Expand Down Expand Up @@ -49,20 +49,20 @@ func (receiver *MigrateCreator) getStub(table string, create bool) (string, stri
}

driver := receiver.config.GetString("database.connections." + receiver.config.GetString("database.default") + ".driver")
switch orm.Driver(driver) {
case orm.DriverPostgres:
switch database.Driver(driver) {
case database.DriverPostgres:
if create {
return migration.PostgresStubs{}.CreateUp(), migration.PostgresStubs{}.CreateDown()
}

return migration.PostgresStubs{}.UpdateUp(), migration.PostgresStubs{}.UpdateDown()
case orm.DriverSqlite:
case database.DriverSqlite:
if create {
return migration.SqliteStubs{}.CreateUp(), migration.SqliteStubs{}.CreateDown()
}

return migration.SqliteStubs{}.UpdateUp(), migration.SqliteStubs{}.UpdateDown()
case orm.DriverSqlserver:
case database.DriverSqlserver:
if create {
return migration.SqlserverStubs{}.CreateUp(), migration.SqlserverStubs{}.CreateDown()
}
Expand Down
12 changes: 6 additions & 6 deletions database/console/test_utils.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package console

import (
contractsorm "github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/database"
"github.com/goravel/framework/support/file"
)

func createMigrations(driver contractsorm.Driver) {
func createMigrations(driver database.Driver) {
switch driver {
case contractsorm.DriverPostgres:
case database.DriverPostgres:
createPostgresMigrations()
case contractsorm.DriverMysql:
case database.DriverMysql:
createMysqlMigrations()
case contractsorm.DriverSqlserver:
case database.DriverSqlserver:
createSqlserverMigrations()
case contractsorm.DriverSqlite:
case database.DriverSqlite:
createSqliteMigrations()
}
}
Expand Down
76 changes: 0 additions & 76 deletions database/db/config.go

This file was deleted.

Loading

0 comments on commit aeaf75d

Please sign in to comment.