Sha256: 432395fc281a83b0747adb3239d4643afe5033333cd3f59e8f72be5a958f9b59

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module ActiveTenant
  class PostgresAdapter
    delegate :connection_config, :establish_connection, :connection, to: ::ActiveRecord::Base

    def initialize
      self.global = ActiveTenant.configuration.global if ActiveTenant.configuration.global
    end

    def all
      connection.select_values("SELECT nspname FROM pg_namespace WHERE nspname <> 'information_schema' AND nspname NOT LIKE 'pg%'") - [global]
    end

    def create(name)
      unless all.include? name
        connection.execute "CREATE SCHEMA \"#{name}\""
      end
    end

    def remove(name)
      if all.include? name
        connection.execute "DROP SCHEMA \"#{name}\" CASCADE"
      end
    end

    def with(name)
      return yield if name == search_path

      ex = nil
      current_schema = search_path
      search_path name
      begin
        result = yield
      rescue => e
        ex = e
      ensure
        search_path current_schema
        raise ex unless ex.nil?
        result
      end
    end

    def name
      search_path
    end

    def global
      @global || 'public'
    end

    private

    def global=(name)
      @global = name
    end

    def search_path(name=nil)
      if name
        connection.execute("SET SEARCH_PATH TO \"#{name}\"")
        establish_connection connection_config.merge(schema_search_path: name)
      else
        connection_config[:schema_search_path]
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_tenant-0.0.1 lib/active_tenant/adapters/postgres_adapter.rb