       else
         newName = currentName
 
+      migration._updateAll = false
+
       future = new Future()
       migration.forward db, currentName, currentSchema, newSchema, future.resolver()
       migrated = future.wait()
+
+      updateAll = true if migrated and migration._updateAll
 
       if i < migrationsPending
         count = globals.Document.Migrations.update

          $set:
            _schema: @Meta.schema

  @updateAll: ->
    setupObservers true



# TODO: What happens if this is called multiple times? We should make sure that for each document observrs are made only once
setupMigrations = ->
  updateAll = false
  # Migrate all except local collections
  for document in globals.Document.list when document.Meta.collection._name isnt null
    # Always run setupMigrations, don't short circuit
    updateAll = document.setupMigrations() or updateAll

  if updateAll
    Log.warn "Migrations requested updating all references..."
    globals.Document.updateAll()
    Log.warn "Done."

migrations = ->
  if globals.Document.Migrations.find({}, limit: 1).count() == 0
