From 82a3c82ba90d9c4b96ea0753c880cb3506674f31 Mon Sep 17 00:00:00 2001 From: totegamma Date: Sat, 12 Oct 2024 23:40:00 +0900 Subject: [PATCH] update commitlog schema --- cmd/api/main.go | 3 +- core/dbschema.go | 27 ++++++++++------- x/store/repository.go | 70 +++++++++++++++++++++++++++++-------------- 3 files changed, 67 insertions(+), 33 deletions(-) diff --git a/cmd/api/main.go b/cmd/api/main.go index 5fc75579..d2d025ad 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -96,7 +96,7 @@ func main() { err := config.Load(configPath) if err != nil { - slog.Error("Failed to load config: ", err) + slog.Error("Failed to load config: ", slog.String("error", err.Error())) } conconf := core.SetupConfig(config.Concrnt) @@ -185,6 +185,7 @@ func main() { &core.SemanticID{}, &core.Job{}, &core.CommitLog{}, + &core.CommitOwner{}, ) if err != nil { diff --git a/core/dbschema.go b/core/dbschema.go index 5243b6dd..a140bd6d 100644 --- a/core/dbschema.go +++ b/core/dbschema.go @@ -217,15 +217,22 @@ type Job struct { TraceID string `json:"traceID" gorm:"type:text"` } +type CommitOwner struct { + ID uint `json:"id" gorm:"primaryKey;auto_increment"` + CommitLogID uint `json:"commitLogID" gorm:"index"` + Owner string `json:"owner" gorm:"type:char(42)"` +} + type CommitLog struct { - ID uint `json:"id" gorm:"primaryKey;auto_increment"` - IP string `json:"ip" gorm:"type:text"` - DocumentID string `json:"documentID" gorm:"type:char(26);uniqueIndex:idx_document_id"` - IsEphemeral bool `json:"isEphemeral" gorm:"type:boolean;default:false"` - Type string `json:"type" gorm:"type:text"` - Document string `json:"document" gorm:"type:json"` - Signature string `json:"signature" gorm:"type:char(130)"` - SignedAt time.Time `json:"signedAt" gorm:"type:timestamp with time zone;not null;default:clock_timestamp()"` - Owners pq.StringArray `json:"owners" gorm:"type:char(42)[]"` - CDate time.Time `json:"cdate" gorm:"type:timestamp with time zone;not null;default:clock_timestamp()"` + ID uint `json:"id" gorm:"primaryKey;auto_increment"` + IP string `json:"ip" gorm:"type:text"` + DocumentID string `json:"documentID" gorm:"type:char(26);uniqueIndex:idx_document_id"` + IsEphemeral bool `json:"isEphemeral" gorm:"type:boolean;default:false"` + Type string `json:"type" gorm:"type:text"` + Document string `json:"document" gorm:"type:json"` + Signature string `json:"signature" gorm:"type:char(130)"` + SignedAt time.Time `json:"signedAt" gorm:"type:timestamp with time zone;not null;default:clock_timestamp()"` + CommitOwners []CommitOwner `json:"commitOwners" gorm:"foreignKey:CommitLogID"` + Owners []string `json:"owners" gorm:"-"` + CDate time.Time `json:"cdate" gorm:"type:timestamp with time zone;not null;default:clock_timestamp()"` } diff --git a/x/store/repository.go b/x/store/repository.go index 6280d534..e01a3652 100644 --- a/x/store/repository.go +++ b/x/store/repository.go @@ -36,7 +36,32 @@ func (r *repository) Log(ctx context.Context, commit core.CommitLog) (core.Commi ctx, span := tracer.Start(ctx, "Store.Repository.Log") defer span.End() - err := r.db.WithContext(ctx).Create(&commit).Error + tx := r.db.Begin() + defer func() { + if r := recover(); r != nil { + tx.Rollback() + } + }() + + err := tx.WithContext(ctx).Create(&commit).Error + if err != nil { + tx.Rollback() + return core.CommitLog{}, err + } + + for _, owner := range commit.Owners { + ownerRecord := core.CommitOwner{ + CommitLogID: commit.ID, + Owner: owner, + } + err = tx.WithContext(ctx).Create(&ownerRecord).Error + if err != nil { + tx.Rollback() + return core.CommitLog{}, err + } + } + + err = tx.Commit().Error return commit, err } @@ -143,14 +168,14 @@ func (r *repository) SyncStatus(ctx context.Context, owner string) (core.SyncSta } var latestSignedAt time.Time - err = r.db. - WithContext(ctx). + err = r.db.WithContext(ctx). Model(&core.CommitLog{}). - Where("? = ANY(owners)", owner). - Where("is_ephemeral = ?", false). - Order("signed_at DESC"). + Joins("JOIN commit_owners ON commit_owners.commit_log_id = commit_logs.id"). + Where("commit_owners.owner = ?", owner). + Where("commit_logs.is_ephemeral = ?", false). + Order("commit_logs.signed_at DESC"). Limit(1). - Pluck("signed_at", &latestSignedAt). + Pluck("commit_logs.signed_at", &latestSignedAt). Error if err != nil { @@ -188,14 +213,14 @@ func (r *repository) SyncCommitFile(ctx context.Context, owner string) error { var pageSize = 10 var firstCommitDate time.Time - err = r.db. - WithContext(ctx). + err = r.db.WithContext(ctx). Model(&core.CommitLog{}). - Where("? = ANY(owners)", owner). - Where("is_ephemeral = ?", false). - Order("signed_at ASC"). + Joins("JOIN commit_owners ON commit_owners.commit_log_id = commit_logs.id"). + Where("commit_owners.owner = ?", owner). + Where("commit_logs.is_ephemeral = ?", false). + Order("commit_logs.signed_at ASC"). Limit(1). - Pluck("signed_at", &firstCommitDate). + Pluck("commit_logs.signed_at", &firstCommitDate). Error if err != nil { @@ -204,14 +229,14 @@ func (r *repository) SyncCommitFile(ctx context.Context, owner string) error { } var latestCommitDate time.Time - err = r.db. - WithContext(ctx). + err = r.db.WithContext(ctx). Model(&core.CommitLog{}). - Where("? = ANY(owners)", owner). - Where("is_ephemeral = ?", false). - Order("signed_at DESC"). + Joins("JOIN commit_owners ON commit_owners.commit_log_id = commit_logs.id"). + Where("commit_owners.owner = ?", owner). + Where("commit_logs.is_ephemeral = ?", false). + Order("commit_logs.signed_at DESC"). Limit(1). - Pluck("signed_at", &latestCommitDate). + Pluck("commit_logs.signed_at", &latestCommitDate). Error if err != nil { @@ -244,11 +269,12 @@ func (r *repository) SyncCommitFile(ctx context.Context, owner string) error { var commits []core.CommitLog query := r.db.WithContext(ctx). - Where("? = ANY(owners)", owner). - Where("is_ephemeral = ?", false) + Joins("JOIN commit_owners ON commit_owners.commit_log_id = commit_logs.id"). + Where("commit_owners.owner = ?", owner). + Where("commit_logs.is_ephemeral = ?", false) if lastSignedAt.IsZero() { - query = query.Order("signed_at ASC") + query = query.Order("commit_logs.signed_at ASC") } err = query.Find(&commits).Limit(pageSize).Error