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 => error
  on_error(schema.name, error)
  { **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.



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

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.



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

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.



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

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.



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

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.



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

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
# 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.



114
115
116
# File 'lib/rom/sql/schema/inferrer.rb', line 114

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