Skip to content

andrewtimberlake/procedural

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Procedural Build Status Code Climate

Procedural adds the ability to add functions and triggers via ActiveRecord migrations.

Thanks to Spectacles where I took much inspiration for the groundwork of this gem.

Installation

gem install procedural   #=> or include it in your Gemfile

Example Migration

class AddTimestampTriggers < ActiveRecord::Migration
  def change
    create_procedure :created_at_trigger, language: 'plpgsql', returns: 'trigger' do
      <<-SQL
        IF (TG_OP = 'UPDATE') THEN
          NEW."created_at" := OLD."created_at";
        ELSIF (TG_OP = 'INSERT') THEN
          NEW."created_at" := CURRENT_TIMESTAMP;
        END IF;
        RETURN NEW;
      SQL
    end

    create_procedure :updated_at_trigger, language: 'plpgsql', returns: 'trigger' do
      <<-SQL
        NEW."updated_at" := CURRENT_TIMESTAMP;
        RETURN NEW;
      SQL
    end

    create_trigger :users, :users_created_at, :created_at_trigger
    create_trigger :users, :users_updated_at, :updated_at_trigger

    create_trigger :posts, :posts_created_at, :created_at_trigger
    create_trigger :posts, :posts_updated_at, :updated_at_trigger
  end
end

License

The MIT License (MIT)

Copyright (c) 2015 Andrew Timberlake

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Support for Postgresql stored procedures and functions in ActiveRecord migrations

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages