diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-02-06 16:18:06 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-02-06 16:18:06 -0800 |
commit | 34ac46ceebdf95adc8d5ccdcde15cd615f180c89 (patch) | |
tree | 64232662bde99900457fd59abcf2a83cac38cdad | |
parent | 2f0c26bf5913346311b75a3b100d2f79cfe8df3c (diff) | |
parent | fba496f2c0ae583075795ff0200eeaec182e02de (diff) | |
download | rails-34ac46ceebdf95adc8d5ccdcde15cd615f180c89.tar.gz rails-34ac46ceebdf95adc8d5ccdcde15cd615f180c89.tar.bz2 rails-34ac46ceebdf95adc8d5ccdcde15cd615f180c89.zip |
Merge pull request #9203 from jaggederest/dumping_pgsql_extensions
Adding database extension support to schema.rb
5 files changed, 42 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index b2ad4e600d..310d25767b 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -177,6 +177,12 @@ module ActiveRecord false end + # A list of extensions, to be filled in by databases that + # support them (at the moment, postgresql) + def extensions + [] + end + # QUOTING ================================================== # Returns a bind substitution value given a +column+ and list of current diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 5ce2f1b04c..84e48970ca 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -605,6 +605,15 @@ module ActiveRecord end end + def extensions + if supports_extensions? + res = exec_query "SELECT extname from pg_extension", "SCHEMA" + res.rows.map { |r| res.column_types['extname'].type_cast r.first } + else + [] + end + end + # Returns the configured supported identifier length supported by PostgreSQL def table_alias_length @table_alias_length ||= query('SHOW max_identifier_length', 'SCHEMA')[0][0].to_i diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 36bde44e7c..f90aaa916d 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -24,6 +24,7 @@ module ActiveRecord def dump(stream) header(stream) + extensions(stream) tables(stream) trailer(stream) stream @@ -66,6 +67,17 @@ HEADER stream.puts "end" end + def extensions(stream) + return unless @connection.supports_extensions? + extensions = @connection.extensions + stream.puts <<EXTENSIONS +# These are extensions that must be enabled in order to support this database +EXTENSIONS + extensions.each do |extension| + stream.puts " enable_extension #{extension.inspect}" + end + end + def tables(stream) @connection.tables.sort.each do |tbl| next if ['schema_migrations', ignore_tables].flatten.any? do |ignored| diff --git a/activerecord/test/cases/adapters/postgresql/hstore_test.rb b/activerecord/test/cases/adapters/postgresql/hstore_test.rb index 331e3f7e4d..6640f9b497 100644 --- a/activerecord/test/cases/adapters/postgresql/hstore_test.rb +++ b/activerecord/test/cases/adapters/postgresql/hstore_test.rb @@ -35,6 +35,11 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase @connection.execute 'drop table if exists hstores' end + def test_hstore_included_in_extensions + assert @connection.respond_to?(:extensions), "connection should have a list of extensions" + assert @connection.extensions.include?('hstore'), "extension list should include hstore" + end + def test_hstore_enabled assert @connection.extension_enabled?('hstore') end diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index cae12e0e3a..c5092e15ea 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -231,6 +231,16 @@ class SchemaDumperTest < ActiveRecord::TestCase end if current_adapter?(:PostgreSQLAdapter) + def test_schema_dump_includes_extensions + connection = ActiveRecord::Base.connection + return skip unless connection.supports_extensions? + unless connection.extension_enabled?('hstore') + connection.enable_extension 'hstore' + end + output = standard_dump + assert_match %r{enable_extension "hstore"}, output + end + def test_schema_dump_includes_xml_shorthand_definition output = standard_dump if %r{create_table "postgresql_xml_data_type"} =~ output |