Module: Sequel::PGTools

Defined in:
lib/sequel/extensions/pg_tools.rb

Overview

Extension with some tools that use pg internal tables and views

Instance Method Summary collapse

Instance Method Details

#inherited_tables_for(table_name, schema: :public) ⇒ Array<Symbol>

List inherited tables for specific parent table

Examples:

DB.inherited_tables_for(:event_log)
# => [:event_log_2019_01, :event_log_2019_02]

DB.inherited_tables_for(:event_log, schema: :foo)
# => []

Parameters:

  • table_name (String, Symbol)

    name of the parent table

  • schema (String, Symbol) (defaults to: :public)

    schema of the parent table, defaults to :public

Returns:

  • (Array<Symbol>)

    list of inhertied tables



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sequel/extensions/pg_tools.rb', line 18

def inherited_tables_for(table_name, schema: :public)
  self[:pg_inherits]
    .select(Sequel[:cn][:nspname].as(:schema), Sequel[:c][:relname].as(:child))
    .left_join(Sequel[:pg_class].as(:c), Sequel[:inhrelid] => Sequel[:c][:oid])
    .left_join(Sequel[:pg_class].as(:p), Sequel[:inhparent] => Sequel[:p][:oid])
    .left_join(Sequel[:pg_namespace].as(:pn), Sequel[:pn][:oid] => Sequel[:p][:relnamespace])
    .left_join(Sequel[:pg_namespace].as(:cn), Sequel[:cn][:oid] => Sequel[:c][:relnamespace])
    .where(Sequel[:p][:relname] => table_name.to_s, Sequel[:pn][:nspname] => schema.to_s)
    .to_a
    .map { |x| x[:child].to_sym }
end