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

chore: remove wire #660

Merged
merged 12 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
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 Configs interface {
Reads() []FullConfig
Writes() []FullConfig
}
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
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"
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
)

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"
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
"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()
configs := databasedb.NewConfigs(config, connection)
writeConfigs := configs.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:
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
mysqlDsn := databasedb.Dsn(writeConfigs[0])
if mysqlDsn == "" {
return nil, nil
}
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
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:
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
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
Loading