feat: Sites feature backend
This commit is contained in:
1 parent
45fcbd890d
commit
a11338094e
98 files changed
+5365
-214
No files matched your search
@@ -6,15 +6,18 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Migrate(db *gorm.DB) error {
|
||||
err := tableOptionsFor(db).AutoMigrate(&models.Article{}, &models.Image{}, &models.User{}, &models.Invite{}, &models.APIKey{}, &models.PurgeSettings{}, &models.AuditEvent{}, &models.AuditSettings{}, &models.ImageVariant{}, &models.ImageVariantFile{}, &models.ImageVariantRejectionDay{})
|
||||
func Migrate(db *gorm.DB, publicURL string) error {
|
||||
err := tableOptionsFor(db).AutoMigrate(&models.Article{}, &models.Image{}, &models.User{}, &models.Invite{}, &models.APIKey{}, &models.PurgeSettings{}, &models.AuditEvent{}, &models.AuditSettings{}, &models.ImageVariant{}, &models.ImageVariantFile{}, &models.ImageVariantRejectionDay{}, &models.Site{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ensureImagePathIndex(db); err != nil {
|
||||
return err
|
||||
}
|
||||
return BackfillUserRoles(db)
|
||||
if err := BackfillUserRoles(db); err != nil {
|
||||
return err
|
||||
}
|
||||
return SeedDefaultSite(db, publicURL)
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
domainSite "RenewCMS/internal/domain/site"
|
||||
entity "RenewCMS/internal/infrastructure/persistence/models"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func SeedDefaultSite(db *gorm.DB, publicURL string) error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
defaultSite, err := ensureDefaultSite(tx, publicURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Unscoped().Model(&entity.Article{}).Where("site_id IS NULL OR site_id = 0").UpdateColumn("site_id", defaultSite.ID).Error
|
||||
})
|
||||
}
|
||||
|
||||
func ensureDefaultSite(tx *gorm.DB, publicURL string) (entity.Site, error) {
|
||||
var total int64
|
||||
if err := tx.Model(&entity.Site{}).Count(&total).Error; err != nil {
|
||||
return entity.Site{}, err
|
||||
}
|
||||
if total > 0 {
|
||||
return firstDefaultSite(tx)
|
||||
}
|
||||
baseURL, err := domainSite.SeedBaseURL(publicURL)
|
||||
if err != nil {
|
||||
return entity.Site{}, fmt.Errorf("PUBLIC_URL %q cannot seed the first site: %w", publicURL, err)
|
||||
}
|
||||
seeded := entity.Site{
|
||||
Name: domainSite.DefaultName,
|
||||
Key: domainSite.DefaultKey,
|
||||
BaseURL: baseURL,
|
||||
DefaultRobots: domainSite.RobotsIndex,
|
||||
IsDefault: true,
|
||||
}
|
||||
if err := tx.Create(&seeded).Error; err != nil {
|
||||
return entity.Site{}, err
|
||||
}
|
||||
return seeded, nil
|
||||
}
|
||||
|
||||
func firstDefaultSite(tx *gorm.DB) (entity.Site, error) {
|
||||
var found entity.Site
|
||||
if err := tx.Order("is_default DESC, id ASC").First(&found).Error; err != nil {
|
||||
return entity.Site{}, err
|
||||
}
|
||||
return found, nil
|
||||
}
|
||||
Reference in new issue
Block a user