Sha256: db57c6e3367a10aef35c69fd919d698aa8a60590854271e6f76d508dd3a68d54

Contents?: true

Size: 981 Bytes

Versions: 9

Compression:

Stored size: 981 Bytes

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

9 entries across 9 versions & 1 rubygems

Version Path
active_recall-2.1.0 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-2.0.2 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-2.0.1 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-2.0.0 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.8.6 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.8.5 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.8.4 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.8.3 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb
active_recall-1.8.0 lib/generators/active_recall/templates/migrate_okubo_to_active_recall.rb