Low commit activity in last 3 years
No release in over a year
Adds support for triggers in ActiveRecord
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 13.0
~> 3.0
~> 4.2.0

Runtime

>= 5.2, < 7.1
 Project Readme

Gem Version Build Status Coverage Status

SchemaPlus::Triggers

ScemaPlus::Triggers adds support for SQL triggers in ActiveRecord.

SchemaPlus::Triggers is part of the SchemaPlus family of Ruby on Rails ActiveRecord extension gems.

Installation

As usual:

gem "schema_plus_triggers"                # in a Gemfile
gem.add_dependency "schema_plus_triggers" # in a .gemspec

Compatibility

SchemaPlus::Triggers is tested on:

  • ruby 2.5 with activerecord 5.2, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 2.5 with activerecord 6.0, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 2.5 with activerecord 6.1, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 2.7 with activerecord 5.2, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 2.7 with activerecord 6.0, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 2.7 with activerecord 6.1, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 2.7 with activerecord 7.0, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 3.0 with activerecord 6.0, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 3.0 with activerecord 6.1, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 3.0 with activerecord 7.0, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 3.1 with activerecord 6.0, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 3.1 with activerecord 6.1, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12
  • ruby 3.1 with activerecord 7.0, using postgresql:9.6, postgresql:10, postgresql:11 or postgresql:12

Usage

Note for PostgreSQL

You will need to also add the schema_plus_functions gem and define your trigger function.

create_function :my_trigger_func, '', <<-END
RETURNS trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  BEGIN
    IF (TG_OP = 'DELETE') THEN
      INSERT INTO log (operation, log) VALUES ('DELETE', OLD.name);
    ELSEIF (TG_OP = 'UPDATE') THEN
      INSERT INTO log (operation, log) VALUES ('UPDATE', OLD.name || ' to ' || NEW.name);
    ELSEIF (TG_OP = 'INSERT') THEN
      INSERT INTO log (operation, log) VALUES ('INSERT', NEW.name);
    END IF; 
  END;
END;
$$
END

Migrations

To declare a trigger use create_trigger:

create_trigger :my_table, :my_trigger, 'after insert', 'for each row execute procedure my_trigger_func()' 

To drop a trigger use drop_trigger:

drop_trigger :my_table, :my_trigger

Introspection

You can query the list of triggers defined at the connection level (uncached):

connection.triggers

This will return an array of arrays. The inner array containing the table name and the trigger name.

History

  • 1.0.1 - Add AR 6.1 and 7.0, as well as Ruby 3.1 support
  • 1.0.0 - Drop ruby < 2.5, and add Rails 6.0
  • 0.2.0 - Add testing for multiple PostgreSQL versions
  • 0.1.0 - Initial release

Development & Testing

Are you interested in contributing to SchemaPlus::Triggers? Thanks! Please follow the standard protocol: fork, feature branch, develop, push, and issue pull request.

Some things to know about to help you develop and test:

  • schema_dev: SchemaPlus::Triggers uses schema_dev to facilitate running rspec tests on the matrix of ruby, activerecord, and database versions that the gem supports, both locally and on github actions

    To to run rspec locally on the full matrix, do:

      $ schema_dev bundle install
      $ schema_dev rspec
    

    You can also run on just one configuration at a time; For info, see schema_dev --help or the schema_dev README.

    The matrix of configurations is specified in schema_dev.yml in the project root.

  • schema_plus_core: SchemaPlus::Triggers uses the SchemaPlus::Core API that provides middleware callback stacks to make it easy to extend ActiveRecord's behavior. If that API is missing something you need for your contribution, please head over to schema_plus_core and open an issue or pull request.
  • schema_monkey: SchemaPlus::Triggers is implemented as a schema_monkey client, using schema_monkey's convention-based protocols for extending ActiveRecord and using middleware stacks.