A Rails application demonstrating PostgreSQL triggers using the pg_sql_triggers gem. This example project showcases various trigger features including timing (BEFORE/AFTER), conditions (WHEN clauses), and interactive testing capabilities.
- Timing Control: Triggers can fire BEFORE or AFTER database operations
- Conditional Triggers: Triggers can use SQL WHEN clauses to conditionally execute
- Interactive Test UI: Web-based interface for testing all trigger features
- Trigger Management: Enable/disable triggers dynamically through the UI
- Multiple Examples: Email validation, slug generation, audit logging, and more
- Ruby (see Gemfile for version requirements)
- PostgreSQL
- Rails 8.0+
-
Install dependencies:
bundle install
-
Set up the database:
bin/rails db:create bin/rails db:migrate
-
Start the Rails server:
bin/rails server
-
Access the application:
- Test UI:
http://localhost:3000- Interactive interface for testing triggers - Trigger Management UI:
http://localhost:3000/pg_sql_triggers- Full engine UI for managing triggers
- Test UI:
- User Email Validation - Validates email format before insert/update
- Post Slug Generation - Automatically generates slugs from titles (BEFORE trigger)
- Order Total Validation - Ensures order totals are positive
- Order Status Validation - Validates order status with conditional execution
- User Audit Logging - Logs user changes to audit_logs table (AFTER trigger)
- Comment Count Update - Updates comment counts on posts
The application includes an interactive test UI accessible at the root path. You can test all trigger features through the web interface, or refer to TEST_EXAMPLES.md for comprehensive test scenarios and examples.
To run the test suite:
bin/rails test- Test Examples: See
TEST_EXAMPLES.mdfor detailed test scenarios - Environment Setup: See
ENV_SETUP.mdfor environment variable configuration
app/triggers/- Trigger definitions using the PgSqlTriggers DSLdb/triggers/- Trigger migrationsapp/controllers/trigger_tests_controller.rb- Test UI controllerapp/views/trigger_tests/- Test UI viewsconfig/initializers/pg_sql_triggers.rb- Trigger engine configuration