I wanted a better way to manage views. More like the way liquibase does.
Objectives:
-
Create view in standard rails migration.
-
Have views saved in db/views/#{view}.sql file
-
When view file is modified, reload the view after all other migrations are run.
-
If view is deleted recreate it.
-
If view deleted from other delete view x cascade. recreate the deleted view.
-
Have task to reload views, rake db:reload:view
-
Natively support both postgres and mysql
“gem ‘migration_view’, git: ‘github.com/davidabenson/migration_view.git’”
bundle install
rails generate migration_view:install
rake db:migrate
rake db:reload:view rake db:reload:procs
def change sql=<<-SQL_CODE create or replace view product_view as select id, name from product SQL_CODE MigrationView::create_view('product_view', sql) end
def change cascade = true MigrationView::drop_view('product_view', cascade) end
def change sql=<<-SQL_CODE CREATE PROCEDURE products() BEGIN select * from products; END; END; SQL_CODE drop_if_exists = true MigrationView::drop_view('products', drop_if_exists) end
In you migration have:
class MigrationName < ActiveRecord::Migration def change Rails.logger = Logger.new(STDOUT) end end
This project rocks and uses MIT-LICENSE.