【问题标题】:Pushing a single table to Heroku将单个表推送到 Heroku
【发布时间】:2015-03-26 20:12:48
【问题描述】:

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

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

是否有一个命令可以让我只将特定的表推送到 heroku?

【问题讨论】:

  • 试试heroku db:push --tables <table1>, <table2> ..让我知道结果。
  • Umm this feature 已被弃用。我找到了来自here的命令。
  • 是的,很遗憾,我正要提到这一点。 pg:push 的类似选项不存在。

标签: ruby-on-rails database heroku deployment


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    在你填充完 seed.rb 文件并将你的 repo 推送到 heroku 之后:

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

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

    【讨论】:

    • 已部署的应用程序上已存在该表。我假设我可以只为那个表做种,但是对我想上传的每个其他表都这样做会非常耗时。
    【解决方案3】:

    我的建议是使用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 工具,例如 Importing and Exporting 用于大数据,或者只要提供的 CLI 命令未涵盖本问题中的特定情况。

    【讨论】:

    • 感谢您的彻底回复!有能力时将奖励赏金。
    • db982398 &lt; products.sql 是替换表格还是追加表格?
    • 获取凭证运行$ heroku pg:credentials:url
    • 例如,如果这是我的配置 URL postgres://user3123:passkja83kd8@ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398 那么密码是 passkja83kd8
    猜你喜欢
    • 2014-02-13
    • 2014-02-02
    • 2013-01-12
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多