class AddIntegrationId < ActiveRecord::Migration
  tag :predeploy
  disable_ddl_transaction!

  def self.up
    add_column :pseudonyms,       :integration_id, :string
    add_column :course_sections,  :integration_id, :string
    add_column :courses,          :integration_id, :string
    add_column :enrollment_terms, :integration_id, :string
    add_column :accounts,         :integration_id, :string

    add_index :pseudonyms,        [:integration_id, :account_id],
              unique: true, algorithm: :concurrently,
              name: "index_pseudonyms_on_integration_id",
              where: "integration_id IS NOT NULL"
    add_index :course_sections,   [:integration_id, :root_account_id],
              unique: true, algorithm: :concurrently,
              name: "index_sections_on_integration_id",
              where: "integration_id IS NOT NULL"
    add_index :courses,           [:integration_id, :root_account_id],
              unique: true, algorithm: :concurrently,
              name: "index_courses_on_integration_id",
              where: "integration_id IS NOT NULL"
    add_index :enrollment_terms,  [:integration_id, :root_account_id],
              unique: true, algorithm: :concurrently,
              name: "index_terms_on_integration_id",
              where: "integration_id IS NOT NULL"
    add_index :accounts,          [:integration_id, :root_account_id],
              unique: true, algorithm: :concurrently,
              name: "index_accounts_on_integration_id",
              where: "integration_id IS NOT NULL"

  end

  def self.down
    remove_index :pseudonyms,       name: "index_pseudonyms_on_integration_id"
    remove_index :course_sections,  name: "index_sections_on_integration_id"
    remove_index :courses,          name: "index_courses_on_integration_id"
    remove_index :enrollment_terms, name: "index_terms_on_integration_id"
    remove_index :accounts,         name: "index_accounts_on_integration_id"

    remove_column :pseudonyms,       :integration_id
    remove_column :course_sections,  :integration_id
    remove_column :courses,          :integration_id
    remove_column :enrollment_terms, :integration_id
    remove_column :accounts,         :integration_id
  end
end

Add integration_id to SIS models
Add integration_id to SIS models

An integration_id can be used in a way similar to sis_source_id in
that it documents a 3rd party identifier for a particular object. The
reason we use an integration_id instead of a sis_source_id for some
SIS integrations is that there is BOTH a sis_source_id and another
id that is used (as in the case of SIF) to ferry information between
systems.

Models that have an integration_id now include:
- pseudonyms
- course_sections
- courses
- enrollment_terms
- accounts

Test Plan:
- make sure the above tables have an integration_id column after "rake db:migrate"

Fixes SIS-107

Change-Id: I80a03d7384fc6ff243436bdf18798ece5059acb7
Reviewed-on: https://gerrit.instructure.com/27593
Reviewed-by: Cody Cutrer <c4fe2b1d90ef8f2548c8aeabfa633434316f0305@instructure.com>
Reviewed-by: Ken Romney <4369dabf8e7d5ba636f3ef79948e6ca3ce23a99f@instructure.com>
Tested-by: Jenkins <d95b56ce41a2e1ac4cecdd398defd7414407cc08@instructure.com>
Reviewed-by: Brian Palmer <3cb4e1df5ec4da2c7c4af7c52cec8cf340a55a10@instructure.com>
Product-Review: Duane Johnson <0a02221ea64f4c9685145b5f44cf84715b4a8bb5@instructure.com>
QA-Review: Duane Johnson <0a02221ea64f4c9685145b5f44cf84715b4a8bb5@instructure.com>
