繁体   English   中英

将单个表推送到Heroku

[英]Pushing a single table to Heroku

我知道heroku pg:push命令将整个数据库推送到Heroku。

现在我正在推出我的产品,我希望能够只推送一个包含本地收集信息的特定表,而不会覆盖现有表(例如用户)。

是否有一个命令使我只能将特定的表推送到heroku?

我的建议是直接使用pg_dumppsql命令使用PostgreSQL转储/恢复功能。

使用pg_dump您可以从本地数据库转储特定的表

$ pg_dump --data-only --table=products sourcedb > products.sql

然后从配置中获取Heroku PostgreSQL连接字符串

$ heroku config | grep HEROKU_POSTGRESQL

# example
# postgres://user3123:passkja83kd8@ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398

并使用从Heroku检索到的信息恢复远程数据库中的表。

$ psql -h ec2-117-21-174-214.compute-1.amazonaws.com -p 6212 -U user3123 db982398 < products.sql

您需要自定义-p-h-U参数以及数据库名称。 密码将由psql提示。

您也可以使用pg_restore过滤转储并恢复表,但我个人更喜欢psql

请注意,Heroku建议在多个文档中使用PostgreSQL工具,例如导入和导出大数据,或者提供的CLI命令不包括此问题中的特定情况。

我编写了从heroku中提取DB url的脚本。 然后它从生产中转储单个表并在development / localhost上恢复它们​​。 像这样运行:

rake production_to_development:run\['users;news;third_table',my-sushi-app\]

码:

namespace :production_to_development do
  task :run, [:tables, :app] => [:environment] do |t, args|
    tables = args["tables"].split(';')
    database_url = nil
    Bundler.with_clean_env { database_url = `heroku config:get DATABASE_URL --app=#{args["app"]}` }

    require 'addressable/uri'
    uri = Addressable::URI.parse(database_url)
    remote_database = uri.path[1,uri.path.length-2] # there is \n at the end of the path!

    tables.each do |table|
      backup_file = "tmp/#{table}.backup"
      #bin_dir = "/Applications/Postgres.app/Contents/Versions/latest/bin"
      bin_dir = ""

      dump_command = "PGPASSWORD=#{uri.password} #{bin_dir}/pg_dump --file \"#{backup_file}\" --host \"#{uri.host}\" --port \"#{uri.port}\" --username \"#{uri.user}\" --no-password --verbose --format=c --blobs --table \"public.#{table}\" \"#{remote_database}\""
      `#{dump_command}`
      `psql -U 'root' -d my_table -c 'drop table if exists #{table}'`
      `pg_restore -d my_table --no-owner  #{backup_file}`
    end

  end
end

如果我理解正确,您只需要一个数据库表,并将其本地创建的数据推送到您的Rails生产应用程序。 也许这是一种简单的方法,但您可以为表创建一个迁移,然后使用db/seeds.rb填充。

在您填充seeds.rb文件并将您的repo推送到heroku之后:

heroku run rake db:migrate
heroku run rake db:seed

此外,如果您的本地表有大量数据并且您正在使用Rails 4,请查看种子转储gem: https//github.com/rroblak/seed_dump 这将获取您现有的数据库数据并将其映射到种子格式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM