Module: ActiveRecord::Materialized::CacheTableSchema Private

Defined in:
lib/activerecord/materialized/cache_table_schema.rb

Overview

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

Provisions a view's cache table from its source relation. The per-column type inference lives in ColumnTypeInference; this module maps the inferred definitions onto create_table (and, via MigrationBuilder, a generated migration).

Defined Under Namespace

Classes: ColumnDefinition, IndexDefinition

Class Method Summary collapse

Class Method Details

.column_definitions(connection, relation) ⇒ 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.

The inferred MV columns. Names come from the query's projection (authoritative, via a zero-row probe); each column's type is inferred structurally from its projected node by ColumnTypeInference. Shared by table creation, migration generation, and drift checks.



67
68
69
70
71
72
# File 'lib/activerecord/materialized/cache_table_schema.rb', line 67

def column_definitions(connection, relation)
  nodes = relation.select_values
  connection.exec_query(relation.limit(0).to_sql).columns.each_with_index.map do |name, index|
    ColumnTypeInference.definition_for(connection, relation, nodes[index], name)
  end
end

.create_table!(view_class, table_name, relation) ⇒ 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
# File 'lib/activerecord/materialized/cache_table_schema.rb', line 45

def create_table!(view_class, table_name, relation)
  build_table!(view_class.connection, table_name, relation, index: index_definition(view_class))
end

.ensure_table!(view_class, relation) ⇒ 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.



38
39
40
41
42
43
# File 'lib/activerecord/materialized/cache_table_schema.rb', line 38

def ensure_table!(view_class, relation)
  return if view_class.table_exists?

  build_table!(view_class.connection, view_class.table_name, relation, index: index_definition(view_class))
  view_class.reset_column_information
end

.index_definition(view_class) ⇒ IndexDefinition?

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.

The partition-key index for a view, or nil for a non-grouped view (no maintenance key to index). A key derived purely from the GROUP BY is the partition identity, so the index is UNIQUE — which also structurally prevents duplicate partition rows under concurrent maintenance (see #95). An explicit incremental_keys override may be coarser than the grouping, so that index is non-unique.

Returns:



55
56
57
58
59
60
61
# File 'lib/activerecord/materialized/cache_table_schema.rb', line 55

def index_definition(view_class)
  keys = view_class.maintenance_key_columns
  return nil if keys.empty?

  columns = keys.map { |key| key.rpartition(".").last }
  IndexDefinition.new(columns: columns, unique: view_class.incremental_key_columns.empty?)
end