Class: SqlView::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_view.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#parentObject (readonly)

Returns the value of attribute parent.



60
61
62
# File 'lib/sql_view.rb', line 60

def parent
  @parent
end

Instance Method Details

#downObject



84
85
86
87
88
89
# File 'lib/sql_view.rb', line 84

def down
  sql = "  DROP\#{materialized_or_not}VIEW IF EXISTS \#{parent.view_name};\n"
  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 = "  REFRESH\#{materialized_or_not}VIEW\#{concurrently_or_not}\#{parent.view_name};\n  SQL\n  execute(sql, log: false)\nend\n"

#upObject



74
75
76
77
78
79
80
81
82
# File 'lib/sql_view.rb', line 74

def up
  view_sql = parent.sql_view_options[:sql_or_proc].call
  raise "Please configure schema for #{parent} (association or SQL) for the view" if view_sql.to_s == ""
  sql = "  CREATE\#{materialized_or_not}VIEW \#{parent.view_name} AS\n  \#{view_sql.respond_to?(:to_sql) ? view_sql.to_sql : view_sql };\n"
  execute(sql)
end