提交 34ac46ce 编写于 作者: A Aaron Patterson

Merge pull request #9203 from jaggederest/dumping_pgsql_extensions

Adding database extension support to schema.rb
...@@ -177,6 +177,12 @@ def supports_extensions? ...@@ -177,6 +177,12 @@ def supports_extensions?
false false
end end
# A list of extensions, to be filled in by databases that
# support them (at the moment, postgresql)
def extensions
[]
end
# QUOTING ================================================== # QUOTING ==================================================
# Returns a bind substitution value given a +column+ and list of current # Returns a bind substitution value given a +column+ and list of current
......
...@@ -605,6 +605,15 @@ def extension_enabled?(name) ...@@ -605,6 +605,15 @@ def extension_enabled?(name)
end end
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 # Returns the configured supported identifier length supported by PostgreSQL
def table_alias_length def table_alias_length
@table_alias_length ||= query('SHOW max_identifier_length', 'SCHEMA')[0][0].to_i @table_alias_length ||= query('SHOW max_identifier_length', 'SCHEMA')[0][0].to_i
......
...@@ -24,6 +24,7 @@ def self.dump(connection=ActiveRecord::Base.connection, stream=STDOUT) ...@@ -24,6 +24,7 @@ def self.dump(connection=ActiveRecord::Base.connection, stream=STDOUT)
def dump(stream) def dump(stream)
header(stream) header(stream)
extensions(stream)
tables(stream) tables(stream)
trailer(stream) trailer(stream)
stream stream
...@@ -66,6 +67,17 @@ def trailer(stream) ...@@ -66,6 +67,17 @@ def trailer(stream)
stream.puts "end" stream.puts "end"
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) def tables(stream)
@connection.tables.sort.each do |tbl| @connection.tables.sort.each do |tbl|
next if ['schema_migrations', ignore_tables].flatten.any? do |ignored| next if ['schema_migrations', ignore_tables].flatten.any? do |ignored|
......
...@@ -35,6 +35,11 @@ def teardown ...@@ -35,6 +35,11 @@ def teardown
@connection.execute 'drop table if exists hstores' @connection.execute 'drop table if exists hstores'
end 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 def test_hstore_enabled
assert @connection.extension_enabled?('hstore') assert @connection.extension_enabled?('hstore')
end end
......
...@@ -231,6 +231,16 @@ def test_schema_dump_includes_decimal_options ...@@ -231,6 +231,16 @@ def test_schema_dump_includes_decimal_options
end end
if current_adapter?(:PostgreSQLAdapter) 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 def test_schema_dump_includes_xml_shorthand_definition
output = standard_dump output = standard_dump
if %r{create_table "postgresql_xml_data_type"} =~ output if %r{create_table "postgresql_xml_data_type"} =~ output
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册