diff --git a/app/assets/javascripts/app/services/migrationManager.js b/app/assets/javascripts/app/services/migrationManager.js index 66f52bca8..f27dc9005 100644 --- a/app/assets/javascripts/app/services/migrationManager.js +++ b/app/assets/javascripts/app/services/migrationManager.js @@ -1,14 +1,16 @@ class MigrationManager extends SFMigrationManager { - constructor($rootScope, modelManager, syncManager, componentManager, storageManager) { - super(modelManager, syncManager, storageManager); + constructor($rootScope, modelManager, syncManager, componentManager, storageManager, statusManager, authManager) { + super(modelManager, syncManager, storageManager, authManager); this.componentManager = componentManager; + this.statusManager = statusManager; } registeredMigrations() { return [ this.editorToComponentMigration(), - this.componentUrlToHostedUrl() + this.componentUrlToHostedUrl(), + this.removeTagReferencesFromNotes() ]; } @@ -54,6 +56,10 @@ class MigrationManager extends SFMigrationManager { component.url value to store clientData, such as the CodeEditor, which stores the programming language for the note in the note's clientData[component.url]. We want to rewrite any matching items to transfer that clientData into clientData[component.uuid]. + + April 3, 2019 note: it seems this migration is mis-named. The first part of the description doesn't match what the code is actually doing. + It has nothing to do with url/hosted_url relationship and more to do with just mapping client data from the note's hosted_url to its uuid + Created: July 6, 2018 */ componentUrlToHostedUrl() { @@ -62,10 +68,10 @@ class MigrationManager extends SFMigrationManager { content_type: "SN|Component", handler: async (components) => { let hasChanges = false; - var notes = this.modelManager.validItemsForContentType("Note"); - for(var note of notes) { - for(var component of components) { - var clientData = note.getDomainDataItem(component.hosted_url, ComponentManager.ClientDataDomain); + let notes = this.modelManager.validItemsForContentType("Note"); + for(let note of notes) { + for(let component of components) { + let clientData = note.getDomainDataItem(component.hosted_url, ComponentManager.ClientDataDomain); if(clientData) { note.setDomainDataItem(component.uuid, clientData, ComponentManager.ClientDataDomain); note.setDomainDataItem(component.hosted_url, null, ComponentManager.ClientDataDomain); @@ -81,6 +87,63 @@ class MigrationManager extends SFMigrationManager { } } } + + + + /* + Migrate notes which have relationships on tags to migrate those relationships to the tags themselves. + That is, notes.content.references should not include any mention of tags. + This will apply to notes created before the schema change. Now, only tags reference notes. + Created: April 3, 2019 + */ + removeTagReferencesFromNotes() { + return { + name: "remove-tag-references-from-notes", + content_type: "Note", + handler: async (notes) => { + + let needsSync = false; + let status = this.statusManager.addStatusFromString("Running migration..."); + + for(let note of notes) { + if(!note.content) { + continue; + } + + let references = note.content.references; + // Remove any tag references, and transfer them to the tag if neccessary. + let newReferences = []; + + for(let reference of references) { + if(reference.content_type != "Tag") { + newReferences.push(reference); + continue; + } + + // is Tag content_type, we will not be adding this to newReferences + let tag = this.modelManager.findItem(reference.uuid); + if(tag && !tag.hasRelationshipWithItem(note)) { + tag.addItemAsRelationship(note); + tag.setDirty(true, true); + needsSync = true; + } + } + + if(newReferences.length != references.length) { + note.content.references = newReferences; + note.setDirty(true, true); + needsSync = true; + } + } + + if(needsSync) { + await this.syncManager.sync(); + } + + this.statusManager.removeStatus(status); + } + } + } } angular.module('app').service('migrationManager', MigrationManager);