Module: Hypershield

Defined in:
lib/hypershield.rb,
lib/hypershield/engine.rb,
lib/hypershield/version.rb,
lib/hypershield/migration.rb,
lib/generators/hypershield/install_generator.rb

Defined Under Namespace

Modules: Generators, Migration Classes: Engine

Constant Summary collapse

VERSION =
"0.3.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledObject

Returns the value of attribute enabled.



13
14
15
# File 'lib/hypershield.rb', line 13

def enabled
  @enabled
end

.log_sqlObject

Returns the value of attribute log_sql.



13
14
15
# File 'lib/hypershield.rb', line 13

def log_sql
  @log_sql
end

.schemasObject

Returns the value of attribute schemas.



13
14
15
# File 'lib/hypershield.rb', line 13

def schemas
  @schemas
end

Class Method Details

.drop_view(view) ⇒ Object



25
26
27
28
29
# File 'lib/hypershield.rb', line 25

def drop_view(view)
  schemas.each do |schema, _|
    execute("DROP VIEW IF EXISTS #{quote_ident(schema)}.#{quote_ident(view)}")
  end
end

.refresh(dry_run: false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hypershield.rb', line 31

def refresh(dry_run: false)
  if adapter_name =~ /sqlite/i
    raise "Adapter not supported: #{adapter_name}"
  end

  quiet_logging do
    statements = []

    schemas.each do |schema, config|
      hide = config[:hide].to_a
      show = config[:show].to_a

      hypershield_tables = tables(schema)

      tables.sort_by { |k, _| k }.each do |table, columns|
        next if table == "pg_stat_statements"

        columns.reject! do |column|
          hide.any? { |m| "#{table}.#{column}".include?(m) } &&
            !show.any? { |m| "#{table}.#{column}".include?(m) }
        end

        # if the hypershield view has the same columns, assume it doesn't need updated
        # this may not necessarily be true if someone manually updates the view
        # we could check the view definition, but this is harder as the database normalizes it
        next if hypershield_tables[table] == columns

        statements << "DROP VIEW IF EXISTS #{quote_ident(schema)}.#{quote_ident(table)} CASCADE"

        if columns.any?
          statements << "CREATE VIEW #{quote_ident(schema)}.#{quote_ident(table)} AS SELECT #{columns.map { |c| quote_ident(c) }.join(", ")} FROM #{quote_ident(table)}"
        end
      end
    end

    if dry_run
      if statements.any?
        puts statements.map { |v| "#{v};" }.join("\n")
      end
    else
      # originally this was performed in a transaction
      # however, this appears to cause issues in certain situations - see #5 and #6
      # this shouldn't be a huge issue now that we only update specific views
      # we already drop views outside of the transaction when migrations are run
      statements.each do |statement|
        execute(statement)
      end
    end
  end
end