Sha256: 7e962a2464b0160fa818e901e3260594221705820283e6466692149070a2e0c9

Contents?: true

Size: 1.13 KB

Versions: 13

Compression:

Stored size: 1.13 KB

Contents

class CreateNobelPrizes < ActiveRecord::Migration
  def up
    create_table :nobel_prizes do |t|
      t.integer :nobel_prize_winner_id
      t.string  :category
      t.integer :year
    end

    execute(
      'INSERT INTO nobel_prizes("nobel_prize_winner_id", "category", "year") ' +
      'SELECT id, category, year ' +
      'FROM nobel_prize_winners'
    )

    NobelPrizeWinner.find_by_first_name_and_last_name('Marie', 'Curie').nobel_prizes.create(category: 'Physics', year: 1903)

    remove_column :nobel_prize_winners, :category, :year
  end

  def down
    add_column :nobel_prize_winners, :category, :string
    add_column :nobel_prize_winners, :year, :integer

    NobelPrizeWinner.find_by_first_name_and_last_name('Marie', 'Curie').nobel_prizes.order(:year).first.delete

    execute(
      'UPDATE nobel_prize_winners ' +
      'SET category = (SELECT nobel_prizes.category FROM nobel_prizes WHERE nobel_prize_winners.id = nobel_prizes.nobel_prize_winner_id), ' +
        'year = (SELECT nobel_prizes.year FROM nobel_prizes WHERE nobel_prize_winners.id = nobel_prizes.nobel_prize_winner_id)'
    )

    drop_table :nobel_prizes
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
with_filters-0.1.2 spec/dummy/db/migrate/20120203212237_create_nobel_prizes.rb
with_filters-0.1.1 spec/dummy/db/migrate/20120203212237_create_nobel_prizes.rb
with_filters-0.1.0 spec/dummy/db/migrate/20120203212237_create_nobel_prizes.rb
with_order-0.1.0 spec/dummy/db/migrate/20120203212237_create_nobel_prizes.rb
tableficate-0.3.2 spec/test_app/db/migrate/20111010191626_create_nobel_prizes.rb
tableficate-0.3.1 spec/test_app/db/migrate/20111010191626_create_nobel_prizes.rb
tableficate-0.3.0 spec/test_app/db/migrate/20111010191626_create_nobel_prizes.rb
tableficate-0.2.1 spec/test_app/db/migrate/20111010191626_create_nobel_prizes.rb
tableficate-0.2.0 spec/test_app/db/migrate/20111010191626_create_nobel_prizes.rb
tableficate-0.1.3 spec/test_app/db/migrate/20111010191626_create_nobel_prizes.rb
tableficate-0.1.2 spec/test_app/db/migrate/20111010191626_create_nobel_prizes.rb
tableficate-0.1.1 spec/test_app/db/migrate/20111010191626_create_nobel_prizes.rb
tableficate-0.0.1 spec/test_app/db/migrate/20111010191626_create_nobel_prizes.rb