Class: Monocle::View
- Inherits:
-
Object
- Object
- Monocle::View
- Defined in:
- lib/monocle/view.rb
Instance Attribute Summary collapse
-
#dependants ⇒ Object
Returns the value of attribute dependants.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #create ⇒ Object
- #create_command ⇒ Object
- #drop ⇒ Object
- #drop_command ⇒ Object
- #drop_requested? ⇒ Boolean
- #exists? ⇒ Boolean
-
#initialize(name) ⇒ View
constructor
A new instance of View.
- #materialized? ⇒ Boolean
- #migrate ⇒ Object
- #path_for_sql ⇒ Object
- #refresh(concurrently: false) ⇒ Object
- #slug ⇒ Object
Constructor Details
#initialize(name) ⇒ View
Returns a new instance of View.
10 11 12 13 |
# File 'lib/monocle/view.rb', line 10 def initialize(name) @name = name @dependants = [] end |
Instance Attribute Details
#dependants ⇒ Object
Returns the value of attribute dependants.
5 6 7 |
# File 'lib/monocle/view.rb', line 5 def dependants @dependants end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/monocle/view.rb', line 4 def name @name end |
Instance Method Details
#create ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/monocle/view.rb', line 30 def create debug "Creating #{name}..." execute create_command Migration.find_or_create_by version: slug dependants.each &:create true rescue ActiveRecord::StatementInvalid => e # We may have another new view coming that this view depend on # if the relation name is included on our list of views, we create # that first and then retry if e. =~ /PG::UndefinedTable/ && e..scan(/relation \"(\w+)\" does not exist/) && list.keys.include?($1.to_sym) warn "Can't create #{name} because it depends on #{$1}, creating that first..." list.fetch($1.to_sym).create retry else fail e end end |
#create_command ⇒ Object
103 104 105 |
# File 'lib/monocle/view.rb', line 103 def create_command @create_command ||= File.read(path_for_sql) end |
#drop ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/monocle/view.rb', line 22 def drop debug "Dropping #{name}..." self.dependants = get_dependants_from_pg dependants.each &:drop execute drop_command true end |
#drop_command ⇒ Object
98 99 100 101 |
# File 'lib/monocle/view.rb', line 98 def drop_command _materialized = 'MATERIALIZED' if materialized? "DROP #{_materialized} VIEW IF EXISTS #{name};" end |
#drop_requested? ⇒ Boolean
19 20 |
# File 'lib/monocle/view.rb', line 19 def drop_requested? end |
#exists? ⇒ Boolean
111 112 113 |
# File 'lib/monocle/view.rb', line 111 def exists? execute(check_if_view_exists_sql).entries.map(&:values).flatten.first end |
#materialized? ⇒ Boolean
15 16 17 |
# File 'lib/monocle/view.rb', line 15 def materialized? !!(@materialized ||= create_command =~ /MATERIALIZED VIEW/i) end |
#migrate ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/monocle/view.rb', line 51 def migrate if :drop == slug drop info "#{name} dropped." return true end if versions.include?(slug) debug "Skipping #{name} as it's already up to date." true else status = drop && create info "#{name} migrated to #{slug}!" status end end |
#path_for_sql ⇒ Object
107 108 109 |
# File 'lib/monocle/view.rb', line 107 def path_for_sql @path_for_sql ||= File.join views_path, "#{name}.sql" end |
#refresh(concurrently: false) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/monocle/view.rb', line 67 def refresh(concurrently: false) if :drop == slug drop info "#{name} dropped." return true end # We don't refresh normal views return false unless materialized? _concurrently = " CONCURRENTLY" if concurrently refresh_cmd = "REFRESH MATERIALIZED VIEW#{_concurrently} #{name}" puts "MONOCLE:REFRESH - REFRESH MATERIALIZED VIEW#{_concurrently} #{name}" execute refresh_cmd true rescue ActiveRecord::StatementInvalid => e # This view is trying to select from a different view that hasn't been # populated. if e. =~ /PG::ObjectNotInPrerequisiteState/ && e..scan(/materialized view \"(\w+)\" has not been populated/) && list.keys.include?($1.to_sym) warn "Can't refresh #{name} because it depends on #{$1} which hasn't been populated, refreshing that first..." list.fetch($1.to_sym).refresh retry else fail e end end |
#slug ⇒ Object
94 95 96 |
# File 'lib/monocle/view.rb', line 94 def slug @slug ||= VersionGenerator.new(path_for_sql).generate end |