|
| 1 | +class ConsolidatedSecurityAndFeaturesMigration < ActiveRecord::Migration[7.1] |
| 2 | + def up |
| 3 | + # ==== AUTH TOKEN SECURITY FEATURES ==== |
| 4 | + |
| 5 | + # Add token_type to auth_tokens |
| 6 | + unless column_exists?(:auth_tokens, :token_type) |
| 7 | + add_column :auth_tokens, :token_type, :integer, null: false, default: 0 |
| 8 | + add_index :auth_tokens, :token_type |
| 9 | + end |
| 10 | + |
| 11 | + # Add session binding columns |
| 12 | + unless column_exists?(:auth_tokens, :session_ip) |
| 13 | + add_column :auth_tokens, :session_ip, :string |
| 14 | + end |
| 15 | + |
| 16 | + unless column_exists?(:auth_tokens, :session_user_agent) |
| 17 | + add_column :auth_tokens, :session_user_agent, :string |
| 18 | + end |
| 19 | + |
| 20 | + # Add last seen tracking |
| 21 | + unless column_exists?(:auth_tokens, :last_seen_ip) |
| 22 | + add_column :auth_tokens, :last_seen_ip, :string |
| 23 | + end |
| 24 | + |
| 25 | + unless column_exists?(:auth_tokens, :last_seen_ua) |
| 26 | + add_column :auth_tokens, :last_seen_ua, :string |
| 27 | + end |
| 28 | + |
| 29 | + # Use TEXT for IP history in case of MySQL/MariaDB |
| 30 | + unless column_exists?(:auth_tokens, :ip_history) |
| 31 | + if ActiveRecord::Base.connection.adapter_name.downcase.include?('mysql') |
| 32 | + add_column :auth_tokens, :ip_history, :text |
| 33 | + else |
| 34 | + # PostgreSQL supports arrays |
| 35 | + add_column :auth_tokens, :ip_history, :string, array: true, default: [] |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + # Add security feature timestamps |
| 40 | + unless column_exists?(:auth_tokens, :suspicious_activity_detected_at) |
| 41 | + add_column :auth_tokens, :suspicious_activity_detected_at, :datetime |
| 42 | + end |
| 43 | + |
| 44 | + unless column_exists?(:auth_tokens, :invalidation_requested_at) |
| 45 | + add_column :auth_tokens, :invalidation_requested_at, :datetime |
| 46 | + add_index :auth_tokens, :invalidation_requested_at |
| 47 | + end |
| 48 | + |
| 49 | + unless column_exists?(:auth_tokens, :last_activity_at) |
| 50 | + add_column :auth_tokens, :last_activity_at, :datetime |
| 51 | + end |
| 52 | + |
| 53 | + # Add created_at and updated_at if they don't exist |
| 54 | + unless column_exists?(:auth_tokens, :created_at) && column_exists?(:auth_tokens, :updated_at) |
| 55 | + add_timestamps :auth_tokens, default: -> { 'CURRENT_TIMESTAMP' }, null: false |
| 56 | + end |
| 57 | + |
| 58 | + # ==== SCORM FEATURES ==== |
| 59 | + |
| 60 | + # Add scorm_extensions column if it doesn't exist |
| 61 | + if column_exists?(:tasks, :scorm_extensions) |
| 62 | + Rails.logger.info "Column 'scorm_extensions' already exists in 'tasks' table. Skipping..." |
| 63 | + else |
| 64 | + add_column :tasks, :scorm_extensions, :integer, null: false, default: 0 |
| 65 | + end |
| 66 | + |
| 67 | + # Add columns to task_definitions if they don't exist |
| 68 | + change_table :task_definitions do |t| |
| 69 | + t.boolean :scorm_enabled, default: false unless column_exists?(:task_definitions, :scorm_enabled) |
| 70 | + t.boolean :scorm_allow_review, default: false unless column_exists?(:task_definitions, :scorm_allow_review) |
| 71 | + t.boolean :scorm_bypass_test, default: false unless column_exists?(:task_definitions, :scorm_bypass_test) |
| 72 | + t.boolean :scorm_time_delay_enabled, default: false unless column_exists?(:task_definitions, :scorm_time_delay_enabled) |
| 73 | + t.integer :scorm_attempt_limit, default: 0 unless column_exists?(:task_definitions, :scorm_attempt_limit) |
| 74 | + end |
| 75 | + |
| 76 | + # ==== TASK COMMENTS POLYMORPHIC RELATIONSHIP ==== |
| 77 | + |
| 78 | + # Enable polymorphic relationships for task comments |
| 79 | + remove_index :task_comments, :overseer_assessment_id if index_exists?(:task_comments, :overseer_assessment_id) |
| 80 | + |
| 81 | + add_column :task_comments, :commentable_type, :string unless column_exists?(:task_comments, :commentable_type) |
| 82 | + rename_column :task_comments, :overseer_assessment_id, :commentable_id if column_exists?(:task_comments, :overseer_assessment_id) |
| 83 | + |
| 84 | + if column_exists?(:task_comments, :commentable_id) && column_exists?(:task_comments, :commentable_type) |
| 85 | + # Using find_each to process records individually with validations |
| 86 | + TaskComment.where.not(commentable_id: nil).find_each do |comment| |
| 87 | + comment.update(commentable_type: 'OverseerAssessment') |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + add_index :task_comments, [:commentable_type, :commentable_id] unless index_exists?(:task_comments, [:commentable_type, :commentable_id]) |
| 92 | + |
| 93 | + # ==== TASK DEFINITION FILENAME HANDLING ==== |
| 94 | + |
| 95 | + # Add new_column_name to task_definitions if it doesn't exist |
| 96 | + unless column_exists?(:task_definitions, :new_column_name) |
| 97 | + add_column :task_definitions, :new_column_name, :string |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + def down |
| 102 | + # ==== REVERT AUTH TOKEN SECURITY FEATURES ==== |
| 103 | + |
| 104 | + # Remove timestamps if they exist |
| 105 | + remove_column :auth_tokens, :created_at if column_exists?(:auth_tokens, :created_at) |
| 106 | + remove_column :auth_tokens, :updated_at if column_exists?(:auth_tokens, :updated_at) |
| 107 | + |
| 108 | + # Remove security feature timestamps |
| 109 | + remove_column :auth_tokens, :last_activity_at if column_exists?(:auth_tokens, :last_activity_at) |
| 110 | + |
| 111 | + remove_index :auth_tokens, :invalidation_requested_at if index_exists?(:auth_tokens, :invalidation_requested_at) |
| 112 | + remove_column :auth_tokens, :invalidation_requested_at if column_exists?(:auth_tokens, :invalidation_requested_at) |
| 113 | + |
| 114 | + remove_column :auth_tokens, :suspicious_activity_detected_at if column_exists?(:auth_tokens, :suspicious_activity_detected_at) |
| 115 | + |
| 116 | + # Remove IP history |
| 117 | + remove_column :auth_tokens, :ip_history if column_exists?(:auth_tokens, :ip_history) |
| 118 | + |
| 119 | + # Remove last seen tracking |
| 120 | + remove_column :auth_tokens, :last_seen_ua if column_exists?(:auth_tokens, :last_seen_ua) |
| 121 | + remove_column :auth_tokens, :last_seen_ip if column_exists?(:auth_tokens, :last_seen_ip) |
| 122 | + |
| 123 | + # Remove session binding columns |
| 124 | + remove_column :auth_tokens, :session_user_agent if column_exists?(:auth_tokens, :session_user_agent) |
| 125 | + remove_column :auth_tokens, :session_ip if column_exists?(:auth_tokens, :session_ip) |
| 126 | + |
| 127 | + # Remove token_type |
| 128 | + remove_index :auth_tokens, :token_type if index_exists?(:auth_tokens, :token_type) |
| 129 | + remove_column :auth_tokens, :token_type if column_exists?(:auth_tokens, :token_type) |
| 130 | + |
| 131 | + # ==== REVERT SCORM FEATURES ==== |
| 132 | + |
| 133 | + # Remove scorm_extensions column if it exists |
| 134 | + remove_column :tasks, :scorm_extensions if column_exists?(:tasks, :scorm_extensions) |
| 135 | + |
| 136 | + # Remove columns from task_definitions if they exist |
| 137 | + if column_exists?(:task_definitions, :scorm_enabled) |
| 138 | + change_table :task_definitions do |t| |
| 139 | + t.remove :scorm_enabled, :scorm_allow_review, :scorm_bypass_test, :scorm_time_delay_enabled, :scorm_attempt_limit |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + # ==== REVERT TASK COMMENTS POLYMORPHIC RELATIONSHIP ==== |
| 144 | + |
| 145 | + # Revert polymorphic relationships for task comments |
| 146 | + remove_index :task_comments, [:commentable_type, :commentable_id] if index_exists?(:task_comments, [:commentable_type, :commentable_id]) |
| 147 | + rename_column :task_comments, :commentable_id, :overseer_assessment_id if column_exists?(:task_comments, :commentable_id) |
| 148 | + remove_column :task_comments, :commentable_type if column_exists?(:task_comments, :commentable_type) |
| 149 | + |
| 150 | + add_index :task_comments, :overseer_assessment_id unless index_exists?(:task_comments, :overseer_assessment_id) |
| 151 | + |
| 152 | + # ==== REVERT TASK DEFINITION FILENAME HANDLING ==== |
| 153 | + |
| 154 | + # Remove new_column_name from task_definitions if it exists |
| 155 | + remove_column :task_definitions, :new_column_name if column_exists?(:task_definitions, :new_column_name) |
| 156 | + end |
| 157 | +end |
0 commit comments