diff --git a/README.md b/README.md index bb1987d..18df459 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/init.rb b/init.rb index 2dc5319..3e3fc36 100644 --- a/init.rb +++ b/init.rb @@ -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. @@ -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)