Sha256: 42ff3e808cebab2aaa28c8ecbf7cabb8ca7bddfb29ea61835ef6f34dbe1fc0ac

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

Sequel.migration do
  up do
    users = Zen.database[:users].all

    create_table(:user_statuses) do
      primary_key :id

      String :name, :null => false
      TrueClass :allow_login, :default => false
    end

    alter_table(:users) do
      drop_column(:status)

      add_foreign_key(
        :user_status_id,
        :user_statuses,
        :on_delete => :cascade,
        :on_update => :cascade,
        :key       => :id
      )
    end

    Zen.database[:user_statuses].insert_multiple([
      {:name => 'active', :allow_login => true},
      {:name => 'closed'}
    ])

    open   = Zen.database[:user_statuses].filter(:name => 'active').all[0]
    closed = Zen.database[:user_statuses].filter(:name => 'closed').all[0]

    # Migrate the existing statuses.
    users.each do |user|
      status = user[:status] == 'open' ? open[:id] : closed[:id]

      Zen.database[:users] \
        .filter(:id => user[:id]) \
        .update(:user_status_id => status)
    end
  end

  down do
    users  = Zen.database[:users].all
    open   = Zen.database[:user_statuses].filter(:name => 'active').all[0]
    closed = Zen.database[:user_statuses].filter(:name => 'closed').all[0]

    alter_table(:users) do
      if Zen.database.adapter_scheme.to_s.include?('mysql')
        drop_constraint(:users_ibfk_1, :type => :foreign_key)
      end

      drop_column(:user_status_id)
      add_column(:status, String, :null => false, :default => 'open')
    end

    drop_table(:user_statuses)

    # Put the old statuses back in place.
    users.each do |user|
      status = user[:user_status_id] == open[:id] ? 'open' : 'closed'

      Zen.database[:users] \
        .filter(:id => user[:id]) \
        .update(:status => status)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
zen-0.3 lib/zen/package/users/migrations/1320272365_status_ids.rb
zen-0.3b1 lib/zen/package/users/migrations/1320272365_status_ids.rb
zen-0.3b lib/zen/package/users/migrations/1320272365_status_ids.rb