A Ruby gem that adds database constraint methods to ActiveRecord migrations for PostgreSQL data integrity.
Rein is a Ruby gem that adds a DSL to ActiveRecord migrations for defining database constraints in PostgreSQL. It solves the problem of enforcing data integrity at the database level rather than just at the application level, which ActiveRecord doesn't natively support well. The gem provides methods for foreign keys, uniqueness, checks, and other constraints without requiring hand-written SQL.
Ruby on Rails developers using PostgreSQL who need to enforce data integrity constraints directly in the database schema through migrations.
Developers choose Rein because it provides a clean, reversible ActiveRecord migration interface to PostgreSQL constraints, avoiding raw SQL while ensuring robust data integrity. Its support for NOT VALID constraints allows safe addition of constraints on large tables without downtime.
Database constraints made easy for ActiveRecord.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Provides a wide range of constraint methods (foreign key, unique, exclusion, check, etc.) with detailed examples in the README, eliminating the need for raw SQL in migrations.
All constraint methods are automatically reversible in Rails migrations, simplifying rollbacks without manual down methods, as stated in the philosophy section.
Enables adding CHECK and foreign key constraints with validate: false to avoid locking large tables during migration, detailed in the 'Validate Constraints' section for safer production use.
Supports if options for constraints like null and inclusion, allowing enforcement based on row-specific conditions, as shown in examples like add_null_constraint with if: "state = 'on_loan'".
Exclusively designed for PostgreSQL, making it unusable for projects with MySQL, SQLite, or other databases, limiting its applicability in multi-database environments.
Unique and exclusion constraints do not support the NOT VALID option, forcing full validation that can lock tables during migration, as noted in the constraint summary table.
Setting up exclusion constraints requires specifying operators and GiST indexes with syntax like [[:book_id, '='], [:owned_during, '&&']], which can be error-prone and less intuitive than other constraints.