Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adding database extension support to schema.rb #9203

Merged
merged 3 commits into from

2 participants

@jaggederest

Fixes issue #9193

I could use some extra eyeballs and feedback on this, I'm not entirely familiar with the schema dumper.

Thanks for your time

jaggederest added some commits
@jaggederest jaggederest Add some tests to enumerate how extensions should be stored in the sc…
…hema output
c0990f0
@jaggederest jaggederest improve tests to check for existence of extensions method, and skip t…
…esting dumped extensions if they are unsupported by the database
e2fdfa9
@jaggederest jaggederest add ActiveRecord::AbstractAdapter#extensions and ActiveRecord::Connec…
…tionAdapters::PostgreSQLAdapter#extensions to allow dumping of enabled extensions to schema.rb, add ActiveRecord::SchemaDumper#extensions to dump extensions to schema.rb
fba496f
@tenderlove tenderlove merged commit 34ac46c into rails:master
@jaggederest jaggederest deleted the jaggederest:dumping_pgsql_extensions branch
@andreychernih andreychernih referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@tenderlove tenderlove referenced this pull request from a commit
@tenderlove tenderlove Merge branch 'master' into jobs
* master:
  Skip schema dumper extensions test if connection does not support it
  active_record: Quote numeric values compared to string columns.
  Run schema dumper extensions without creating real extensions
  Do not print anything related to extensions when they don't none exist
  Add blank line after extensions to separate from tables in schema
  Fix indentation of extensions in schema
  Call super to use the abstract adapter implementation instead
  Add changelog entry for #9203 about schema dumper with db extensions
  add ActiveRecord::AbstractAdapter#extensions and ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#extensions to allow dumping of enabled extensions to schema.rb, add ActiveRecord::SchemaDumper#extensions to dump extensions to schema.rb
  improve tests to check for existence of extensions method, and skip testing dumped extensions if they are unsupported by the database
  Add some tests to enumerate how extensions should be stored in the schema output
  Update changelog from #9128 with author name
  Update actionpack/CHANGELOG.md
  ruby constant syntax is not supported as routing `:controller` option.
  Fix article for generator name
  Update .gitignore
  Enable hstore extensions on tests if it is not enabled and database supports it
  Ignore .ruby-version
  Modify TimeWithZone#as_json to return 3DP of sub-second accuracy by default, since it's allowed by the spec and is very useful.
8609fd1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 7, 2013
  1. @jaggederest
  2. @jaggederest

    improve tests to check for existence of extensions method, and skip t…

    jaggederest authored
    …esting dumped extensions if they are unsupported by the database
  3. @jaggederest

    add ActiveRecord::AbstractAdapter#extensions and ActiveRecord::Connec…

    jaggederest authored
    …tionAdapters::PostgreSQLAdapter#extensions to allow dumping of enabled extensions to schema.rb, add ActiveRecord::SchemaDumper#extensions to dump extensions to schema.rb
This page is out of date. Refresh to see the latest.
View
6 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -177,6 +177,12 @@ def supports_extensions?
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
View
9 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -605,6 +605,15 @@ def extension_enabled?(name)
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
View
12 activerecord/lib/active_record/schema_dumper.rb
@@ -24,6 +24,7 @@ def self.dump(connection=ActiveRecord::Base.connection, stream=STDOUT)
def dump(stream)
header(stream)
+ extensions(stream)
tables(stream)
trailer(stream)
stream
@@ -66,6 +67,17 @@ def trailer(stream)
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|
View
5 activerecord/test/cases/adapters/postgresql/hstore_test.rb
@@ -35,6 +35,11 @@ def teardown
@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
View
10 activerecord/test/cases/schema_dumper_test.rb
@@ -231,6 +231,16 @@ def test_schema_dump_includes_decimal_options
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
Something went wrong with that request. Please try again.