Many updates
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
class RenameRuleMatchedToRuleIdInEvents < ActiveRecord::Migration[8.1]
|
||||
def change
|
||||
# Add new rule_id column (instant - just metadata change)
|
||||
add_column :events, :rule_id, :bigint
|
||||
|
||||
# Drop old rule_matched string column (instant - no data to migrate)
|
||||
remove_column :events, :rule_matched, :string
|
||||
|
||||
# Add foreign key constraint (fast - all values are NULL)
|
||||
add_foreign_key :events, :rules
|
||||
|
||||
# Add index for analytics queries (fast - mostly NULL values)
|
||||
add_index :events, :rule_id
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
class RenameEventIdToRequestIdInEvents < ActiveRecord::Migration[8.1]
|
||||
def change
|
||||
# Only rename the column if it still exists as event_id
|
||||
if column_exists?(:events, :event_id)
|
||||
rename_column :events, :event_id, :request_id
|
||||
end
|
||||
|
||||
# Rename the unique index if it still exists with the old name
|
||||
if index_name_exists?(:events, :index_events_on_event_id)
|
||||
rename_index :events, :index_events_on_event_id, :index_events_on_request_id
|
||||
elsif !index_name_exists?(:events, :index_events_on_request_id)
|
||||
# Create the index with the new name if neither exists
|
||||
add_index :events, :request_id, unique: true, name: :index_events_on_request_id
|
||||
end
|
||||
end
|
||||
end
|
||||
37
db/migrate/20251113031234_add_enums_to_rules.rb
Normal file
37
db/migrate/20251113031234_add_enums_to_rules.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
class AddEnumsToRules < ActiveRecord::Migration[8.1]
|
||||
def change
|
||||
# Add enum columns with default values
|
||||
add_column :rules, :waf_action, :integer, default: 0, null: false
|
||||
add_column :rules, :waf_rule_type, :integer, default: 0, null: false
|
||||
|
||||
# Add indexes for enum columns
|
||||
add_index :rules, :waf_action
|
||||
add_index :rules, :waf_rule_type
|
||||
|
||||
# Migrate existing data
|
||||
# Map action strings to integers (starting from 0 to match Rails enum convention)
|
||||
execute <<-SQL
|
||||
UPDATE rules
|
||||
SET waf_action = CASE action
|
||||
WHEN 'allow' THEN 0
|
||||
WHEN 'deny' THEN 1
|
||||
WHEN 'rate_limit' THEN 2
|
||||
WHEN 'redirect' THEN 3
|
||||
WHEN 'log' THEN 4
|
||||
WHEN 'challenge' THEN 5
|
||||
ELSE 0
|
||||
END;
|
||||
SQL
|
||||
|
||||
# Map rule_type strings to integers
|
||||
execute <<-SQL
|
||||
UPDATE rules
|
||||
SET waf_rule_type = CASE rule_type
|
||||
WHEN 'network' THEN 0
|
||||
WHEN 'rate_limit' THEN 1
|
||||
WHEN 'path_pattern' THEN 2
|
||||
ELSE 0
|
||||
END;
|
||||
SQL
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user