Sha256: 68a2764867b7f690d6352ccd80e9b95b60db66ab14449a5cc6582f1aa7dc79da

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require 'apartment/adapters/postgresql_adapter'

module Apartment
  # JDBC helper to decide wether to use JDBC Postgresql Adapter or JDBC Postgresql Adapter with Schemas
  module Tenant
    def self.jdbc_postgresql_adapter(config)
      if Apartment.use_schemas
        Adapters::JDBCPostgresqlSchemaAdapter.new(config)
      else
        Adapters::JDBCPostgresqlAdapter.new(config)
      end
    end
  end

  module Adapters
    # Default adapter when not using Postgresql Schemas
    class JDBCPostgresqlAdapter < PostgresqlAdapter
      private

      def multi_tenantify_with_tenant_db_name(config, tenant)
        config[:url] = "#{config[:url].gsub(%r{(\S+)/.+$}, '\1')}/#{environmentify(tenant)}"
      end

      def create_tenant_command(conn, tenant)
        conn.create_database(environmentify(tenant), thisisahack: '')
      end

      def rescue_from
        ActiveRecord::JDBCError
      end
    end

    # Separate Adapter for Postgresql when using schemas
    class JDBCPostgresqlSchemaAdapter < PostgresqlSchemaAdapter
      #   Set schema search path to new schema
      #
      def connect_to_new(tenant = nil)
        return reset if tenant.nil?

        tenant = tenant.to_s
        raise ActiveRecord::StatementInvalid, "Could not find schema #{tenant}" unless tenant_exists?(tenant)

        @current = tenant
        Apartment.connection.schema_search_path = full_search_path
      rescue ActiveRecord::StatementInvalid, ActiveRecord::JDBCError
        raise TenantNotFound, "One of the following schema(s) is invalid: #{full_search_path}"
      end

      private

      def tenant_exists?(tenant)
        return true unless Apartment.tenant_presence_check

        Apartment.connection.all_schemas.include? tenant
      end

      def rescue_from
        ActiveRecord::JDBCError
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ros-apartment-2.9.0 lib/apartment/adapters/jdbc_postgresql_adapter.rb
ros-apartment-2.8.1 lib/apartment/adapters/jdbc_postgresql_adapter.rb
ros-apartment-2.8.1.rc2 lib/apartment/adapters/jdbc_postgresql_adapter.rb
ros-apartment-2.8.1.rc1 lib/apartment/adapters/jdbc_postgresql_adapter.rb
ros-apartment-2.8.0 lib/apartment/adapters/jdbc_postgresql_adapter.rb
ros-apartment-2.7.2 lib/apartment/adapters/jdbc_postgresql_adapter.rb