GithubHelp home page GithubHelp logo

eladmeidar / plainviews Goto Github PK

View Code? Open in Web Editor NEW
23.0 3.0 1.0 94 KB

an ActiveRecord migration DSL for creating database views

Home Page: http://blog.eizesus.com/2009/11/plain-view-add-view-to-your-rails-applications

License: MIT License

Ruby 100.00%

plainviews's Introduction

PlainView

PlainView adds another tool to your Rails migrations toolkit create_view (and drop_view of course).
which will allow you to harness the power of database views in your application.

Installation

as for now this is only available as a plugin, install as follows:

script/plugin install git://github.com/eladmeidar/PlainViews.git

Usage

create a migration

First you’ll have to generate a migration

script/generate migration create_users_and_views

Than, on self.up you can use the create_view helper:

class TestView < ActiveRecord::Migration
def self.up
create_view :v_people do |t|
t.base_model :user
t.select :select => ‘id, name’, :conditions => {:name => ’elad’}
t.use_security_mode :definer
t.use_algorithm :merge
t.use_check_option :cascaded
end
end

def self.down drop_view :v_people end

end

  • select – required, select is the actual select statement, you can either use ActiveRecord#find conventions or specify a plain test query.
  • base_mode this is used only if you use the ActiveRecord conventions on the select statement, otherwise it is not required
  • use_security – i don’t know if this is supported on all databases, but in MySQL you can choose either :merge or :temptable.
  • use_algorithm – again, don’t know if this is a cross DBMS option but on MySQL you can choose between :definer or :invoker
  • check_option – same deal, on MySQL you can choose between :cascaded and :local
  • use_raw_sql – if you use this, all other options are not taken into consideration, use this to use raw sql for your view just like AR’s @find_by_sql*

creating a model

based on the name you chose to your view, you can create a model based on the existing rails naming conventions,
you can always name it whatever you want and specify the view name in set_table_name.

Note that the view model inherits from ActiveRecord::View and not from ActiveRecord::Base:

class ThisIsAView < ActiveRecord::View
  set_table_name 'special_view_name'
end

By default you can’t add/remove/update records on the view since not all DBMS support that. you can easily override the readonly method if you fill like it.

Database support

I investigated a bit, and the basic usage (select / base_model) should run successfully on:

  • MySQL
  • OCI
  • Oracle
  • Postgresql
  • SQLite
  • SQLServer

although i was unable to actually test it on all of them, help is appreciated here :)

Tests

Run rake. select the db you want to test by using an env var

> DB=mysql rake

don’t forget to update the plugin’s database.yml in test/config

Todos

plainviews's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

pacovell

plainviews's Issues

INVOKER syntax creates difficulty with test database

Using MySQL 5.5; The "show create view mutual_account_friends;" command that is used to create schema.rb generates the proper SQL:

v.use_raw_sql 'CREATE ALGORITHM=MERGE DEFINER=`server_dev`@`localhost` SQL SECURITY INVOKER VIEW `mutual_account_friends` AS select `af1`.`id` AS `id`,`af1`.`account_id` AS `account_id`,`af1`.`friend_id` AS `friend_id`,`af1`.`created_at` AS `created_at`,`af1`.`updated_at` AS `updated_at` from (`account_friends` `af1` join `account_friends` `af2` on((`af1`.`account_id` = `af2`.`friend_id`))) where ((`af1`.`account_id` = `af2`.`friend_id`) and (`af1`.`friend_id` = `af2`.`account_id`))'

However, the DEFINER=server_dev@localhost component means that this schema line can't be applied unless the user has SUPER privileges (or you use the same user throughout). I want the security model to be the invoker anyway, so everything is fine if I remove the DEFINER section. I am not sure if this is a risky approach, so I'll try to put it in a fork of the plugin and you can decide if it's an issue or not.

Cheers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.