Skip to content

Commit

Permalink
settings
Browse files Browse the repository at this point in the history
  • Loading branch information
iesreza committed Mar 9, 2024
1 parent 635fc0a commit a15291b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 19 additions & 10 deletions lib/settings/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package database

import (
"errors"

"github.com/getevo/evo/v2/lib/args"
"github.com/getevo/evo/v2/lib/db"
"github.com/getevo/evo/v2/lib/log"
Expand Down Expand Up @@ -170,30 +169,38 @@ func (config *Database) Register(sets ...any) error {
var portions = strings.SplitN(domain.Domain, ".", 2)
var parentDomain SettingDomain
var grandfatherDomainId uint
var skip = false
switch len(portions) {
case 1:
err := db.Model(&SettingDomain{}).Where("parent_domain = 0 AND domain = ?", portions[0]).First(&parentDomain).Error
err := db.Debug().Model(&SettingDomain{}).Where("(parent_domain = 0 OR parent_domain IS NULL) AND domain = ?", portions[0]).First(&parentDomain).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
domain.Domain = portions[0]
parentDomain = domain
grandfatherDomainId = parentDomain.ID
skip = true
break
case 2:
err := db.Model(&SettingDomain{}).Preload("ChildrenDomains", "Domain = ?", portions[1]).Preload("ChildrenDomains.Parameters").Where("parent_domain = 0 AND domain = ?", portions[0]).First(&parentDomain).Error
err := db.Debug().Model(&SettingDomain{}).Preload("ChildrenDomains", "Domain = ?", portions[1]).Preload("ChildrenDomains.Parameters").Where("(parent_domain = 0 or parent_domain IS NULL) AND domain = ?", portions[0]).First(&parentDomain).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
domain.Domain = portions[1]
parentDomain.ChildrenDomains = append(parentDomain.ChildrenDomains, domain)
parentDomain.Domain = portions[0]
grandfatherDomainId = 0
skip = true
break
}
err = db.Clauses(clause.Insert{Modifier: "IGNORE"}).Where("domain = ? AND parent_domain = ?", parentDomain.Domain, grandfatherDomainId).Save(&parentDomain).Error
if err != nil {
return err
if parentDomain.ParentDomain != nil && *parentDomain.ParentDomain == 0 {
parentDomain.ParentDomain = nil
}
if !skip {
err = db.Debug().Clauses(clause.Insert{Modifier: "IGNORE"}).Where("domain = ? AND parent_domain = ?", parentDomain.Domain, grandfatherDomainId).Save(&parentDomain).Error
if err != nil {
return err
}
}

}
Expand Down Expand Up @@ -228,7 +235,7 @@ func (config *Database) Register(sets ...any) error {

switch len(portions) {
case 1:
err := db.Model(&SettingDomain{}).Where("parent_domain = 0 AND domain = ?", portions[0]).First(&parentDomain).Error
err := db.Model(&SettingDomain{}).Where("(parent_domain = 0 OR parent_domain IS NULL) AND domain = ?", portions[0]).First(&parentDomain).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
Expand Down Expand Up @@ -258,7 +265,9 @@ func (config *Database) Register(sets ...any) error {
}
break
}

if parentDomain.ParentDomain != nil && *parentDomain.ParentDomain != 0 {
parentDomain.ParentDomain = nil
}
// Now we can finally create the setting parameter, subdomain and domain in one go
err = db.Clauses(clause.Insert{Modifier: "IGNORE"}).Where("parent_domain = 0 AND domain = ?", parentDomain.Domain).Save(&parentDomain).Error
if err != nil {
Expand All @@ -274,7 +283,7 @@ func (config *Database) Register(sets ...any) error {
}
func (config *Database) Init(params ...string) error {
// Check if the database structure is the old one or the new one
if err := db.Model(&SettingDomain{}).Where("parent_domain = 0").First(&SettingDomain{}).Error; err == nil {
if err := db.Model(&SettingDomain{}).Where("parent_domain = 0 OR parent_domain IS NULL").First(&SettingDomain{}).Error; err == nil {
log.Info("new db version detected")
dbVersion = 1
} else {
Expand Down Expand Up @@ -312,7 +321,7 @@ func (config *Database) Init(params ...string) error {
config.data1 = make(map[string]generic.Value)
var items []SettingDomain
// This preload limits the depth of children domains to 2 level, domain, subdomain and parameter
err := db.Model(&SettingDomain{}).Preload("Parameters").Preload("ChildrenDomains").Preload("ChildrenDomains.Parameters").Where("parent_domain = 0").Find(&items).Error
err := db.Model(&SettingDomain{}).Preload("Parameters").Preload("ChildrenDomains").Preload("ChildrenDomains.Parameters").Where("parent_domain = 0 OR parent_domain IS NULL").Find(&items).Error
if err != nil {
log.Error("Error while loading settings from db", err)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/settings/database/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type SettingDomain struct {
Description string `gorm:"column:description" json:"description"`
ReadOnly bool `gorm:"column:read_only" json:"read_only"`
Visible bool `gorm:"column:visible" json:"visible"`
ParentDomain *uint `gorm:"column:parent_domain;index:key,unique;default:0" json:"parent_domain"`
ParentDomain *uint `gorm:"column:parent_domain;index:key,unique;default:NULL" json:"parent_domain"`
ChildrenDomains []SettingDomain `gorm:"foreignkey:ParentDomain;references:ID" json:"children_domains"`
Parameters []Setting `gorm:"foreignkey:DomainID;references:ID" json:"parameters"`
CreatedAt time.Time `json:"-"`
Expand Down

0 comments on commit a15291b

Please sign in to comment.