class DBCode::Schema

Attributes

connection[R]
name[R]

Public Class Methods

new(name:, connection:) click to toggle source
# File lib/dbcode/schema.rb, line 29
def initialize(name:, connection:)
  @name, @connection = name, connection
end

Public Instance Methods

append_path!(config) click to toggle source
# File lib/dbcode/schema.rb, line 63
def append_path!(config)
  #update all future connections
  config.merge! schema_search_path: search_path.append(name)
  #update all active connections
  connection.pool.connections.each do |connection|
    connection.schema_search_path = config[:schema_search_path]
  end
end
digest() click to toggle source
# File lib/dbcode/schema.rb, line 48
    def digest
      comment = connection.select_one <<-SQL
        select pg_catalog.obj_description(n.oid, 'pg_namespace') as md5
        from pg_catalog.pg_namespace n where n.nspname = '#@name'
      SQL
      comment && comment['md5'] =~ /^dbcode_md5:(.+)$/ && $1
    end
digest=(digest) click to toggle source
# File lib/dbcode/schema.rb, line 42
    def digest=(digest)
      execute <<-SQL
        comment on schema #@name is 'dbcode_md5:#{digest}'
      SQL
    end
reset!() click to toggle source
# File lib/dbcode/schema.rb, line 35
    def reset!
      execute <<-SQL
        drop schema if exists #@name cascade;
        create schema #@name;
      SQL
    end
within_schema(&block) click to toggle source
# File lib/dbcode/schema.rb, line 56
def within_schema(&block)
  old_path = search_path
  connection.schema_search_path = old_path.prepend(name).to_s
  connection.transaction(&block)
  connection.schema_search_path = old_path.to_s
end

Private Instance Methods

search_path() click to toggle source
# File lib/dbcode/schema.rb, line 73
def search_path
  SearchPath.new(connection.schema_search_path)
end