diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-02-01 16:01:38 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-02-01 16:01:38 -0200 |
commit | 83ea905fd106872841bf7219f108ef9d2299d86a (patch) | |
tree | 3fad27489fa98dfecb1c1b87926fc7a04ab99196 /activerecord/lib | |
parent | e9be1c1e80f4e7efeb1c231505c28b95f901ff8b (diff) | |
parent | a099d7d97f80236185f1994a76a4366b2a5e21ab (diff) | |
download | rails-83ea905fd106872841bf7219f108ef9d2299d86a.tar.gz rails-83ea905fd106872841bf7219f108ef9d2299d86a.tar.bz2 rails-83ea905fd106872841bf7219f108ef9d2299d86a.zip |
Merge pull request #13688 from jbaudanza/psql-index-exists
PostgreSQL implementation of SchemaStatements#index_name_exists?
Conflicts:
activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 571257f6dd..ae8ede4b42 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -126,6 +126,19 @@ module ActiveRecord 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') |