Skip to content

Commit c1577c6

Browse files
committed
- Disgard changes in 20240920052508_convert_task_def_filenames
- Disgard changes in 20241025050957 add_scorm_feat - Add new migration file for consolidated security and features - Consolidate security and feature migrations into one - Ensure that the new migration file is properly formatted and includes all necessary changes
1 parent eb3def3 commit c1577c6

3 files changed

Lines changed: 174 additions & 47 deletions

db/migrate/20240920052508_convert_task_def_filenames.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
class ConvertTaskDefFilenames < ActiveRecord::Migration[7.1]
2-
def change
3-
unless column_exists?(:task_definitions, :new_column_name)
4-
add_column :task_definitions, :new_column_name, :string
5-
end
62

3+
# Check filenames in the upload requirements for each task definition
4+
# and replace any invalid characters using sanitize filename
5+
def change
76
TaskDefinition.find_in_batches do |group|
87
group.each do |task_def|
98
next if task_def.valid?
109

1110
upload_req = task_def.upload_requirements
12-
change = false
1311

12+
change = false
1413
upload_req.each do |req|
1514
unless req['name'].match?(/^[a-zA-Z0-9_\- \.]+$/)
1615
req['name'] = FileHelper.sanitized_filename(req['name'])
@@ -31,10 +30,3 @@ def change
3130
end
3231
end
3332
end
34-
35-
class AddAuthTokenType < ActiveRecord::Migration[7.1]
36-
def change
37-
add_column :auth_tokens, :token_type, :integer, null: false, default: 0
38-
add_index :auth_tokens, :token_type
39-
end
40-
end
Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,24 @@
11
class AddScormFeat < ActiveRecord::Migration[7.1]
2-
def up
3-
# Add scorm_extensions column if it doesn't exist
4-
if column_exists?(:tasks, :scorm_extensions)
5-
add_column :tasks, :scorm_extensions, :integer, null: false, default: 0
6-
else
7-
Rails.logger.info "Column 'scorm_extensions' already exists in 'tasks' table. Skipping..."
8-
end
2+
def change
3+
# Record scorm extensions added to a task
4+
add_column :tasks, :scorm_extensions, :integer, null: false, default: 0
95

10-
# Add columns to task_definitions if they don't exist
116
change_table :task_definitions do |t|
12-
t.boolean :scorm_enabled, default: false unless column_exists?(:task_definitions, :scorm_enabled)
13-
t.boolean :scorm_allow_review, default: false unless column_exists?(:task_definitions, :scorm_allow_review)
14-
t.boolean :scorm_bypass_test, default: false unless column_exists?(:task_definitions, :scorm_bypass_test)
15-
t.boolean :scorm_time_delay_enabled, default: false unless column_exists?(:task_definitions, :scorm_time_delay_enabled)
16-
t.integer :scorm_attempt_limit, default: 0 unless column_exists?(:task_definitions, :scorm_attempt_limit)
7+
t.boolean :scorm_enabled, default: false
8+
t.boolean :scorm_allow_review, default: false
9+
t.boolean :scorm_bypass_test, default: false
10+
t.boolean :scorm_time_delay_enabled, default: false
11+
t.integer :scorm_attempt_limit, default: 0
1712
end
1813

1914
# Enable polymorphic relationships for task comments
20-
remove_index :task_comments, :overseer_assessment_id if index_exists?(:task_comments, :overseer_assessment_id)
21-
22-
add_column :task_comments, :commentable_type, :string unless column_exists?(:task_comments, :commentable_type)
23-
rename_column :task_comments, :overseer_assessment_id, :commentable_id if column_exists?(:task_comments, :overseer_assessment_id)
24-
25-
TaskComment.where.not(commentable_id: nil).in_batches.update_all(commentable_type: 'OverseerAssessment')
26-
27-
add_index :task_comments, [:commentable_type, :commentable_id] unless index_exists?(:task_comments, [:commentable_type, :commentable_id])
28-
end
29-
30-
def down
31-
# Remove scorm_extensions column if it exists
32-
remove_column :tasks, :scorm_extensions if column_exists?(:tasks, :scorm_extensions)
15+
remove_index :task_comments, :overseer_assessment_id
3316

34-
# Remove columns from task_definitions if they exist
35-
change_table :task_definitions do |t|
36-
t.remove :scorm_enabled, :scorm_allow_review, :scorm_bypass_test, :scorm_time_delay_enabled, :scorm_attempt_limit if column_exists?(:task_definitions, :scorm_enabled)
37-
end
17+
add_column :task_comments, :commentable_type, :string
18+
rename_column :task_comments, :overseer_assessment_id, :commentable_id
3819

39-
# Revert polymorphic relationships for task comments
40-
remove_index :task_comments, [:commentable_type, :commentable_id] if index_exists?(:task_comments, [:commentable_type, :commentable_id])
41-
rename_column :task_comments, :commentable_id, :overseer_assessment_id if column_exists?(:task_comments, :commentable_id)
42-
remove_column :task_comments, :commentable_type if column_exists?(:task_comments, :commentable_type)
20+
TaskComment.where('NOT commentable_id IS NULL').in_batches.update_all(commentable_type: 'OverseerAssessment')
4321

44-
add_index :task_comments, :overseer_assessment_id unless index_exists?(:task_comments, :overseer_assessment_id)
22+
add_index :task_comments, [:commentable_type, :commentable_id]
4523
end
4624
end
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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

Comments
 (0)