# frozen_string_literal: true
class CreateCourseAssessmentProgrammingQuestions < ActiveRecord::Migration[4.2]
  def change
    create_table :course_assessment_question_programming do |t|
      t.string :language, null: false
      t.integer :memory_limit, comment: 'Memory limit, in MiB'
      t.integer :time_limit, comment: 'Time limit, in seconds'
      t.uuid :import_job_id, comment: 'The ID of the importing job',
                             foreign_key: { references: :jobs, on_delete: :set_null },
                             index: :unique
    end

    create_table :course_assessment_question_programming_template_files do |t|
      t.references :question, foreign_key: { references: :course_assessment_question_programming },
                              null: false
      t.string :filename, null: false
      t.text :content, null: false
    end

    create_table :course_assessment_question_programming_test_cases do |t|
      t.references :question, foreign_key: { references: :course_assessment_question_programming },
                              null: false
      t.string :identifier, comment: 'Test case identifier generated by the testing framework',
                            index: { with: :question_id, unique: true,
                                     name: 'index_course_assessment_question_programming_test_'\
                                           'case_ident' }, null: false
      t.boolean :public, null: false
      t.text :description, null: false
      t.text :hint
    end

    create_table :course_assessment_answer_programming

    create_table :course_assessment_answer_programming_files do |t|
      t.references :answer, foreign_key: { references: :course_assessment_answer_programming },
                            null: false
      t.string :filename, null: false,
                          index: { with: :answer_id, unique: true,
                                   name: 'index_course_assessment_answer_programming_files_'\
                                         'filename' }
      t.text :content, default: '', null: false
    end

    change_table :course_assessment_answer_auto_gradings do |t|
      t.actable foreign_key: false
    end
    add_index :course_assessment_answer_auto_gradings, [:actable_id, :actable_type],
              name: :index_course_assessment_answer_auto_gradings_on_actable, unique: true

    create_table :course_assessment_answer_programming_auto_gradings

    create_table :course_assessment_answer_programming_auto_grading_test_results do |t|
      t.references :auto_grading,
                   foreign_key: { references: :course_assessment_answer_programming_auto_gradings },
                   null: false
      t.references :test_case,
                   foreign_key: { references: :course_assessment_question_programming_test_cases }
      t.boolean :passed, null: false
      t.text :message
    end
  end
end
