|
1 | 1 | 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 | + |
2 | 4 | def up |
3 | | - create_table :patient_scores, id: :uuid do |t| |
4 | | - t.references :patient, null: false, foreign_key: true, type: :uuid |
5 | | - t.string :score_type, null: false, limit: 100 |
6 | | - t.decimal :score_value, precision: 5, scale: 2, null: false |
7 | | - t.datetime :device_created_at, null: false |
8 | | - t.datetime :device_updated_at, null: false |
9 | | - t.datetime :deleted_at |
| 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 | + |
| 10 | + unless table_exists?(:patient_scores) |
| 11 | + create_table :patient_scores, id: :uuid do |t| |
| 12 | + t.references :patient, null: false, foreign_key: true, type: :uuid |
| 13 | + t.string :score_type, null: false, limit: 100 |
| 14 | + t.decimal :score_value, precision: 5, scale: 2, null: false |
| 15 | + t.datetime :device_created_at, null: false |
| 16 | + t.datetime :device_updated_at, null: false |
| 17 | + t.datetime :deleted_at |
10 | 18 |
|
11 | | - t.timestamps |
| 19 | + t.timestamps |
| 20 | + end |
12 | 21 | end |
13 | 22 |
|
14 | | - add_index :patient_scores, [:patient_id, :score_type] |
15 | | - add_index :patient_scores, :updated_at |
| 23 | + add_index :patient_scores, [:patient_id, :score_type], unique: true unless index_exists?(:patient_scores, [:patient_id, :score_type]) |
| 24 | + add_index :patient_scores, :updated_at unless index_exists?(:patient_scores, :updated_at) |
16 | 25 | end |
17 | 26 |
|
18 | 27 | def down |
19 | | - drop_table :patient_scores |
| 28 | + drop_table :patient_scores, if_exists: true |
| 29 | + end |
| 30 | + |
| 31 | + private |
| 32 | + |
| 33 | + def schema_complete? |
| 34 | + REQUIRED_COLUMNS.all? { |col| column_exists?(:patient_scores, col) } |
20 | 35 | end |
21 | 36 | end |
0 commit comments