Skip to content
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
5 changes: 4 additions & 1 deletion database/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ type Schema struct {

func NewSchema(config config.Config, log log.Log, orm contractsorm.Orm, driver driver.Driver, migrations []contractsschema.Migration) *Schema {
prefix := driver.Config().Prefix
schema := driver.Config().Schema
grammar := driver.Grammar()
processor := driver.Processor()
schema := driver.Config().Schema
if schema == "" {
schema = driver.Config().Database
}

return &Schema{
config: config,
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ google.golang.org/genproto v0.0.0-20241230172942-26aa7a208def/go.mod h1:zNtPaqLK
google.golang.org/genproto/googleapis/api v0.0.0-20241230172942-26aa7a208def h1:0Km0hi+g2KXbXL0+riZzSCKz23f4MmwicuEb00JeonI=
google.golang.org/genproto/googleapis/api v0.0.0-20241230172942-26aa7a208def/go.mod h1:u2DoMSpCXjrzqLdobRccQMc9wrnMAJ1DLng0a2yqM2Q=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250127172529-29210b9bc287 h1:J1H9f+LEdWAfHcez/4cvaVBox7cOYT+IU6rgqj5x++8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250127172529-29210b9bc287/go.mod h1:8BS3B93F/U1juMFq9+EDk+qOT5CO1R9IzXxG3PTqiRk=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
Expand Down
7 changes: 5 additions & 2 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ toolchain go1.23.4

require (
github.com/brianvoe/gofakeit/v7 v7.2.1
github.com/goravel/framework v1.15.2-0.20250201145925-219c1fd077ee
github.com/goravel/postgres v0.0.2-0.20250201151054-89092fd392cc
github.com/goravel/framework v1.15.2-0.20250202111117-cbb806d1e8de
github.com/goravel/mysql v0.0.0-20250202150522-4e41e0908557
github.com/goravel/postgres v0.0.2-0.20250202145044-bfeb31ead351
github.com/spf13/cast v1.7.1
github.com/stretchr/testify v1.10.0
gorm.io/gorm v1.25.12
Expand All @@ -24,6 +25,7 @@ require (
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dromara/carbon/v2 v2.5.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand Down Expand Up @@ -53,6 +55,7 @@ require (
google.golang.org/grpc v1.70.0 // indirect
google.golang.org/protobuf v1.36.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.5.7 // indirect
gorm.io/driver/postgres v1.5.11 // indirect
gorm.io/plugin/dbresolver v1.5.3 // indirect
)
10 changes: 6 additions & 4 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQ
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/goravel/framework v1.15.2-0.20250201145925-219c1fd077ee h1:qSjG7urPFSK3s0zTr6/tfmgd1Y2C03sgy2I96uYodwI=
github.com/goravel/framework v1.15.2-0.20250201145925-219c1fd077ee/go.mod h1:RQFLhQGde5giIl2Lx8RR7CobozbUnajiUGpDBfABhBw=
github.com/goravel/postgres v0.0.2-0.20250201151054-89092fd392cc h1:Cde34PxovADvPgzuYJKhrBcPHN9ZOBXDlAGpUhH62R8=
github.com/goravel/postgres v0.0.2-0.20250201151054-89092fd392cc/go.mod h1:QFHRAFJrU6egk8IMiOQO869g1JvOCyvpWbmGul4OaYY=
github.com/goravel/framework v1.15.2-0.20250202111117-cbb806d1e8de h1:U8dX7nxF+NeB7xgSeYiWMsgy76Qj2ngE79eg+JU6dNA=
github.com/goravel/framework v1.15.2-0.20250202111117-cbb806d1e8de/go.mod h1:2p1D3MvyV+Ft+iC627iQMxzjMDwHGckgy8B+EgtCbqA=
github.com/goravel/mysql v0.0.0-20250202150522-4e41e0908557 h1:Fl0e/GHo3QYTBmujmkTPp7mq3n1/XbeVKhRoNzglKXQ=
github.com/goravel/mysql v0.0.0-20250202150522-4e41e0908557/go.mod h1:HFrQicxhe6TBgdibbyKObz5r2kcLhfTCWHF+1+Jov+I=
github.com/goravel/postgres v0.0.2-0.20250202145044-bfeb31ead351 h1:cLiyIr7CRlCMulsRtduNegBNV1IxEfVCSm38JfFjDh8=
github.com/goravel/postgres v0.0.2-0.20250202145044-bfeb31ead351/go.mod h1:oCnSYJQl5AyTFGuBlSPMgtaWHQfyqH39ss7tB+mD9ek=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
Expand Down
2 changes: 2 additions & 0 deletions tests/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ func TestDefaultMigratorWithDBSuite(t *testing.T) {

func (s *DefaultMigratorWithDBSuite) SetupTest() {
postgresTestQuery := postgresTestQuery("goravel_", true)
mysqlTestQuery := mysqlTestQuery("goravel_", true)

s.driverToTestQuery = map[string]*TestQuery{
postgresTestQuery.Driver().Config().Driver: postgresTestQuery,
mysqlTestQuery.Driver().Config().Driver: mysqlTestQuery,
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func (s *OrmSuite) SetupSuite() {
connection := postgresTestQuery.Driver().Config().Connection
s.queries[connection] = postgresTestQuery
s.defaultConnection = connection

mysqlTestQuery := mysqlTestQuery("", false)
mysqlTestQuery.CreateTable(TestTableRoles)
connection = mysqlTestQuery.Driver().Config().Connection
s.queries[connection] = mysqlTestQuery
}

func (s *OrmSuite) SetupTest() {
Expand Down
2 changes: 1 addition & 1 deletion tests/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewTestQuery(ctx context.Context, driver driver.Driver, config config.Confi
func (r *TestQuery) CreateTable(testTables ...TestTable) {
driverName := r.driver.Config().Driver

for table, sql := range newTestTables(driverName).All() {
for table, sql := range newTestTables(driverName, r.Driver().Grammar()).All() {
if (len(testTables) == 0 && table != TestTableSchema) || slices.Contains(testTables, table) {
if _, err := r.query.Exec(sql()); err != nil {
panic(fmt.Sprintf("create table %v failed: %v", table, err))
Expand Down
24 changes: 11 additions & 13 deletions tests/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ import (
"github.com/goravel/framework/database/gorm"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/support/carbon"
"github.com/goravel/postgres"
"github.com/stretchr/testify/suite"
)

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

type QueryTestSuite struct {
suite.Suite
queries map[string]*TestQuery
Expand All @@ -36,6 +30,10 @@ func (s *QueryTestSuite) SetupSuite() {
postgresTestQuery := postgresTestQuery("", false)
postgresTestQuery.CreateTable()
s.queries[postgresTestQuery.Driver().Config().Driver] = postgresTestQuery

mysqlTestQuery := mysqlTestQuery("", false)
mysqlTestQuery.CreateTable()
s.queries[mysqlTestQuery.Driver().Config().Driver] = mysqlTestQuery
}

func (s *QueryTestSuite) SetupTest() {
Expand Down Expand Up @@ -2089,7 +2087,7 @@ func (s *QueryTestSuite) TestJoin() {

func (s *QueryTestSuite) TestLockForUpdate() {
for driver, query := range s.queries {
if driver != DriverSqlite {
if driver != "sqlite" {
s.Run(driver, func() {
user := User{Name: "lock_for_update_user"}
s.Nil(query.Query().Create(&user))
Expand Down Expand Up @@ -2963,7 +2961,7 @@ func (s *QueryTestSuite) TestSelect() {

func (s *QueryTestSuite) TestSharedLock() {
for driver, query := range s.queries {
if driver != DriverSqlite {
if driver != "sqlite" {
s.Run(driver, func() {
user := User{Name: "shared_lock_user"}
s.Nil(query.Query().Create(&user))
Expand Down Expand Up @@ -3091,9 +3089,9 @@ func (s *QueryTestSuite) TestToSql() {
for driver, query := range s.queries {
s.Run(driver, func() {
switch driver {
case DriverPostgres:
case postgres.Name:
s.Equal("SELECT * FROM \"users\" WHERE \"id\" = $1 AND \"users\".\"deleted_at\" IS NULL", query.Query().Where("id", 1).ToSql().Find(User{}))
case DriverSqlserver:
case "sqlserver":
s.Equal("SELECT * FROM \"users\" WHERE \"id\" = @p1 AND \"users\".\"deleted_at\" IS NULL", query.Query().Where("id", 1).ToSql().Find(User{}))
default:
s.Equal("SELECT * FROM `users` WHERE `id` = ? AND `users`.`deleted_at` IS NULL", query.Query().Where("id", 1).ToSql().Find(User{}))
Expand All @@ -3106,9 +3104,9 @@ func (s *QueryTestSuite) TestToRawSql() {
for driver, query := range s.queries {
s.Run(driver, func() {
switch driver {
case DriverPostgres:
case postgres.Name:
s.Equal("SELECT * FROM \"users\" WHERE \"id\" = 1 AND \"users\".\"deleted_at\" IS NULL", query.Query().Where("id", 1).ToRawSql().Find(User{}))
case DriverSqlserver:
case "sqlserver":
s.Equal("SELECT * FROM \"users\" WHERE \"id\" = $1$ AND \"users\".\"deleted_at\" IS NULL", query.Query().Where("id", 1).ToRawSql().Find(User{}))
default:
s.Equal("SELECT * FROM `users` WHERE `id` = 1 AND `users`.`deleted_at` IS NULL", query.Query().Where("id", 1).ToRawSql().Find(User{}))
Expand Down
3 changes: 2 additions & 1 deletion tests/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ func TestRepositoryTestSuite(t *testing.T) {

func (s *RepositoryTestSuite) SetupTest() {
postgresTestQuery := postgresTestQuery("goravel_", true)

mysqlTestQuery := mysqlTestQuery("goravel_", true)
s.driverToTestQuery = map[string]*TestQuery{
postgresTestQuery.Driver().Config().Driver: postgresTestQuery,
mysqlTestQuery.Driver().Config().Driver: mysqlTestQuery,
}
}

Expand Down
31 changes: 13 additions & 18 deletions tests/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
contractsschema "github.com/goravel/framework/contracts/database/schema"
databaseschema "github.com/goravel/framework/database/schema"
"github.com/goravel/framework/support/carbon"
"github.com/goravel/mysql"
"github.com/goravel/postgres"
"github.com/spf13/cast"
"github.com/stretchr/testify/assert"
Expand All @@ -31,7 +32,9 @@ func TestSchemaSuite(t *testing.T) {
func (s *SchemaSuite) SetupTest() {
s.prefix = "goravel_"
postgresTestQuery := postgresTestQuery(s.prefix, true)
mysqlTestQuery := mysqlTestQuery(s.prefix, true)
s.driverToTestQuery[postgresTestQuery.Driver().Config().Driver] = postgresTestQuery
s.driverToTestQuery[mysqlTestQuery.Driver().Config().Driver] = mysqlTestQuery
}

func (s *SchemaSuite) TearDownTest() {
Expand Down Expand Up @@ -221,8 +224,7 @@ func (s *SchemaSuite) TestColumnExtraAttributes() {
s.NoError(testQuery.Query().Where("id", columnExtraAttribute.ID).First(&anotherColumnExtraAttribute))
s.Equal("world", anotherColumnExtraAttribute.Name)
s.Equal(columnExtraAttribute.UseCurrent, anotherColumnExtraAttribute.UseCurrent)
// TODO Replace with mysql.Name
if driver == "mysql" {
if driver == mysql.Name {
s.NotEqual(columnExtraAttribute.UseCurrentOnUpdate, anotherColumnExtraAttribute.UseCurrentOnUpdate)
s.True(anotherColumnExtraAttribute.UseCurrentOnUpdate.Between(now, carbon.Now().AddSecond()))
} else {
Expand Down Expand Up @@ -893,12 +895,11 @@ func (s *SchemaSuite) TestColumnTypes_Sqlite() {
}

func (s *SchemaSuite) TestColumnTypes_Mysql() {
// TODO Replace with mysql.Name
if s.driverToTestQuery["mysql"] == nil {
if s.driverToTestQuery[mysql.Name] == nil {
s.T().Skip("Skip test")
}

testQuery := s.driverToTestQuery["mysql"]
testQuery := s.driverToTestQuery[mysql.Name]
schema := newSchema(testQuery, s.driverToTestQuery)
table := "mysql_columns"
s.createTableAndAssertColumnsForColumnMethods(schema, table)
Expand Down Expand Up @@ -1628,12 +1629,11 @@ func (s *SchemaSuite) TestEnum_Sqlite() {
}

func (s *SchemaSuite) TestEnum_Mysql() {
// TODO Replace with mysql.Name
if s.driverToTestQuery["mysql"] == nil {
if s.driverToTestQuery[mysql.Name] == nil {
s.T().Skip("Skip test")
}

testQuery := s.driverToTestQuery["mysql"]
testQuery := s.driverToTestQuery[mysql.Name]
schema := newSchema(testQuery, s.driverToTestQuery)
table := "mysql_enum"

Expand Down Expand Up @@ -1803,8 +1803,7 @@ func (s *SchemaSuite) TestFullText() {

s.Require().Nil(err)

// TODO Replace with mysql.Name
if driver == "msyql" || driver == postgres.Name {
if driver == mysql.Name || driver == postgres.Name {
s.True(schema.HasIndex(table, "goravel_fulltext_name_fulltext"))
s.True(schema.HasIndex(table, "fulltext_avatar_fulltext"))
} else {
Expand Down Expand Up @@ -1840,8 +1839,7 @@ func (s *SchemaSuite) TestPrimary() {
if driver == postgres.Name {
s.Require().True(schema.HasIndex(table, "goravel_primaries_pkey"))
}
// TODO Replace with mysql.Name
if driver == "mysql" {
if driver == mysql.Name {
s.Require().True(schema.HasIndex(table, "primary"))
}
// TODO Replace with sqlserver.Name
Expand All @@ -1855,8 +1853,7 @@ func (s *SchemaSuite) TestPrimary() {
if driver == postgres.Name {
s.Require().False(schema.HasIndex(table, "goravel_primaries_pkey"))
}
// TODO Replace with mysql.Name
if driver == "mysql" {
if driver == mysql.Name {
s.Require().False(schema.HasIndex(table, "primary"))
}
// TODO Replace with sqlserver.Name
Expand Down Expand Up @@ -2039,12 +2036,11 @@ func (s *SchemaSuite) TestID_Sqlite() {
}

func (s *SchemaSuite) TestID_Mysql() {
// TODO Replace with mysql.Name
if s.driverToTestQuery["mysql"] == nil {
if s.driverToTestQuery[mysql.Name] == nil {
s.T().Skip("Skip test")
}

testQuery := s.driverToTestQuery["mysql"]
testQuery := s.driverToTestQuery[mysql.Name]
schema := newSchema(testQuery, s.driverToTestQuery)

tests := []struct {
Expand Down Expand Up @@ -2317,7 +2313,6 @@ func (s *SchemaSuite) TestSql() {

func (s *SchemaSuite) TestTypeMethods() {
for driver, testQuery := range s.driverToTestQuery {
// TODO Add other drivers
if driver != postgres.Name {
continue
}
Expand Down
Loading