Skip to content

Commit 92f6e9d

Browse files
committed
fix: Make CreatePatientScores migration idempotent
1 parent c104b22 commit 92f6e9d

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

db/migrate/20260209112204_create_patient_scores.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
class CreatePatientScores < ActiveRecord::Migration[6.1]
2+
REQUIRED_COLUMNS = %i[patient_id score_type score_value device_created_at device_updated_at deleted_at].freeze
3+
24
def up
5+
if table_exists?(:patient_scores) && !schema_complete?
6+
say "Dropping patient_scores table due to incomplete schema"
7+
drop_table :patient_scores
8+
end
9+
310
unless table_exists?(:patient_scores)
411
create_table :patient_scores, id: :uuid do |t|
512
t.references :patient, null: false, foreign_key: true, type: :uuid
@@ -13,11 +20,17 @@ def up
1320
end
1421
end
1522

16-
add_index :patient_scores, [:patient_id, :score_type] unless index_exists?(:patient_scores, [:patient_id, :score_type])
23+
add_index :patient_scores, [:patient_id, :score_type], unique: true unless index_exists?(:patient_scores, [:patient_id, :score_type])
1724
add_index :patient_scores, :updated_at unless index_exists?(:patient_scores, :updated_at)
1825
end
1926

2027
def down
2128
drop_table :patient_scores, if_exists: true
2229
end
30+
31+
private
32+
33+
def schema_complete?
34+
REQUIRED_COLUMNS.all? { |col| column_exists?(:patient_scores, col) }
35+
end
2336
end

0 commit comments

Comments
 (0)