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



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

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.



83
84
85
86
87
88
89
90
91
# File 'lib/rom/sql/schema/inferrer.rb', line 83

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

  gateway.connection.foreign_key_list(dataset).map { |columns:, table:, key:, **rest|
    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.



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

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.



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

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

    gateway.connection.indexes(dataset).map { |index_name, columns:, unique:, **rest|
      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.



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

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



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

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