Skip to content

Commit

Permalink
update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 18, 2024
1 parent 33bbfbc commit c273226
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 176 deletions.
32 changes: 17 additions & 15 deletions database/schema/mysql_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,25 @@ func (r *MysqlSchema) DropAllTables() error {
return nil
}

if _, err = r.orm.Query().Exec(r.grammar.CompileDisableForeignKeyConstraints()); err != nil {
return err
}
return r.orm.Transaction(func(tx orm.Query) error {
if _, err = tx.Exec(r.grammar.CompileDisableForeignKeyConstraints()); err != nil {
return err
}

Check warning on line 42 in database/schema/mysql_schema.go

View check run for this annotation

Codecov / codecov/patch

database/schema/mysql_schema.go#L41-L42

Added lines #L41 - L42 were not covered by tests

var dropTables []string
for _, table := range tables {
dropTables = append(dropTables, table.Name)
}
if _, err = tx.Exec(r.grammar.CompileDropAllTables(dropTables)); err != nil {
return err
}

Check warning on line 50 in database/schema/mysql_schema.go

View check run for this annotation

Codecov / codecov/patch

database/schema/mysql_schema.go#L49-L50

Added lines #L49 - L50 were not covered by tests

if _, err = tx.Exec(r.grammar.CompileEnableForeignKeyConstraints()); err != nil {
return err
}

Check warning on line 54 in database/schema/mysql_schema.go

View check run for this annotation

Codecov / codecov/patch

database/schema/mysql_schema.go#L53-L54

Added lines #L53 - L54 were not covered by tests

var dropTables []string
for _, table := range tables {
dropTables = append(dropTables, table.Name)
}
if _, err = r.orm.Query().Exec(r.grammar.CompileDropAllTables(dropTables)); err != nil {
return err
}

if _, err = r.orm.Query().Exec(r.grammar.CompileEnableForeignKeyConstraints()); err != nil {
return err
}

return err
})
}

