Skip to content

Commit

Permalink
8706 remove schema specific PostgreSQLAdapter patches / remove access…
Browse files Browse the repository at this point in the history
…_method from IndexDefinition
  • Loading branch information
altivi committed Sep 6, 2019
1 parent 9fa77ad commit 58d1e71
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ActiveRecord # :nodoc:
module ConnectionAdapters # :nodoc:
# Patched version: 3.1.3
# Patched version: 5.2.3
# Patched methods::
# * indexes
class PostgreSQLAdapter
Expand All @@ -9,72 +9,23 @@ class PostgreSQLAdapter
# Regex to find where clause in index statements
INDEX_WHERE_EXPRESSION = /WHERE (.+)$/

# Returns the list of all tables in the schema search path or a specified schema.
#
# == Patch:
# If current user is not `postgres` original method return all tables from all schemas
# without schema prefix. This disables such behavior by querying only default schema.
# Tables with schemas will be queried later.
#
def tables(name = nil)
query(<<-SQL, 'SCHEMA').map { |row| row[0] }
SELECT tablename
FROM pg_tables
WHERE schemaname = ANY (ARRAY['public'])
SQL
end

# Checks if index exists for given table.
#
# == Patch:
# Search using provided schema if table_name includes schema name.
#
def index_name_exists?(table_name, index_name, default = nil)
postgre_sql_name = PostgreSQL::Utils.extract_schema_qualified_name(table_name)
schema, table = postgre_sql_name.schema, postgre_sql_name.identifier
schemas = schema ? "ARRAY['#{schema}']" : 'current_schemas(false)'

exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
SELECT COUNT(*)
FROM pg_class t
INNER JOIN pg_index d ON t.oid = d.indrelid
INNER JOIN pg_class i ON d.indexrelid = i.oid
WHERE i.relkind = 'i'
AND i.relname = '#{index_name}'
AND t.relname = '#{table}'
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (#{schemas}) )
SQL
end

# Returns an array of indexes for the given table.
#
# == Patch 1 reason:
# Since {ActiveRecord::SchemaDumper#tables} is patched to process tables
# with a schema prefix, the {#indexes} method receives table_name as
# "<schema>.<table>". This patch allows it to handle table names with
# a schema prefix.
#
# == Patch 1:
# Search using provided schema if table_name includes schema name.
#
# == Patch 2 reason:
# {ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#indexes} is patched
# to support partial indexes using :where clause.
# Remove type specification from stored Postgres index definitions.
#
# == Patch 2:
# Search the postgres indexdef for the where clause and pass the output to
# the custom {PgSaurus::ConnectionAdapters::IndexDefinition}
# Split compound functional indexes to array.
#
def indexes(table_name)
scope = quoted_scope(table_name)

result = query(<<-SQL, "SCHEMA")
SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid,
am.amname, pg_catalog.obj_description(i.oid, 'pg_class') AS comment
pg_catalog.obj_description(i.oid, 'pg_class') AS comment
FROM pg_class t
INNER JOIN pg_index d ON t.oid = d.indrelid
INNER JOIN pg_class i ON d.indexrelid = i.oid
INNER JOIN pg_am am ON i.relam = am.oid
LEFT JOIN pg_namespace n ON n.oid = i.relnamespace
WHERE i.relkind = 'i'
AND d.indisprimary = 'f'
Expand All @@ -89,8 +40,7 @@ def indexes(table_name)
indkey = row[2].split(" ").map(&:to_i)
inddef = row[3]
oid = row[4]
access_method = row[5]
comment = row[6]
comment = row[5]

using, expressions, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: WHERE (.+))?\z/m).flatten

Expand Down Expand Up @@ -137,8 +87,7 @@ def indexes(table_name)
opclasses: opclasses,
where: where,
using: using.to_sym,
comment: comment.presence,
access_method: access_method
comment: comment.presence
)
end
end
Expand Down Expand Up @@ -170,23 +119,6 @@ def split_expression(expression)
result
end

# Find where statement from index definition
#
# @param [Hash] index index attributes
# @return [String] where statement
def find_where_statement(index)
index[:definition].scan(INDEX_WHERE_EXPRESSION).flatten[0]
end

# Find length of index
# TODO Update lengths once we merge in ActiveRecord code that supports it. -dresselm 20120305
#
# @param [Hash] index index attributes
# @return [Array]
def find_lengths(index)
[]
end

# Remove type specification from stored Postgres index definitions
#
# @param [String] column_with_type the name of the column with type
Expand Down
6 changes: 2 additions & 4 deletions lib/pg_saurus/connection_adapters/index_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module PgSaurus::ConnectionAdapters
# with the additional parameters.
class IndexDefinition # :nodoc:
attr_reader :table, :name, :unique, :columns, :lengths, :orders, :opclasses,
:where, :type, :using, :comment, :access_method
:where, :type, :using, :comment

def initialize(
table,
Expand All @@ -17,8 +17,7 @@ def initialize(
where: nil,
type: nil,
using: nil,
comment: nil,
access_method: nil
comment: nil
)
@table = table
@name = name
Expand All @@ -31,7 +30,6 @@ def initialize(
@type = type
@using = using
@comment = comment
@access_method = access_method
end

private
Expand Down

0 comments on commit 58d1e71

Please sign in to comment.