Skip to content

Commit

Permalink
Merge pull request #22623 from greysteil/support-passing-schema-name-…
Browse files Browse the repository at this point in the history
…to-indexes

Support passing the schema name prefix to `conenction.indexes`
  • Loading branch information
matthewd committed Dec 18, 2015
2 parents c316ce9 + d927f35 commit da6713f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
* Support passing the schema name as a prefix to table name in
`ConnectionAdapters::SchemaStatements#indexes`. Previously the prefix would
be considered a full part of the index name, and only the schema in the
current search path would be considered.

*Grey Baker*

* Ignore index name in `index_exists?` and `remove_index` when not passed a
name to check for.

Expand Down
Expand Up @@ -169,15 +169,18 @@ def index_name_exists?(table_name, index_name, default)

# Returns an array of indexes for the given table.
def indexes(table_name, name = nil)
result = query(<<-SQL, 'SCHEMA')
SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
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 d.indisprimary = 'f'
AND t.relname = '#{table_name}'
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
table = Utils.extract_schema_qualified_name(table_name.to_s)

result = query(<<-SQL, 'SCHEMA')
SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
FROM pg_class t
INNER JOIN pg_index d ON t.oid = d.indrelid
INNER JOIN pg_class i ON d.indexrelid = i.oid
LEFT JOIN pg_namespace n ON n.oid = i.relnamespace
WHERE i.relkind = 'i'
AND d.indisprimary = 'f'
AND t.relname = '#{table.identifier}'
AND n.nspname = #{table.schema ? "'#{table.schema}'" : 'ANY (current_schemas(false))'}
ORDER BY i.relname
SQL

Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/adapters/postgresql/schema_test.rb
Expand Up @@ -321,6 +321,11 @@ def test_dump_indexes_for_schema_multiple_schemas_in_search_path
do_dump_index_tests_for_schema("public, #{SCHEMA_NAME}", INDEX_A_COLUMN, INDEX_B_COLUMN_S1, INDEX_D_COLUMN, INDEX_E_COLUMN)
end

def test_dump_indexes_for_table_with_scheme_specified_in_name
indexes = @connection.indexes("#{SCHEMA_NAME}.#{TABLE_NAME}")
assert_equal 4, indexes.size
end

def test_with_uppercase_index_name
@connection.execute "CREATE INDEX \"things_Index\" ON #{SCHEMA_NAME}.things (name)"

Expand Down

0 comments on commit da6713f

Please sign in to comment.