func (r *MysqlSchema) DropAllTypes() error {
Expand Down
43 changes: 17 additions & 26 deletions database/schema/postgres_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package schema

import (
"fmt"
"slices"
"strings"

"github.com/goravel/framework/contracts/database/orm"
contractsschema "github.com/goravel/framework/contracts/database/schema"
"github.com/goravel/framework/database/schema/grammars"
"github.com/goravel/framework/database/schema/processors"
"slices"
)

type PostgresSchema struct {
Expand Down Expand Up @@ -81,19 +79,21 @@ func (r *PostgresSchema) DropAllTypes() error {
}
}

if len(dropTypes) > 0 {
if _, err := r.orm.Query().Exec(r.grammar.CompileDropAllTypes(dropTypes)); err != nil {
return err
return r.orm.Transaction(func(tx orm.Query) error {
if len(dropTypes) > 0 {
if _, err := tx.Exec(r.grammar.CompileDropAllTypes(dropTypes)); err != nil {
return err
}

Check warning on line 86 in database/schema/postgres_schema.go

View check run for this annotation

Codecov / codecov/patch

database/schema/postgres_schema.go#L82-L86

Added lines #L82 - L86 were not covered by tests
}
}

if len(dropDomains) > 0 {
if _, err := r.orm.Query().Exec(r.grammar.CompileDropAllDomains(dropDomains)); err != nil {
return err
if len(dropDomains) > 0 {
if _, err := tx.Exec(r.grammar.CompileDropAllDomains(dropDomains)); err != nil {
return err
}

Check warning on line 92 in database/schema/postgres_schema.go

View check run for this annotation

Codecov / codecov/patch

database/schema/postgres_schema.go#L89-L92

Added lines #L89 - L92 were not covered by tests
}
}

return nil
return nil

Check warning on line 95 in database/schema/postgres_schema.go

View check run for this annotation

Codecov / codecov/patch

database/schema/postgres_schema.go#L95

Added line #L95 was not covered by tests
})
}

func (r *PostgresSchema) DropAllViews() error {
Expand Down Expand Up @@ -121,7 +121,11 @@ func (r *PostgresSchema) DropAllViews() error {
}

func (r *PostgresSchema) GetIndexes(table string) ([]contractsschema.Index, error) {
schema, table := r.parseSchemaAndTable(table)
schema, table, err := parseSchemaAndTable(table, r.schema)
if err != nil {
return nil, err
}

Check warning on line 127 in database/schema/postgres_schema.go

View check run for this annotation

Codecov / codecov/patch

database/schema/postgres_schema.go#L126-L127

Added lines #L126 - L127 were not covered by tests

table = r.prefix + table

var dbIndexes []processors.DBIndex
Expand All @@ -140,16 +144,3 @@ func (r *PostgresSchema) GetTypes() ([]contractsschema.Type, error) {

return r.processor.ProcessTypes(types), nil
}

func (r *PostgresSchema) parseSchemaAndTable(reference string) (schema, table string) {
parts := strings.Split(reference, ".")
schema = r.schema
if len(parts) == 2 {
schema = parts[0]
parts = parts[1:]
}

table = parts[0]

return
}
59 changes: 0 additions & 59 deletions database/schema/postgres_schema_test.go

This file was deleted.

28 changes: 15 additions & 13 deletions database/schema/sqlite_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,22 @@ func (r *SqliteSchema) DropAllTypes() error {
}

func (r *SqliteSchema) DropAllViews() error {
if _, err := r.orm.Query().Exec(r.grammar.CompileEnableWriteableSchema()); err != nil {
return err
}
if _, err := r.orm.Query().Exec(r.grammar.CompileDropAllViews(nil)); err != nil {
return err
}
if _, err := r.orm.Query().Exec(r.grammar.CompileDisableWriteableSchema()); err != nil {
return err
}
if _, err := r.orm.Query().Exec(r.grammar.CompileRebuild()); err != nil {
return err
}
return r.orm.Transaction(func(tx orm.Query) error {
if _, err := tx.Exec(r.grammar.CompileEnableWriteableSchema()); err != nil {
return err
}
if _, err := tx.Exec(r.grammar.CompileDropAllViews(nil)); err != nil {
return err
}
if _, err := tx.Exec(r.grammar.CompileDisableWriteableSchema()); err != nil {
return err
}
if _, err := tx.Exec(r.grammar.CompileRebuild()); err != nil {
return err
}

Check warning on line 63 in database/schema/sqlite_schema.go

View check run for this annotation

Codecov / codecov/patch

database/schema/sqlite_schema.go#L51-L63

Added lines #L51 - L63 were not covered by tests

return nil
return nil

Check warning on line 65 in database/schema/sqlite_schema.go

View check run for this annotation

Codecov / codecov/patch

database/schema/sqlite_schema.go#L65

Added line #L65 was not covered by tests
})
}

func (r *SqliteSchema) GetIndexes(table string) ([]schema.Index, error) {
Expand Down
20 changes: 5 additions & 15 deletions database/schema/sqlserver_schema.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package schema

import (
"strings"

"github.com/goravel/framework/contracts/database/orm"
contractsschema "github.com/goravel/framework/contracts/database/schema"
"github.com/goravel/framework/database/schema/grammars"
Expand Down Expand Up @@ -51,7 +49,11 @@ func (r *SqlserverSchema) DropAllViews() error {
}

func (r *SqlserverSchema) GetIndexes(table string) ([]contractsschema.Index, error) {
schema, table := r.parseSchemaAndTable(table)
schema, table, err := parseSchemaAndTable(table, "")
if err != nil {
return nil, err
}

Check warning on line 55 in database/schema/sqlserver_schema.go

View check run for this annotation

Codecov / codecov/patch

database/schema/sqlserver_schema.go#L54-L55

Added lines #L54 - L55 were not covered by tests

table = r.prefix + table

var dbIndexes []processors.DBIndex
Expand All @@ -65,15 +67,3 @@ func (r *SqlserverSchema) GetIndexes(table string) ([]contractsschema.Index, err
func (r *SqlserverSchema) GetTypes() ([]contractsschema.Type, error) {
return nil, nil

Check warning on line 68 in database/schema/sqlserver_schema.go

View check run for this annotation

Codecov / codecov/patch

database/schema/sqlserver_schema.go#L67-L68

Added lines #L67 - L68 were not covered by tests
}

func (r *SqlserverSchema) parseSchemaAndTable(reference string) (schema, table string) {
parts := strings.Split(reference, ".")
if len(parts) == 2 {
schema = parts[0]
parts = parts[1:]
}

table = parts[0]

return
}
44 changes: 0 additions & 44 deletions database/schema/sqserver_schema_test.go

This file was deleted.

28 changes: 28 additions & 0 deletions database/schema/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package schema

import (
"strings"

"github.com/goravel/framework/errors"
)

func parseSchemaAndTable(reference, defaultSchema string) (string, string, error) {
if reference == "" {
return "", "", errors.SchemaEmptyReferenceString
}

parts := strings.Split(reference, ".")
if len(parts) > 2 {
return "", "", errors.SchemaErrorReferenceFormat
}

schema := defaultSchema
if len(parts) == 2 {
schema = parts[0]
parts = parts[1:]
}

table := parts[0]

return schema, table, nil
}
30 changes: 30 additions & 0 deletions database/schema/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package schema

import (
"testing"

"github.com/goravel/framework/errors"
"github.com/stretchr/testify/assert"
)

func TestParseSchemaAndTable(t *testing.T) {
tests := []struct {
reference string
defaultSchema string
expectedSchema string
expectedTable string
expectedError error
}{
{"public.users", "public", "public", "users", nil},
{"users", "goravel", "goravel", "users", nil},
{"", "", "", "", errors.SchemaEmptyReferenceString},
{"public.users.extra", "", "", "", errors.SchemaErrorReferenceFormat},
}

for _, test := range tests {
schema, table, err := parseSchemaAndTable(test.reference, test.defaultSchema)
assert.Equal(t, test.expectedSchema, schema)
assert.Equal(t, test.expectedTable, table)
assert.Equal(t, test.expectedError, err)
}
}
10 changes: 6 additions & 4 deletions errors/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ var (
RouteDefaultDriverNotSet = New("please set default driver")
RouteInvalidDriver = New("init %s route driver fail: route must be implement route.Route or func() (route.Route, error)")

SchemaDriverNotSupported = New("driver %s is not supported")
SchemaFailedToCreateTable = New("failed to create %s table: %v")
SchemaFailedToDropTable = New("failed to drop %s table: %v")
SchemaFailedToGetTables = New("failed to get %s tables: %v")
SchemaDriverNotSupported = New("driver %s is not supported")
SchemaFailedToCreateTable = New("failed to create %s table: %v")
SchemaFailedToDropTable = New("failed to drop %s table: %v")
SchemaFailedToGetTables = New("failed to get %s tables: %v")
SchemaEmptyReferenceString = New("reference string can't be empty")
SchemaErrorReferenceFormat = New("invalid format: too many dots in reference")

SessionDriverAlreadyExists = New("session driver [%s] already exists")
SessionDriverExtensionFailed = New("session failed to extend session [%s] driver [%v]")
Expand Down

0 comments on commit c273226

Please sign in to comment.