Skip to content

Better view management though migrations

License

Notifications You must be signed in to change notification settings

davidabenson/migration_view

Repository files navigation

MigrationView

I wanted a better way to manage views. More like the way liquibase does.

Objectives:

  1. Create view in standard rails migration.

  2. Have views saved in db/views/#{view}.sql file

  3. When view file is modified, reload the view after all other migrations are run.

  4. If view is deleted recreate it.

  5. If view deleted from other delete view x cascade. recreate the deleted view.

  6. Have task to reload views, rake db:reload:view

  7. Natively support both postgres and mysql

Install

“gem ‘migration_view’, git: ‘github.com/davidabenson/migration_view.git’”

bundle install

rails generate migration_view:install

rake db:migrate

Tasks available

rake db:reload:view
rake db:reload:procs

Create a view

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

Delete view

def change
  cascade = true
  MigrationView::drop_view('product_view', cascade)
end

Create procedure

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

Debugging

In you migration have:

class MigrationName < ActiveRecord::Migration
  def change
      Rails.logger = Logger.new(STDOUT)
  end
end

This project rocks and uses MIT-LICENSE.

About

Better view management though migrations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published