Module: ActiveRecord::Materialized::Metadata::Schema Private

Defined in:
lib/activerecord/materialized/metadata/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.

Lazily provisions and migrates the materialized-view metadata table.

API:

  • private

Class Method Summary collapse

Class Method Details

.create_metadata_table!(connection) ⇒ 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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/activerecord/materialized/metadata/schema.rb', line 19

def (connection)
  table = ::ActiveRecord::Materialized.
  connection.create_table(table, force: :cascade) do |t|
    t.string :view_name, null: false
    t.datetime :last_refreshed_at
    t.boolean :refreshing, null: false, default: false
    t.boolean :dirty, null: false, default: true
    t.boolean :warm, null: false, default: false
    t.integer :row_count
    t.integer :refresh_duration_ms
    t.text :last_error
    t.text :maintenance_payload
    t.datetime :last_reconciled_at
    t.integer :reconciled_partition_count
    t.integer :fresh_set_generation, null: false, default: 0
    t.timestamps
  end
  connection.add_index(table, :view_name, unique: true)
end

.ensure_column!(connection, name, type, default: nil, null: true) ⇒ 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



52
53
54
55
56
57
58
# File 'lib/activerecord/materialized/metadata/schema.rb', line 52

def ensure_column!(connection, name, type, default: nil, null: true)
  return if MetadataRecord.column_names.include?(name.to_s)

  connection.add_column(
    ::ActiveRecord::Materialized., name, type, default: default, null: null
  )
end

.ensure_columns!(connection) ⇒ 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.

Lazily add columns introduced after a table was first provisioned, so an app migrated from an earlier version picks them up without a new migration.

API:

  • private



41
42
43
44
45
46
47
48
49
50
# File 'lib/activerecord/materialized/metadata/schema.rb', line 41

def ensure_columns!(connection)
  return unless MetadataRecord.table_exists?

  ensure_column!(connection, :dirty, :boolean, default: true, null: false)
  ensure_column!(connection, :warm, :boolean, default: false, null: false)
  ensure_column!(connection, :maintenance_payload, :text)
  ensure_column!(connection, :last_reconciled_at, :datetime)
  ensure_column!(connection, :reconciled_partition_count, :integer)
  ensure_column!(connection, :fresh_set_generation, :integer, default: 0, null: false)
end

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



12
13
14
15
16
17
# File 'lib/activerecord/materialized/metadata/schema.rb', line 12

def ensure_table!(view_class)
  connection = view_class.connection
  (connection) unless MetadataRecord.table_exists?
  ensure_columns!(connection)
  MetadataRecord.reset_column_information
end