Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mirek/db-logic

Ruby on Rails database logic support
https://github.com/mirek/db-logic

Last synced: about 1 month ago
JSON representation

Ruby on Rails database logic support

Awesome Lists containing this project

README

        

# db:logic

Ruby on Rails database logic support.

# Usage

rails plugin install git://github.com/mirek/db-logic.git

In `#{Rails.root}/db/logic` add new files for your functions, procedures, triggers, events, views etc:

event.foo.sql
view.foo.sql
trigger.table.foo.sql
function.foo.sql
procedure.foo.sql

Update database logic with:

rake db:logic:update

With capistrano:

cap deploy deploy:db:logic

## FAQ

### There are no migrations for db:logic?

No. Think of database logic as database seeds. It's invoked only once at the top all current migrations.
You guarantee successful run only for the most up to date db schema (just like seeds).

## Examples

### Functions

Example mysql function `db/logic/function.country_name_by_ip_address.sql` using geoip table:

delimiter $$

drop function if exists `country_name_by_ip_address`$$

create function `country_name_by_ip_address`(ip_address char(15))
returns varchar(255)
deterministic
reads sql data
comment 'Country name by ip address (as string)'
begin
declare r varchar(255) default null;
select
name
from
geo_countries
where
inet_aton(ip_address) >= from_ip_number and
inet_aton(ip_address) <= to_ip_number
limit
1
into
r;
return r;
end$$

delimiter ;

### Procedures

### Triggers

Example mysql `trigger.users.create_user_login_event.sql` trigger creating new events after user login (after `User#last_login_at` update):

delimiter $$

drop trigger /*!50032 if exists */ `users_create_user_login_event`$$

create trigger `users_create_user_login_event` after update on `users`
for each row begin
declare r bool;
if (old.last_login_at <> new.last_login_at) then
select
create_event(
'USER_LOGIN',
'User',
new.id,
null,
null,
concat(
new.username,
' just logged in from ',
ifnull(
country_name_by_ip_address(new.last_login_ip_address),
' unknown place on earth'
)
),
null
) into r;
end if;
end$$

delimiter ;

### Events

### Views

## Copyright

Copyright (c) 2010 Mirek Rusin, released under the MIT license