Sha256: 3c9da2a869e9181ec5845227ebfe182b366c006c7ffe8f3ea5f4cc5f9d183665

Contents?: true

Size: 1.23 KB

Versions: 88

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

class CreateDecidimSurveyAnswerOptions < ActiveRecord::Migration[5.1]
  class SurveyQuestion < ApplicationRecord
    self.table_name = :decidim_surveys_survey_questions
  end

  class SurveyAnswerOption < ApplicationRecord
    self.table_name = :decidim_surveys_survey_answer_options
  end

  def up
    create_table :decidim_surveys_survey_answer_options do |t|
      t.references :decidim_survey_question, index: { name: "index_decidim_surveys_answer_options_question_id" }
      t.jsonb :body
    end

    SurveyQuestion.find_each do |question|
      question.answer_options.each do |answer_option|
        SurveyAnswerOption.create!(
          decidim_survey_question_id: question.id,
          body: answer_option["body"]
        )
      end
    end

    remove_column :decidim_surveys_survey_questions, :answer_options
  end

  def down
    add_column :decidim_surveys_survey_questions, :answer_options, :jsonb, default: []

    SurveyAnswerOption.find_each do |answer_option|
      question = SurveyQuestion.find(answer_option.decidim_survey_question_id)

      question.answer_options << { "body" => answer_option.body }

      question.save!
    end

    drop_table :decidim_surveys_survey_answer_options
  end
end

Version data entries

88 entries across 88 versions & 1 rubygems

Version Path
decidim-surveys-0.29.1 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.28.4 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.27.9 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.29.0 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.28.3 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.27.8 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.29.0.rc4 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.29.0.rc3 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.29.0.rc2 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.29.0.rc1 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.28.2 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.27.7 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.28.1 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.27.6 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.26.10 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.26.9 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.28.0 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.27.5 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.28.0.rc5 db/migrate/20180405015012_create_decidim_survey_answer_options.rb
decidim-surveys-0.28.0.rc4 db/migrate/20180405015012_create_decidim_survey_answer_options.rb