Skip to content

Allows database views to be stored as sql files and be easily applyable

License

Notifications You must be signed in to change notification settings

stevo/activerecord-database-views

Repository files navigation

activerecord-database-views Code Climate

Allows database views to be stored as sql files and be easily applyable

Installation

Just add following line to your Gemfile

gem 'activerecord-database-views'

and run bundle install

Usage

Place your SQL that view will be generated from into db\views directory (The actual view name will be the same as file name), i.e.

-- db\views\reverse_users.sql
SELECT * FROM users ORDER BY users.id DESC

Then run console (console c) and execute...

ActiveRecord::DatabaseViews.reload!

...to reload all defined views.

You can test them out by

ActiveRecord::Base.connection.execute('SELECT * FROM reverse_users').to_a

or just attach them to a model file

class ReverseUser < ActiveRecord::Base
end

and run

ReverseUser.all

How to run some code with views dropped

This is sometimes necessary in case of some changing migrations

ActiveRecord::DatabaseViews.without do
    # some code that has to be executed with views dropped
end

How to force rails to return instances of original class and not view class

discriminate_class_for_record to the rescue :)

class ReverseUser < ActiveRecord::Base
  private
    
  def self.discriminate_class_for_record(record) 
    User
  end
end

How to reload views automatically after migrations

Add new file hooks.rake under lib\tasks and copy following code

db_tasks = %w[db:migrate db:rollback db:schema:load]

namespace :reload_views do
  db_tasks.each do |task_name|
    task task_name => %w[environment db:load_config] do
      ActiveRecord::DatabaseViews.reload!
    end
  end
end

db_tasks.each do |task_name|
  Rake::Task[task_name].enhance do
    Rake::Task["reload_views:#{task_name}"].invoke
  end
end

As a bonus you will be able to reload views using rake task as well, i.e. rake reload_views:db:migrate

About

Allows database views to be stored as sql files and be easily applyable

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages