Skip to content

Commit

Permalink
update commitlog schema
Browse files Browse the repository at this point in the history
  • Loading branch information
totegamma committed Oct 12, 2024
1 parent 416caae commit 82a3c82
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 33 deletions.
3 changes: 2 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -185,6 +185,7 @@ func main() {
&core.SemanticID{},
&core.Job{},
&core.CommitLog{},
&core.CommitOwner{},
)

if err != nil {
Expand Down
27 changes: 17 additions & 10 deletions core/dbschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()"`
}
70 changes: 48 additions & 22 deletions x/store/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 82a3c82

Please sign in to comment.