Sha256: 7b25e6ca3594c40ed9cd02e3359f2ce176fd19fd51b61b1327e0180f612f3342

Contents?: true

Size: 1.31 KB

Versions: 58

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Sequel
  # Extension with some tools that use pg internal tables and views
  module PGTools
    # List inherited tables for specific parent table
    #
    # @param table_name [String, Symbol] name of the parent table
    # @param schema [String, Symbol] schema of the parent table, defaults to +:public+
    #
    # @example
    #   DB.inherited_tables_for(:event_log)
    #   # => [:event_log_2019_01, :event_log_2019_02]
    #
    #   DB.inherited_tables_for(:event_log, schema: :foo)
    #   # => []
    # @return [Array<Symbol>] list of inhertied tables
    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
  end

  Database.register_extension(:pg_tools, PGTools)
end

Version data entries

58 entries across 58 versions & 1 rubygems

Version Path
umbrellio-sequel-plugins-0.17.0 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.16.1 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.16.0.239 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.16.0.238 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.16.0.235 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.16.0.234 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.16.0.233 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.16.0 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.16.0.211 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.15.0.198 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.14.0.192 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.14.0.189 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.14.0.188 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.14.0.187 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.14.0 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.13.0.185 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.13.0.172 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.13.0 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.12.0.169 lib/sequel/extensions/pg_tools.rb
umbrellio-sequel-plugins-0.12.0 lib/sequel/extensions/pg_tools.rb