Permalink
Browse files

Merge pull request #13688 from jbaudanza/psql-index-exists

PostgreSQL implementation of SchemaStatements#index_name_exists?

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
2 parents e9be1c1 + a099d7d commit 83ea905fd106872841bf7219f108ef9d2299d86a @rafaelfranca rafaelfranca committed Feb 1, 2014
View
@@ -1,3 +1,13 @@
+* PostgreSQL implementation of SchemaStatements#index_name_exists?
+
+ The database agnostic implementation does not detect with indexes that are
+ not supported by the ActiveRecord schema dumper. For example, expressions
+ indexes would not be detected.
+
+ This fixes #11018.
+
+ *Jonathan Baudanza*
+
* Parsing PostgreSQL arrays with empty strings now works correctly.
Previously, if you tried to parse `{"1","","2","","3"}` the result
@@ -126,6 +126,19 @@ def schema_exists?(name)
SQL
end
+ def index_name_exists?(table_name, index_name, default)
+ 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_name}'
+ AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
+ SQL
+ end
+
# Returns an array of indexes for the given table.
def indexes(table_name, name = nil)
result = query(<<-SQL, 'SCHEMA')
@@ -246,6 +246,18 @@ def test_ignore_nil_schema_search_path
assert_nothing_raised { with_schema_search_path nil }
end
+ def test_index_name_exists
+ with_schema_search_path(SCHEMA_NAME) do
+ assert @connection.index_name_exists?(TABLE_NAME, INDEX_A_NAME, true)
+ assert @connection.index_name_exists?(TABLE_NAME, INDEX_B_NAME, true)
+ assert @connection.index_name_exists?(TABLE_NAME, INDEX_C_NAME, true)
+ assert @connection.index_name_exists?(TABLE_NAME, INDEX_D_NAME, true)
+ assert @connection.index_name_exists?(TABLE_NAME, INDEX_E_NAME, true)
+ assert @connection.index_name_exists?(TABLE_NAME, INDEX_E_NAME, true)
+ assert_not @connection.index_name_exists?(TABLE_NAME, 'missing_index', true)
+ end
+ end
+
def test_dump_indexes_for_schema_one
do_dump_index_tests_for_schema(SCHEMA_NAME, INDEX_A_COLUMN, INDEX_B_COLUMN_S1, INDEX_D_COLUMN, INDEX_E_COLUMN)
end

0 comments on commit 83ea905

Please sign in to comment.