Skip to content

Commit

Permalink
added support to export and import extensions (think hstore, postgis)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keenan Brock committed Dec 7, 2012
1 parent 2d42ce4 commit 47d5c1f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions lib/postgres_ext/active_record/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/schema_dumper/extension_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 47d5c1f

Please sign in to comment.