Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/mirek/db-logic
- Owner: mirek
- License: mit
- Created: 2010-05-13T22:58:57.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2010-05-14T00:12:54.000Z (over 14 years ago)
- Last Synced: 2024-05-09T16:18:35.652Z (8 months ago)
- Language: Ruby
- Homepage:
- Size: 97.7 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.mkdn
- License: MIT-LICENSE
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.sqlUpdate 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