mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-09 17:44:59 +09:00
fix set by relation source migration
This commit is contained in:
parent
274f130ec2
commit
6b29889f38
3 changed files with 41 additions and 6 deletions
|
@ -42,11 +42,12 @@ func NewDocFromSnapshot(rootId string, snapshot *pb.ChangeSnapshot, opts ...Snap
|
|||
// migrate old dataview blocks with relations
|
||||
if dvBlock := b.GetDataview(); dvBlock != nil {
|
||||
if len(dvBlock.RelationLinks) == 0 {
|
||||
dvBlock.RelationLinks = relationutils.MigrateRelationsModels(dvBlock.Relations)
|
||||
dvBlock.RelationLinks = relationutils.MigrateRelationModels(dvBlock.Relations)
|
||||
}
|
||||
if !sOpts.doNotMigrateTypes {
|
||||
dvBlock.Source, typesToMigrate = relationutils.MigrateObjectTypeIds(dvBlock.Source)
|
||||
}
|
||||
dvBlock.Source = relationutils.MigrateRelationIds(dvBlock.Source) // can also contain relation ids
|
||||
}
|
||||
blocks[b.Id] = simple.New(b)
|
||||
}
|
||||
|
@ -56,7 +57,7 @@ func NewDocFromSnapshot(rootId string, snapshot *pb.ChangeSnapshot, opts ...Snap
|
|||
}
|
||||
|
||||
if len(snapshot.Data.RelationLinks) == 0 && len(snapshot.Data.ExtraRelations) > 0 {
|
||||
snapshot.Data.RelationLinks = relationutils.MigrateRelationsModels(snapshot.Data.ExtraRelations)
|
||||
snapshot.Data.RelationLinks = relationutils.MigrateRelationModels(snapshot.Data.ExtraRelations)
|
||||
}
|
||||
// clear nil values
|
||||
pb2.StructDeleteEmptyFields(snapshot.Data.Details)
|
||||
|
@ -383,11 +384,12 @@ func (s *State) changeBlockCreate(bc *pb.ChangeBlockCreate) (err error) {
|
|||
s.Set(b)
|
||||
if dv := b.Model().GetDataview(); dv != nil {
|
||||
if len(dv.RelationLinks) == 0 {
|
||||
dv.RelationLinks = relationutils.MigrateRelationsModels(dv.Relations)
|
||||
dv.RelationLinks = relationutils.MigrateRelationModels(dv.Relations)
|
||||
}
|
||||
var typesToMigrate []string
|
||||
dv.Source, typesToMigrate = relationutils.MigrateObjectTypeIds(dv.Source)
|
||||
s.objectTypesToMigrate = append(s.objectTypesToMigrate, typesToMigrate...)
|
||||
dv.Source = relationutils.MigrateRelationIds(dv.Source) // can also contain relation ids
|
||||
}
|
||||
}
|
||||
return s.InsertTo(bc.TargetId, bc.Position, bIds...)
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
package relationutils
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gogo/protobuf/types"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/bundle"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore/addr"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/pbtypes"
|
||||
"github.com/gogo/protobuf/types"
|
||||
)
|
||||
|
||||
func RelationFromStruct(st *types.Struct) *Relation {
|
||||
|
@ -97,7 +101,7 @@ func (rs Relations) GetModelByKey(key string) *model.Relation {
|
|||
return nil
|
||||
}
|
||||
|
||||
func MigrateRelationsModels(rels []*model.Relation) (relLinks []*model.RelationLink) {
|
||||
func MigrateRelationModels(rels []*model.Relation) (relLinks []*model.RelationLink) {
|
||||
relLinks = make([]*model.RelationLink, 0, len(rels))
|
||||
for _, rel := range rels {
|
||||
relLinks = append(relLinks, &model.RelationLink{
|
||||
|
@ -107,3 +111,32 @@ func MigrateRelationsModels(rels []*model.Relation) (relLinks []*model.RelationL
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
func MigrateRelationIds(ids []string) []string {
|
||||
// shortcut if there is nothing to migrate
|
||||
hasIdsToMigrate := false
|
||||
for _, id := range ids {
|
||||
if strings.HasPrefix(id, addr.BundledRelationURLPrefix) || strings.HasPrefix(id, addr.OldIndexedRelationURLPrefix) {
|
||||
hasIdsToMigrate = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasIdsToMigrate {
|
||||
return ids
|
||||
}
|
||||
|
||||
normalized := make([]string, len(ids))
|
||||
var (
|
||||
key string
|
||||
err error
|
||||
)
|
||||
for i, id := range ids {
|
||||
key, err = pbtypes.RelationIdToKey(id)
|
||||
if err != nil {
|
||||
normalized[i] = id
|
||||
} else {
|
||||
normalized[i] = addr.RelationKeyToIdPrefix + key
|
||||
}
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
|
|
@ -160,7 +160,7 @@ func (b *builtinObjects) createObject(ctx context.Context, rd io.ReadCloser) (er
|
|||
f["analyticsOriginalId"] = pbtypes.String(oldId)
|
||||
|
||||
st.Set(simple.New(m))
|
||||
rels := relationutils.MigrateRelationsModels(st.OldExtraRelations())
|
||||
rels := relationutils.MigrateRelationModels(st.OldExtraRelations())
|
||||
st.AddRelationLinks(rels...)
|
||||
|
||||
st.RemoveDetail(bundle.RelationKeyCreator.String(), bundle.RelationKeyLastModifiedBy.String())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue