Class: ROM::SQL::Schema::Inferrer Private

Inherits:
ROM::Schema::Inferrer
  • Object
show all
Defined in:
lib/rom/sql/schema/inferrer.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

FALLBACK_SCHEMA =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  attributes: EMPTY_ARRAY,
  indexes: EMPTY_SET,
  foreign_keys: EMPTY_SET
}.freeze

Instance Method Summary collapse

Instance Method Details

#call(schema, gateway) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
38
39
40
41
42
43
44
# File 'lib/rom/sql/schema/inferrer.rb', line 35

def call(schema, gateway)
  if enabled?
    infer_from_database(gateway, schema, **super)
  else
    infer_from_attributes(gateway, schema, **super)
  end
rescue Sequel::Error => e
  on_error(schema.name, e)
  {**FALLBACK_SCHEMA, indexes: schema.indexes}
end

#foreign_keys_from_attributes(attributes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



108
109
110
111
112
113
# File 'lib/rom/sql/schema/inferrer.rb', line 108

def foreign_keys_from_attributes(attributes)
  attributes
    .select(&:foreign_key?)
    .map { |attr| SQL::ForeignKey.new([attr.unwrap], attr.target) }
    .to_set
end

#foreign_keys_from_database(gateway, schema, attributes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



88
89
90
91
92
93
94
95
96
97
# File 'lib/rom/sql/schema/inferrer.rb', line 88

def foreign_keys_from_database(gateway, schema, attributes)
  dataset = schema.name.dataset

  gateway.connection.foreign_key_list(dataset).map { |definition|
    columns, table, key = definition.values_at(:columns, :table, :key)
    attrs = columns.map { |name| attributes[name] }

    SQL::ForeignKey.new(attrs, table, parent_keys: key)
  }.to_set
end

#indexes_from_attributes(attributes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



100
101
102
103
104
105
# File 'lib/rom/sql/schema/inferrer.rb', line 100

def indexes_from_attributes(attributes)
  attributes
    .select(&:indexed?)
    .map { |attr| SQL::Index.new([attr.unwrap]) }
    .to_set
end

#indexes_from_database(gateway, schema, attributes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rom/sql/schema/inferrer.rb', line 72

def indexes_from_database(gateway, schema, attributes)
  if gateway.connection.respond_to?(:indexes)
    dataset = schema.name.dataset

    gateway.connection.indexes(dataset).map { |index_name, definition|
      columns, unique = definition.values_at(:columns, :unique)
      attrs = columns.map { |name| attributes[name] }

      SQL::Index.new(attrs, name: index_name, unique: unique)
    }.to_set
  else
    EMPTY_SET
  end
end

#infer_from_attributes(_gateway, schema, attributes:, **rest) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
64
65
66
67
68
69
# File 'lib/rom/sql/schema/inferrer.rb', line 61

def infer_from_attributes(_gateway, schema, attributes:, **rest)
  indexes = schema.indexes | indexes_from_attributes(attributes)
  foreign_keys = foreign_keys_from_attributes(attributes)

  {**rest,
    attributes: attributes.map { |attr| mark_indexed(attr, indexes) },
    foreign_keys: foreign_keys,
    indexes: indexes}
end

#infer_from_database(gateway, schema, attributes:, **rest) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rom/sql/schema/inferrer.rb', line 47

def infer_from_database(gateway, schema, attributes:, **rest)
  idx = attributes_index(attributes)
  indexes = indexes_from_database(gateway, schema, idx)
  foreign_keys = foreign_keys_from_database(gateway, schema, idx)

  {**rest,
    attributes: attributes.map { |attr|
                  mark_fk(mark_indexed(attr, indexes), foreign_keys)
                },
    foreign_keys: foreign_keys,
    indexes: indexes}
end

#suppress_errorsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



116
117
118
# File 'lib/rom/sql/schema/inferrer.rb', line 116

def suppress_errors
  with(raise_on_error: false, silent: true)
end