Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added support to export and import extensions (think hstore, postgis)

  • Loading branch information...
commit 47d5c1fc8e3e981acbbad0f4b6dd2609d67d3b54 1 parent 2d42ce4
Keenan Brock authored
8 lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb
View
@@ -209,6 +209,10 @@ def add_index(table_name, column_name, options = {})
execute "CREATE #{unique} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)}#{index_type}(#{index_columns})#{index_options}"
end
+ def add_extension(extension_name, options={})
+ execute "CREATE extension if not exists \"#{extension_name}\""
+ end
+
def change_table(table_name, options = {})
if supports_bulk_alter? && options[:bulk]
recorder = ActiveRecord::Migration::CommandRecorder.new(self)
@@ -320,6 +324,10 @@ def indexes(table_name, name = nil)
end.compact
end
+ def extensions
+ select_rows('select extname from pg_extension', 'extensions').map { |row| row[0] }.delete_if {|name| name == 'plpgsql'}
+ end
+
private
def ipaddr_to_string(value)
19 lib/postgres_ext/active_record/schema_dumper.rb
View
@@ -7,7 +7,26 @@ def self.valid_column_spec_keys
VALID_COLUMN_SPEC_KEYS
end
+ def dump(stream)
+ header(stream)
+ # added
+ extensions(stream)
+ # /added
+ tables(stream)
+ trailer(stream)
+ stream
+ end
+
private
+
+ def extensions(stream)
+ exts=@connection.extensions
+
+ unless exts.empty?
+ stream.puts exts.map { |name| " add_extension \"#{name}\""}.join("\n") + "\n\n"
+ end
+ end
+
def table(table, stream)
columns = @connection.columns(table)
begin
14 spec/schema_dumper/extension_spec.rb
View
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe 'extension schema dump' do
+ let!(:connection) { ActiveRecord::Base.connection }
+ it 'correctly creates and exports database extensions' do
+ stream = StringIO.new
+ connection.add_extension 'hstore'
+
+ ActiveRecord::SchemaDumper.dump(connection, stream)
+ output = stream.string
+
+ output.should match /add_extension "hstore"/
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.