Skip to content

patrick204nqh/dbwatcher

Repository files navigation

dbwatcher Logo

dbwatcher

🔍 Track, visualize, and debug database operations in your Rails applications

CI Release Gem Gem Version RubyGems Downloads License: MIT Maintainability Code Coverage

A Rails gem that tracks and visualizes database operations in your application. Built for developers who need to understand complex data flows and debug database interactions.

Why dbwatcher?

When developing Rails applications, understanding what database operations occur during code execution can be challenging. dbwatcher provides a simple way to:

  • Track database changes within specific code blocks
  • Monitor SQL operations during HTTP requests
  • Visualize database relationships and model associations
  • Debug complex data flows with an intuitive web interface

Key Features

  • Database Operation Tracking - Capture SQL operations (INSERT, UPDATE, DELETE)
  • Targeted Monitoring - Track specific code blocks or entire HTTP requests
  • Interactive Dashboard - Clean web interface for exploring captured data
  • Relationship Diagrams - Visualize database relationships and model associations
  • Simple Setup - File-based storage with zero additional database requirements
  • Development Ready - Designed specifically for development environments

Screenshots

Dashboard Interface

image

Session View

image

View more screenshots in here →

Installation

Add to your Gemfile:

gem 'dbwatcher', group: :development

Install the gem:

bundle install

The dashboard automatically becomes available at /dbwatcher in your Rails application.

Usage

Track Code Blocks

Monitor specific operations:

Dbwatcher.track(name: "User Registration") do
  user = User.create!(name: "John", email: "john@example.com")
  user.create_profile!(bio: "Developer")
  user.posts.create!(title: "Hello World")
end

Track HTTP Requests

Add ?dbwatch=true to any URL:

GET /users/123?dbwatch=true
POST /api/users?dbwatch=true

View Results

Visit /dbwatcher in your browser to explore tracked operations.

Configuration

dbwatcher works out of the box with zero configuration - simply install the gem and visit /dbwatcher in your Rails application.

Configuration Options

View All Configuration Settings
Setting Type Default Description
Core Settings
enabled Boolean true Enable or disable DBWatcher
storage_path String "tmp/dbwatcher" Directory for session data storage
Session Management
max_sessions Integer 50 Maximum number of sessions to retain
auto_clean_days Integer 7 Automatically delete sessions older than N days
Query Tracking
track_queries Boolean false Enable full SQL query tracking (resource intensive)
System Information
system_info Boolean true Collect system information for debugging
debug_mode Boolean false Enable detailed debug logging
Database Diagram Options
diagram_show_attributes Boolean true Display model attributes in diagrams
diagram_show_cardinality Boolean true Show relationship cardinality indicators
diagram_show_methods Boolean false Include model methods in diagrams
diagram_max_attributes Integer 10 Maximum attributes displayed per model
diagram_attribute_types Boolean true Show data types for attributes
diagram_relationship_labels Boolean true Display labels on relationship lines

Configuration Example

# config/environments/development.rb
Rails.application.configure do
  config.dbwatcher.enabled = true
  config.dbwatcher.max_sessions = 100
  config.dbwatcher.track_queries = true
end

Advanced Features

Custom Metadata

Add context to your tracking:

Dbwatcher.track(
  name: "Order Processing",
  metadata: { user_id: current_user.id, order_type: "premium" }
) do
  # Database operations here
end

Access Current Session

Access the current tracking session:

session = Dbwatcher.current_session
puts "Session ID: #{session.id}"
puts "Total changes: #{session.changes.count}"

Clear All Data

Remove all stored sessions and queries:

Dbwatcher.clear_all

Development

Running Tests

bundle exec rake test        # All tests
bundle exec rake unit        # Unit tests only
bundle exec rake acceptance  # Feature tests only

Code Coverage

The project uses SimpleCov for code coverage and uploads results to Qlty. To run tests with coverage locally:

COVERAGE=true bundle exec rake test

Coverage reports will be generated in the coverage/ directory.

Local Development

cd spec/dummy
bundle exec rails server -p 3001
open http://localhost:3001/dbwatcher

Code Quality

bundle exec rubocop    # Linting
bundle exec brakeman   # Security analysis

Changelog →

Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b my-feature
  3. Make changes and add tests
  4. Run tests: bundle exec rake test
  5. Run linter: bundle exec rubocop
  6. Commit changes: git commit -am 'Add feature'
  7. Push branch: git push origin my-feature
  8. Open a Pull Request

Contributing guidelines →

Resources

License

Released under the MIT License.