require 'singleton' module Scrivito module Migrations class WorkspaceLock include Singleton def get if exists? begin Workspace.find(workspace_id) rescue Scrivito::ResourceNotFound create_workspace end else create_workspace end end def remove if exists? File.delete(filename) end end def exists? File.exists?(filename) end private def create_workspace Workspace.create(title: 'Migration Working Copy').tap do |workspace| write(workspace) end end def write(workspace) File.write(filename, workspace.id) end def workspace_id File.read(filename) end def filename File.join(Rails.root, 'tmp/migration_store.lock') end end end end