automerge / automerge-classic

A JSON-like data structure (a CRDT) that can be modified concurrently by different users, and merged again automatically.
http://automerge.org/
MIT License
14.77k stars 467 forks source link

Right way to update data schema #535

Open bokolob opened 5 months ago

bokolob commented 5 months ago

Hello. I do it in this way

    if (!doc.content.trip_settings || doc.content.trip_settings.version < 2) {
       let migration = Automerge.change(Automerge.clone(this.doc, { actorId: "0002" }), { time: 1 }, (doc) => {
           doc.content.events = {};
           doc.content.trip_settings = {
                name: this.text_node(db_record.name),
               version: 2,
       };
     });

     let [new_doc, patch] = Automerge.applyChanges(this.doc, [migration]);
     this.doc = new_doc; 
  }

As you can see - all migrations are different, because they set different name, so I can't prepare a blob.

After this migration was written I have some glitches, which I can't debug (Not sure that it's because of migration). E.g. sometimes last version of document becomes empty. And sometimes applying that migration again can return data back.

Is my code correct? Don't I forget something?