Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Mar 17, 2022
1 parent 166affc commit 01b7aae
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 44 deletions.
20 changes: 7 additions & 13 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,15 @@ import (
)

type connection struct {
Name string
Dialect *Dialect
DB *sql.DB
Schemas map[string]*schema
DBSchema map[string][]columnSpec
Name string
Dialect *Dialect
DB *sql.DB
Schemas map[string]*schema
DBSchema map[string][]columnSpec
ValidateTablesExistence bool
ValidateTablesSchemas bool
}

func (c *connection) validateDatabaseSchema() error {
err := c.validateAllTablesArePresent()
if err != nil {
return err
}

return nil
}
func (c *connection) inferedTables() []string {
var tables []string
for t, s := range c.Schemas {
Expand Down
52 changes: 33 additions & 19 deletions orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,50 @@ type ConnectionConfig struct {
// information that we can provide you and also potentialy validations that we
// can do with the database
Entities []Entity
// Check if all infered tables exist in your database
ValidateTablesExistence bool
// Check if columns we infered are in sync with your database.
ValidateTablesSchemas bool
}

// SetupConnections declares a new connections for ORM.
func SetupConnections(configs ...ConnectionConfig) error {
// configure logger
var err error
if err != nil {
return err
}
// Setup declares a new connections for ORM.
func Setup(configs ...ConnectionConfig) error {

for _, c := range configs {
if err = setupConnection(c); err != nil {
if err := setupConnection(c); err != nil {
return err
}
}
for _, conn := range globalConnections {
if !conn.ValidateTablesExistence && !conn.ValidateTablesSchemas {
continue
}

tables, err := getListOfTables(conn.Dialect.QueryListTables)(conn.DB)
if err != nil {
return err
}

for _, table := range tables {
spec, err := getTableSchema(conn.Dialect.QueryTableSchema)(conn.DB, table)
if conn.ValidateTablesSchemas {
spec, err := getTableSchema(conn.Dialect.QueryTableSchema)(conn.DB, table)
if err != nil {
return err
}
conn.DBSchema[table] = spec
} else {
conn.DBSchema[table] = nil
}
}

// check tables existence
if conn.ValidateTablesExistence {
err := conn.validateAllTablesArePresent()
if err != nil {
return err
}
conn.DBSchema[table] = spec
}

err = conn.validateDatabaseSchema()
if err != nil {
return err
}
}

return nil
Expand All @@ -93,11 +105,13 @@ func setupConnection(config ConnectionConfig) error {
}

s := &connection{
Name: config.Name,
DB: config.DB,
Dialect: config.Dialect,
Schemas: schemas,
DBSchema: make(map[string][]columnSpec),
Name: config.Name,
DB: config.DB,
Dialect: config.Dialect,
Schemas: schemas,
DBSchema: make(map[string][]columnSpec),
ValidateTablesExistence: config.ValidateTablesExistence,
ValidateTablesSchemas: config.ValidateTablesSchemas,
}

globalConnections[fmt.Sprintf("%s", config.Name)] = s
Expand Down
41 changes: 30 additions & 11 deletions orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,35 @@ func setup() error {
return err
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS posts (id INTEGER PRIMARY KEY, body text, created_at TIMESTAMP, updated_at TIMESTAMP, deleted_at TIMESTAMP)`)
if err != nil {
return err
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS emails (id INTEGER PRIMARY KEY, post_id INTEGER, email text)`)
if err != nil {
return err
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS header_pictures (id INTEGER PRIMARY KEY, post_id INTEGER, link text)`)
if err != nil {
return err
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS comments (id INTEGER PRIMARY KEY, post_id INTEGER, body text)`)
if err != nil {
return err
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS categories (id INTEGER PRIMARY KEY, title text)`)
if err != nil {
return err
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS post_categories (post_id INTEGER, category_id INTEGER, PRIMARY KEY(post_id, category_id))`)

return orm.SetupConnections(orm.ConnectionConfig{
Name: "default",
DB: db,
Dialect: orm.Dialects.SQLite3,
Entities: []orm.Entity{&Post{}, &Comment{}, &Category{}, &HeaderPicture{}},
if err != nil {
return err
}
return orm.Setup(orm.ConnectionConfig{
Name: "default",
DB: db,
Dialect: orm.Dialects.SQLite3,
Entities: []orm.Entity{&Post{}, &Comment{}, &Category{}, &HeaderPicture{}},
ValidateTablesExistence: true,
})
}

Expand Down Expand Up @@ -498,11 +516,12 @@ func TestSetup(t *testing.T) {
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS categories (id INTEGER PRIMARY KEY, title text)`)
// _, err = db.Exec(`CREATE TABLE IF NOT EXISTS post_categories (post_id INTEGER, category_id INTEGER, PRIMARY KEY(post_id, category_id))`)

err = orm.SetupConnections(orm.ConnectionConfig{
Name: "default",
DB: db,
Dialect: orm.Dialects.SQLite3,
Entities: []orm.Entity{&Post{}, &Comment{}, &Category{}, &HeaderPicture{}},
err = orm.Setup(orm.ConnectionConfig{
Name: "default",
DB: db,
Dialect: orm.Dialects.SQLite3,
Entities: []orm.Entity{&Post{}, &Comment{}, &Category{}, &HeaderPicture{}},
ValidateTablesExistence: true,
})
assert.Error(t, err)
}
2 changes: 1 addition & 1 deletion schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func setup(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
err = SetupConnections(ConnectionConfig{
err = Setup(ConnectionConfig{
Name: "default",
DB: db,
Dialect: Dialects.SQLite3,
Expand Down

0 comments on commit 01b7aae

Please sign in to comment.