Sha256: 7decce5b29db3fd3cfb27b13d493e63360b9fed75cda13aa47de94a77e02a2ea

Contents?: true

Size: 1.76 KB

Versions: 98

Compression:

Stored size: 1.76 KB

Contents

class AddExternalIdToResearchStudyParticipants < ActiveRecord::Migration[5.1]
  def change
    within_renalware_schema do
      # The external_id is for obfuscating the participant id using a random integer.
      # It is used e.g. when passing to an external research application
      add_column(
        :research_study_participants,
        :external_id,
        :integer,
        null: true
      )

      reversible do |direction|
        direction.up do
          # Create a function the trigger will call..
          load_function("update_research_study_participants_from_trigger_v01.sql")

          # .. and the trigger when a row is inserted
          load_trigger("update_research_study_participants_trigger_v01.sql")

          # Populate the just-created external_id column with the correct value that the trigger
          # would otherwise create if it were added in the future.
          connection.execute(
            "UPDATE renalware.research_study_participants SET external_id = renalware.pseudo_encrypt(id::integer) where external_id is NULL;"
          )
        end
        direction.down do
          connection.execute("
            DROP TRIGGER IF EXISTS update_research_study_participants_trigger ON renalware.research_study_participants;
            DROP FUNCTION IF EXISTS update_research_study_participants_from_trigger();
          ")
        end
      end

      # Now we have created and populated the external_id we need to add a unique index.
      # external_id is guaranteed to be unique because the generating function (called by the
      # trigger when a row is inserted) is based on the id.
      # See db/functions/update_research_study_participants_from_trigger_v01
      add_index :research_study_participants, :external_id, unique: true
    end
  end
end

Version data entries

98 entries across 98 versions & 1 rubygems

Version Path
renalware-core-2.1.1 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.1.0 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.167 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.166 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.165 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.164 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.163 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.162 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.161 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.160 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.159 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.158 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.157 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.156 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.155 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.153 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.152 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.151 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.149 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb
renalware-core-2.0.148 db/migrate/20180605141806_add_external_id_to_research_study_participants.rb