Sha256: fa9a52e8147fd12f901673e0aed4d06d4674a787cb0889341fc6dab64fafebef

Contents?: true

Size: 1.98 KB

Versions: 68

Compression:

Stored size: 1.98 KB

Contents

class CreateLowClearanceProfiles < ActiveRecord::Migration[5.1]
  def change
    create_table :low_clearance_profiles do |t|
      t.references :patient, foreign_key: true, index: { unique: true }, null: false
      t.jsonb :document
      t.references :updated_by, foreign_key: { to_table: :users }, index: true, null: false
      t.references :created_by, foreign_key: { to_table: :users }, index: true, null: false
      t.timestamps null: false
    end
    add_index :low_clearance_profiles, :document, using: :gin

    sql_for_modality_type = lambda { |mod_desc_type|
      <<-SQL.squish
        UPDATE modality_descriptions SET type = #{connection.quote(mod_desc_type)}
        WHERE name = 'Low Clearance';
      SQL
    }

    reversible do |direction|
      direction.up do
        connection.execute(
          sql_for_modality_type.call("Renalware::LowClearance::ModalityDescription")
        )
        # Move the low_clearance section of renal_profiles.document into
        # low_clearance_profiles.document
        connection.execute(
          <<-SQL.squish
            INSERT INTO low_clearance_profiles (patient_id, document, updated_at,
            created_at, updated_by_id, created_by_id)
            SELECT patient_id, rp.document -> 'low_clearance', updated_at, created_at,
            (SELECT id FROM users ORDER BY id ASC LIMIT 1),
            (SELECT id FROM users ORDER BY id ASC LIMIT 1)
            FROM renal_profiles rp;
            -- clear out the moved low_clearance section.
            UPDATE renal_profiles SET document = document - 'low_clearance';
          SQL
        )
      end
      direction.down do
        connection.execute(
          sql_for_modality_type.call("Renalware::Renal::LowClearance::ModalityDescription")
        )
        raise "If you really want to roll-back this migration you need to implement the "\
              "insertion into renal_profiles.document -> 'low_clearance' of the "\
              "low_clearance_profiles.document jsonb"
      end
    end
  end
end

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
renalware-core-2.0.64 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.63 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.62 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.61 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.60 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.58 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.57 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.56 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.55 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.54 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.53 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.52 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.51 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.50 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.48 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.47 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.46 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.45 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.44 db/migrate/20180112151706_create_low_clearance_profiles.rb
renalware-core-2.0.43 db/migrate/20180112151706_create_low_clearance_profiles.rb