Ruby Workflow Issue During Migration
<p>I am using Ruby Workflow in my ActiveRecords using Gem: <a href="https://github.com/geekq/workflow" rel="nofollow">Workflow</a></p>

<p><strong>Existing Running Code contains:</strong></p>

<ul>
<li>I am having an ActiveRecord: X</li>
<li>I am having two Migrations already:

<ul>
<li><strong>(Ref1)</strong> CreateX migration (which creates table X)</li>
<li><strong>(Ref2)</strong> CreateInitialEntryInX migration (which creates one entry in table X)</li>
</ul></li>
</ul>

<p><strong>New Changes:</strong></p>

<ul>
<li>Now I wanted to add workflow in ActiveRecord X, hence I did:

<ul>
<li><strong>(Ref3)</strong> I added the workflow code in ActiveRecord Model X (mentioning :status as my workflow field)</li>
<li><strong>(Ref4)</strong> AddStatusFieldToX migration (which adds :status field in table X)</li>
</ul></li>
</ul>

<p>Now when I run rake db:migrate after the changes, the <strong>(Ref2)</strong> breaks cos Migration looks for :status field as it is mentioned in ActiveRecord Model in the Workflow section, but :status field has not been added yet as migration <strong>(Ref4)</strong> has not executed yet.</p>

<p>Hence, all the builds fail when all migrations are run in sequence, Any solution to this?
I do not want to resequence any of the migration or edit any old existing migrations.</p>

<p>My Model looks like:</p>

<pre><code>  class BaseModel &lt; ActiveRecord::Base
      #
      # Workflow to define states of Role
      #
      # Initial State =&gt; Active
      #
      # # State Diagram::
      #   Active  --soft_delete--&gt; Deleted
      #   Deleted
      #
      # * activate, soft_delete are the event which triggers the state transition
      #
      include Workflow
      workflow_column :status
      workflow do
        state :active, X_MODEL_STATES::ACTIVE do
          event :soft_delete, transitions_to: :deleted
        end
        state :deleted, X_MODEL_STATES::DELETED

        on_transition do |from, to, event, *event_args|
          self.update_attribute(:status, to)
        end
      end

      def trigger_event(event)
        begin
          case event.to_i
            when X_MODEL_EVENTS::ACTIVATE
              self.activate!
            when X_MODEL_EVENTS::SOFT_DELETE
              self.soft_delete!
          end
        rescue ....
      end
  end

  class X_MODEL_STATES
    ACTIVE      = 1
    DELETED     = 2
  end

  class X_MODEL_EVENTS
    ACTIVATE      = 1
    SOFT_DELETE   = 2
  end

# Migrations(posting Up functions only - in correct sequence)
#--------------------------------------------------

#1st: Migration - This is already existing migration
CreateX &lt; ActiveRecord::Migration
  def up
    create_table :xs do |t|
      t.string :name
      t.timestamps null: false
    end
  end
end

#2nd: Migration - This is already existing migration
CreateInitialX &lt; ActiveRecord::Migration
  def up
    X.create({:name =&gt; 'Kartik'})
  end
end

#3rd: Migration - This is a new migration
AddStatusToX &lt; ActiveRecord::Migration
  def up
    add_column :xs, :status, :integer
    x.all.each do |x_instance|
      x.status = X_MODEL_STATES::ACTIVE
      x.save!
    end
  end
end
</code></pre>

<p>So, when <strong>Migration#2</strong> runs, it tries to find :status field to write is with initial value of <code>X_MODEL_STATES::ACTIVE</code> as it is mentioned in Active Record Model files workflow as: <code>workflow_column :status</code> and does not find the field as <strong>Migration#3</strong> is yet to execute.</p>