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

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

Overview

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.

API:

  • private

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.

API:

  • private

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

API:

  • private



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.

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rom/sql/schema/inferrer.rb', line 63

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.

API:

  • private



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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.

API:

  • private



118
119
120
# File 'lib/rom/sql/schema/inferrer.rb', line 118

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