Skip to content

Commit

Permalink
Merge pull request #14 from ianforsyth/feat/add-exclude-table
Browse files Browse the repository at this point in the history
Feat/add exclude table
  • Loading branch information
ddollar committed Aug 10, 2015
2 parents 5129c45 + ee0a8fc commit bf35255
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
10 changes: 6 additions & 4 deletions README.md
Expand Up @@ -8,11 +8,13 @@

$ heroku help pg:transfer
Usage: heroku pg:transfer

transfer data between databases

-f, --from DATABASE # source database, defaults to DATABASE_URL on the app
-t, --to DATABASE # target database, defaults to local $DATABASE_URL

-f, --from DATABASE # source database, defaults to DATABASE_URL on the app
-t, --to DATABASE # target database, defaults to local $DATABASE_URL
-b, --tables DATABASE # tables to copy, defaults to all
-B, --exclude_tables DATABASE # tables to exclude, defaults to none
#
$ env DATABASE_URL=postgres://localhost/myapp-development heroku pg:transfer
Source database: DATABASE on myapp.herokuapp.com
Expand Down
17 changes: 10 additions & 7 deletions init.rb
Expand Up @@ -6,14 +6,17 @@ class Heroku::Command::Pg < Heroku::Command::Base
#
# transfer data between databases
#
# -f, --from DATABASE # source database, defaults to DATABASE_URL on the app
# -t, --to DATABASE # target database, defaults to local $DATABASE_URL
# -b, --tables DATABASE # tables to copy, defaults to all
#
# -f, --from DATABASE # source database, defaults to DATABASE_URL on the app
# -t, --to DATABASE # target database, defaults to local $DATABASE_URL
# -b, --tables DATABASE # tables to copy, defaults to all
# -B, --exclude_tables DATABASE # tables to exclude, defaults to none

def transfer
tables = {}
from = options[:from] || "DATABASE"
to = options[:to] || ENV["DATABASE_URL"] || env["DATABASE_URL"]
tables = (options[:tables] || "").split(",")
to = options[:to] || ENV["DATABASE_URL"] || env["DATABASE_URL"]
tables[:include] = (options[:tables] || "").split(",")
tables[:exclude] = (options[:exclude_tables] || "").split(",")

error <<-ERROR unless to
No local DATABASE_URL detected and --to not specified.
Expand Down Expand Up @@ -61,7 +64,7 @@ def pg_dump_command(url, tables)
host = uri.host || "localhost"
port = uri.port || "5432"
user = uri.user ? "-U #{uri.user}" : ""
%{ env PGPASSWORD=#{uri.password} pg_dump --verbose -F c -h #{host} #{user} -p #{port} #{tables.map{|name| "-t #{name}" }.join(" ")} #{database} }
%{ env PGPASSWORD=#{uri.password} pg_dump --verbose -F c -h #{host} #{user} -p #{port} #{tables[:include].map{|name| "-t #{name}" }.join(" ")} #{tables[:exclude].map{|name| "-T #{name}" }.join(" ")} #{database} }
end

def pg_restore_command(url)
Expand Down

0 comments on commit bf35255

Please sign in to comment.