Sha256: c98d9b9af11c4aff2b49822f3d03a37f67c3d0d03a0456e39a741166e27d54d7

Contents?: true

Size: 1 KB

Versions: 13

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

class MigrateOkuboToActiveRecall < ActiveRecord::Migration[5.2]
  DECK_SQL = 'SELECT  "okubo_decks".* FROM "okubo_decks"'
  ITEM_SQL = 'SELECT "okubo_items".* FROM "okubo_items"'

  def self.up
    connection = ActiveRecord::Base.connection
    decks = connection.execute(DECK_SQL).to_a.map(&:symbolize_keys)
    items = connection.execute(ITEM_SQL).to_a.map(&:symbolize_keys)

    decks.each do |deck_attributes|
      deck_create_attributes = deck_attributes.except(:id, :created_at, :updated_at)
      deck = ActiveRecall::Deck.create!(deck_create_attributes)

      items.select do |item_attributes|
        deck_attributes[:id] == item_attributes[:deck_id]
      end.each do |item_attributes|
        item_create_attributes = item_attributes
                                 .except(:id, :created_at, :updated_at)
                                 .merge(deck_id: deck.id)
        ActiveRecall::Item.create!(item_create_attributes)
      end
    end
  end

  def self.down
    true
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
active_recall-1.6.4 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.6.3 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.6.2 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.6.1 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.6.0 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.5.0 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.4.0 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.3.0 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.2.2 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.2.1 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.2.0 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.1.0 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.0.0 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb