Tidy up homepage and navigation
This commit is contained in:
12
db/migrate/20251108041801_create_dsns.rb
Normal file
12
db/migrate/20251108041801_create_dsns.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
class CreateDsns < ActiveRecord::Migration[8.1]
|
||||
def change
|
||||
create_table :dsns do |t|
|
||||
t.string :key
|
||||
t.string :name
|
||||
t.boolean :enabled, default: true, null: false
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
add_index :dsns, :key, unique: true
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
class RemoveProjectIdFromEvents < ActiveRecord::Migration[8.1]
|
||||
def change
|
||||
remove_reference :events, :project, null: false, foreign_key: true
|
||||
end
|
||||
end
|
||||
5
db/migrate/20251108042208_drop_projects_table.rb
Normal file
5
db/migrate/20251108042208_drop_projects_table.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class DropProjectsTable < ActiveRecord::Migration[8.1]
|
||||
def change
|
||||
drop_table :projects
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,67 @@
|
||||
class AddEventsCountToNetworkRanges < ActiveRecord::Migration[8.1]
|
||||
def up
|
||||
# Add the column with default value
|
||||
add_column :network_ranges, :events_count, :integer, null: false, default: 0
|
||||
|
||||
# Add index for faster queries
|
||||
add_index :network_ranges, :events_count
|
||||
|
||||
# Create trigger function to update counter cache
|
||||
execute <<-SQL
|
||||
CREATE OR REPLACE FUNCTION update_network_range_events_count()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
-- Update all network ranges that contain the IP address
|
||||
UPDATE network_ranges
|
||||
SET events_count = events_count +
|
||||
CASE
|
||||
WHEN TG_OP = 'INSERT' THEN 1
|
||||
WHEN TG_OP = 'DELETE' THEN -1
|
||||
ELSE 0
|
||||
END
|
||||
WHERE network >>= NEW.ip_address::inet;
|
||||
|
||||
RETURN COALESCE(NEW, OLD);
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
SQL
|
||||
|
||||
# Create triggers for events table
|
||||
execute <<-SQL
|
||||
CREATE TRIGGER update_network_ranges_events_count_after_insert
|
||||
AFTER INSERT ON events
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_network_range_events_count();
|
||||
SQL
|
||||
|
||||
execute <<-SQL
|
||||
CREATE TRIGGER update_network_ranges_events_count_after_delete
|
||||
AFTER DELETE ON events
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_network_range_events_count();
|
||||
SQL
|
||||
|
||||
# Backfill existing counts
|
||||
execute <<-SQL
|
||||
UPDATE network_ranges
|
||||
SET events_count = (
|
||||
SELECT COUNT(*)
|
||||
FROM events
|
||||
WHERE events.ip_address <<= network_ranges.network
|
||||
);
|
||||
SQL
|
||||
end
|
||||
|
||||
def down
|
||||
# Drop triggers first
|
||||
execute <<-SQL
|
||||
DROP TRIGGER IF EXISTS update_network_ranges_events_count_after_insert ON events;
|
||||
DROP TRIGGER IF EXISTS update_network_ranges_events_count_after_delete ON events;
|
||||
DROP FUNCTION IF EXISTS update_network_range_events_count();
|
||||
SQL
|
||||
|
||||
# Remove column and index
|
||||
remove_index :network_ranges, :events_count
|
||||
remove_column :network_ranges, :events_count
|
||||
end
|
||||
end
|
||||
20
db/migrate/20251108042936_create_default_dsn.rb
Normal file
20
db/migrate/20251108042936_create_default_dsn.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
class CreateDefaultDsn < ActiveRecord::Migration[8.1]
|
||||
def change
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
# Only create if no DSNs exist
|
||||
if Dsn.count == 0
|
||||
Dsn.create!(
|
||||
name: 'Development DSN',
|
||||
key: 'dev-test-key-1234567890abcdef',
|
||||
enabled: true
|
||||
)
|
||||
end
|
||||
end
|
||||
dir.down do
|
||||
# Remove the default DSN if it exists
|
||||
Dsn.where(key: 'dev-test-key-1234567890abcdef').delete_all
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user