Class: MkAcl::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/mikras_utils/mkacl/analyzer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec, conn, warn: true) ⇒ Analyzer



10
11
12
13
14
15
# File 'lib/mikras_utils/mkacl/analyzer.rb', line 10

def initialize(spec, conn, warn: true)
  @spec = spec
  @conn = conn
  @warn = warn
  @uncovered_tables = Set.new
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



5
6
7
# File 'lib/mikras_utils/mkacl/analyzer.rb', line 5

def conn
  @conn
end

#specObject (readonly)

Returns the value of attribute spec.



4
5
6
# File 'lib/mikras_utils/mkacl/analyzer.rb', line 4

def spec
  @spec
end

Class Method Details

.analyze(spec, conn, **opts) ⇒ Object



105
# File 'lib/mikras_utils/mkacl/analyzer.rb', line 105

def self.analyze(spec, conn, **opts) self.new(spec, conn, **opts).analyze end

Instance Method Details

#analyzeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mikras_utils/mkacl/analyzer.rb', line 17

def analyze
  # Check select-mutations are nil
  spec.tables.each { |table|
    table.actions.each { |name, action|
      if name == "select"
        action.rules.each { |rule|
          rule.mutation.nil? or
              raise ArgumentError, "Can't use mutations in select actions in table #{table.name}"
        }
      end
    }
  }

  # Assign default detach
  spec.tables.each { |table|
    table.attach&.copy(:detach) if table.detach.nil?
  }

  # Find child-parent relations between linked tables
  links = conn.tuples %(
    select
      table_name as "child_table_name",
      schema_name || '.' || table_name as "child_table_uid",
      ref_table_name as "parent_table_name",
      ref_schema_name || '.' || ref_table_name as "parent_table_uid",
      column_name as "parent_link_field"
    from meta.links
    where schema_name = '#{spec.app_schema}'
      and ref_schema_name = '#{spec.app_schema}'
  )

  for child_table_name, child_table_uid, parent_table_name, parent_table_uid, parent_link_field in links
    # Detect uncovered tables
    if !spec.key?(child_table_name)
      @uncovered_tables << child_table_name
      next
    elsif !spec.key?(parent_table_name)
      @uncovered_tables << parent_table_name
      next
    end

    # Find referenced table objects
    child_table = spec[child_table_name] or raise "Can't find table #{parent_table_name.inspect}"
    parent_table = spec[parent_table_name] or raise "Can't find referenced table #{parent_table_name.inspect}"

    # Assign table references
    child_table.references[parent_table.name] = [parent_table, parent_link_field]
  end

#     if warn && !@uncovered_tables.empty?
#       puts "Warning: Uncovered tables"
#       indent { puts uncovered_tables }
#     end

  # Assign parents
  spec.tables.each { |table|
    if table.parent_name
      table.parent = spec[table.parent_name] or raise ArgumentError, "Can't find '#{table.parent_name}'"
      table.parent_link_field = table.references[table.parent.name].last
    else
      table.references.size <= 1 or raise ArgumentError, "Table '#{table.name}' has multiple references"
      parent, link_field = table.references.values.first
      if parent&.acl
        table.parent = parent
        table.parent_link_field = link_field
      end
    end
  }

  # Resolve domains
  spec.tables.select(&:acl).each { |t| resolve_domain(t) }

  # Function defaults
  rules = spec.tables.map { |table| table.actions.values.map { |action| action.rules } }.flatten
  rules.each { |rule|
    if rule.mutation && !%w(attach detach).include?(rule.action)
      if !rule.function
        rule.function = DEFAULT_FUNCTION
      end
    else # rule.mutation.nil?
      # do nothing
    end
  }


  spec
end

#uncovered_tablesObject

Tables in the application schema that are not defined in the SPEC file



8
# File 'lib/mikras_utils/mkacl/analyzer.rb', line 8

def uncovered_tables = @uncovered_tables.to_a