Sha256: ce7e6f1f410b9657609706d0dd338d353fdde4d62d25dfea967a2cf436ff36b6

Contents?: true

Size: 1.29 KB

Versions: 161

Compression:

Stored size: 1.29 KB

Contents

class AddDeletedAtToPatientBookmarks < ActiveRecord::Migration[4.2]
  def up
    add_column :patient_bookmarks, :deleted_at, :datetime
    remove_index(:patient_bookmarks, [:patient_id, :user_id])
    add_index :patient_bookmarks, :patient_id

    # We'd like to have a unique index on [patient, user, deleted_at]
    # so that there can only ever be one un-deleted (deleted_at == NULL)
    # patient+user combination - ie you can't bookmark a patient twice.
    # However we also want to allow historical duplicates - there can be
    # many of the same patient+user combination as long as they have a different
    # deleted_at datetime. Because NULLs don't help in the index for our un-deleted
    # scenario (deleted_at == NULL) we have to coalesce those NULLs into the same
    # valid date. This lets us have a unique index when deleted_at is NULL.
    connection.execute(
      "CREATE UNIQUE INDEX patient_bookmarks_uniqueness
      ON patient_bookmarks
      (patient_id, user_id, COALESCE(deleted_at, '1970-01-01'));"
    )
  end
  def down
    remove_index(:patient_bookmarks, name: :patient_bookmarks_uniqueness)
    remove_column :patient_bookmarks, :deleted_at, :datetime
    add_index(:patient_bookmarks, [:patient_id, :user_id], unique: true)
    remove_index :patient_bookmarks, :patient_id
  end
end

Version data entries

161 entries across 161 versions & 1 rubygems

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