Class: SqlView::Migration
- Inherits:
-
Object
- Object
- SqlView::Migration
- Defined in:
- lib/sql_view.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #down ⇒ Object
- #execute(sql, log: true) ⇒ Object
-
#initialize(parent) ⇒ Migration
constructor
A new instance of Migration.
- #refresh(concurrently: false) ⇒ Object
- #up ⇒ Object
Constructor Details
#initialize(parent) ⇒ Migration
Returns a new instance of Migration.
62 63 64 |
# File 'lib/sql_view.rb', line 62 def initialize(parent) @parent = parent end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
60 61 62 |
# File 'lib/sql_view.rb', line 60 def parent @parent end |
Instance Method Details
#down ⇒ Object
84 85 86 87 88 89 |
# File 'lib/sql_view.rb', line 84 def down sql = <<-SQL DROP#{materialized_or_not}VIEW IF EXISTS #{parent.view_name}; SQL execute(sql) end |
#execute(sql, log: true) ⇒ Object
91 92 93 94 |
# File 'lib/sql_view.rb', line 91 def execute(sql, log: true) SqlView.log(sql) if log ActiveRecord::Base.connection.execute sql#.wp end |
#refresh(concurrently: false) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/sql_view.rb', line 66 def refresh(concurrently: false) concurrently_or_not = concurrently ? " CONCURRENTLY " : " " sql = <<-SQL REFRESH#{materialized_or_not}VIEW#{concurrently_or_not}#{parent.view_name}; SQL execute(sql, log: false) end |
#up ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/sql_view.rb', line 74 def up view_sql = parent.[:sql_or_proc].call raise "Please configure schema for #{parent} (association or SQL) for the view" if view_sql.to_s == "" sql = <<-SQL CREATE#{materialized_or_not}VIEW #{parent.view_name} AS #{view_sql.respond_to?(:to_sql) ? view_sql.to_sql : view_sql }; SQL execute(sql) end |