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.



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

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.



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

def foreign_keys_from_attributes(attributes)
  attributes
    .select(&:foreign_key?)
    .to_set { |attr| SQL::ForeignKey.new([attr.unwrap], attr.target) }
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.



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

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

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

    SQL::ForeignKey.new(attrs, table, parent_keys: key)
  end
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.



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

def indexes_from_attributes(attributes)
  attributes
    .select(&:indexed?)
    .to_set { |attr| SQL::Index.new([attr.unwrap]) }
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.



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

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

    gateway.connection.indexes(dataset).to_set do |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)
    end
  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
70
71
# 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.



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

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_errors ⇒ 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.



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

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