docs: add some comment

This commit is contained in:
Laisky.Cai 2021-09-03 06:05:28 +00:00
parent c6511a0dc6
commit 4d291f68b9
3 changed files with 26 additions and 6 deletions

View File

@ -14,9 +14,27 @@ type Model struct {
DeletedAt DeletedAt `gorm:"index"` DeletedAt DeletedAt `gorm:"index"`
} }
// UnDelete clear model's deleted info, you need save this model manually
func (m *Model) UnDelete() {
m.DeletedAt.Valid = false
}
// ModelSupportUnique model that support soft delete and unique index
//
// For example
//
// when you annouce an unique index for column A,
// gorm will automate create a composite index for A & deleted_flag.
//
// create: deleted_flag default to 0
// delete: set deleted_flag's value to primary id to avoid unique conflict
type ModelSupportUnique struct { type ModelSupportUnique struct {
ID uint `gorm:"primarykey"` Model
CreatedAt time.Time
UpdatedAt time.Time
DeletedFlag DeletedFlag `gorm:"type:BIGINT UNSIGNED NOT NULL DEFAULT 0" json:"deleted_flag"` DeletedFlag DeletedFlag `gorm:"type:BIGINT UNSIGNED NOT NULL DEFAULT 0" json:"deleted_flag"`
} }
// UnDelete clear model's deleted info, you need save this model manually
func (m *ModelSupportUnique) UnDelete() {
m.Model.UnDelete()
m.DeletedFlag = 0
}

View File

@ -65,7 +65,8 @@ func (schema *Schema) ParseIndexes() map[string]Index {
if !exists { if !exists {
idx.Fields = append(idx.Fields, IndexOption{ idx.Fields = append(idx.Fields, IndexOption{
Field: df, Field: df,
priority: 100,
}) })
} }
} }

View File

@ -65,10 +65,11 @@ func (sd SoftDeleteUniqueDeleteClause) Build(clause.Builder) {
func (sd SoftDeleteUniqueDeleteClause) MergeClause(*clause.Clause) { func (sd SoftDeleteUniqueDeleteClause) MergeClause(*clause.Clause) {
} }
var regReplaceUpdate = regexp.MustCompile(`UPDATE (.+?) WHERE `)
func (sd SoftDeleteUniqueDeleteClause) ModifyStatement(stmt *Statement) { func (sd SoftDeleteUniqueDeleteClause) ModifyStatement(stmt *Statement) {
re := regexp.MustCompile(`UPDATE (.*) WHERE `)
if sql := stmt.SQL.String(); sql != "" { if sql := stmt.SQL.String(); sql != "" {
setClause := re.FindStringSubmatch(sql)[1] setClause := regReplaceUpdate.FindStringSubmatch(sql)[1]
if setClause == "" { if setClause == "" {
return return
} }