Skip to content

Commit

Permalink
Database validations use single flag, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Mar 21, 2022
1 parent f497544 commit e8033a2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 28 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,20 @@ DELETE FROM [table name] WHERE [cond1 AND/OR cond2 AND/OR ...]
Same as Select and Update.
##### Where
Same as Select and Update.
### Database Validations
Golobby ORM can validate your database state and compare it to your entities and if your database and code are not in sync give you error.
Currently there are two database validations possible:
1. Validate all necessary tables exists.
2. Validate all tables contain necessary columns.
You can enable database validations feature by enabling `DatabaseValidations` flag in your ConnectionConfig.
```go
return orm.SetupConnections(orm.ConnectionConfig{
Name: "default",
DB: db,
Dialect: orm.Dialects.SQLite3,
Entities: []orm.Entity{&Post{}, &Comment{}, &Category{}, &HeaderPicture{}},
DatabaseValidations: true,
})
```
## License

GoLobby ORM is released under the [MIT License](http:// opensource.org/licenses/mit-license.php).
GoLobby ORM is released under the [MIT License](http://opensource.org/licenses/mit-license.php).
1 change: 1 addition & 0 deletions configurators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package orm

import (
"database/sql"

"github.com/gertd/go-pluralize"
)

Expand Down
3 changes: 1 addition & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ type connection struct {
DB *sql.DB
Schemas map[string]*schema
DBSchema map[string][]columnSpec
ValidateTablesExistence bool
ValidateTablesSchemas bool
DatabaseValidations bool
}

func (c *connection) inferedTables() []string {
Expand Down
16 changes: 7 additions & 9 deletions orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ type ConnectionConfig struct {
// information that we can provide you and also potentialy validations that we
// can do with the database
Entities []Entity
// Database validations, check if all tables exists and also table schemas contains all necessary columns.
// Check if all infered tables exist in your database
ValidateTablesExistence bool
// Check if columns we infered are in sync with your database.
ValidateTablesSchemas bool
DatabaseValidations bool
}

// SetupConnections declares a new connections for ORM.
Expand All @@ -57,7 +56,7 @@ func SetupConnections(configs ...ConnectionConfig) error {
}
}
for _, conn := range globalConnections {
if !conn.ValidateTablesExistence && !conn.ValidateTablesSchemas {
if !conn.DatabaseValidations {
continue
}

Expand All @@ -67,7 +66,7 @@ func SetupConnections(configs ...ConnectionConfig) error {
}

for _, table := range tables {
if conn.ValidateTablesSchemas {
if conn.DatabaseValidations {
spec, err := getTableSchema(conn.Dialect.QueryTableSchema)(conn.DB, table)
if err != nil {
return err
Expand All @@ -79,14 +78,14 @@ func SetupConnections(configs ...ConnectionConfig) error {
}

// check tables existence
if conn.ValidateTablesExistence {
if conn.DatabaseValidations {
err := conn.validateAllTablesArePresent()
if err != nil {
return err
}
}

if conn.ValidateTablesSchemas {
if conn.DatabaseValidations {
err = conn.validateTablesSchemas()
if err != nil {
return err
Expand Down Expand Up @@ -117,8 +116,7 @@ func setupConnection(config ConnectionConfig) error {
Dialect: config.Dialect,
Schemas: schemas,
DBSchema: make(map[string][]columnSpec),
ValidateTablesExistence: config.ValidateTablesExistence,
ValidateTablesSchemas: config.ValidateTablesSchemas,
DatabaseValidations: config.DatabaseValidations,
}

globalConnections[fmt.Sprintf("%s", config.Name)] = s
Expand Down
9 changes: 3 additions & 6 deletions orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ func setup() error {
DB: db,
Dialect: orm.Dialects.SQLite3,
Entities: []orm.Entity{&Post{}, &Comment{}, &Category{}, &HeaderPicture{}},
ValidateTablesExistence: true,
ValidateTablesSchemas: true,
DatabaseValidations: true,
})
}

Expand Down Expand Up @@ -523,8 +522,7 @@ func TestSetup(t *testing.T) {
DB: db,
Dialect: orm.Dialects.SQLite3,
Entities: []orm.Entity{&Post{}, &Comment{}, &Category{}, &HeaderPicture{}},
ValidateTablesExistence: true,
ValidateTablesSchemas: true,
DatabaseValidations: true,
})
assert.Error(t, err)

Expand All @@ -543,8 +541,7 @@ func TestSetup(t *testing.T) {
DB: db,
Dialect: orm.Dialects.SQLite3,
Entities: []orm.Entity{&Post{}, &Comment{}, &Category{}, &HeaderPicture{}},
ValidateTablesExistence: true,
ValidateTablesSchemas: true,
DatabaseValidations: true,
})
assert.Error(t, err)

Expand Down
18 changes: 9 additions & 9 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,22 @@ func genericSet(obj Entity, name string, value interface{}) {
val.(reflect.Value).Set(reflect.ValueOf(value))
}
func schemaOfHeavyReflectionStuff(v Entity) *schema {
userSchema := newEntityConfigurator()
v.ConfigureEntity(userSchema)
for _, relation := range userSchema.resolveRelations {
userEntityConfigurator := newEntityConfigurator()
v.ConfigureEntity(userEntityConfigurator)
for _, relation := range userEntityConfigurator.resolveRelations {
relation()
}
schema := &schema{}
if userSchema.connection != "" {
schema.Connection = userSchema.connection
if userEntityConfigurator.connection != "" {
schema.Connection = userEntityConfigurator.connection
}
if userSchema.table != "" {
schema.Table = userSchema.table
if userEntityConfigurator.table != "" {
schema.Table = userEntityConfigurator.table
} else {
panic("you need to have table name for getting schema.")
}

schema.columnConstraints = userSchema.columnConstraints
schema.columnConstraints = userEntityConfigurator.columnConstraints
if schema.Connection == "" {
schema.Connection = "default"
}
Expand All @@ -270,7 +270,7 @@ func schemaOfHeavyReflectionStuff(v Entity) *schema {
schema.setPK = genericSetPkValue
}

schema.relations = userSchema.relations
schema.relations = userEntityConfigurator.relations

return schema
}
Expand Down

0 comments on commit e8033a2

Please sign in to comment.