将单个 table 推送到 Heroku
Pushing a single table to Heroku
我知道 heroku pg:push
命令将整个数据库推送到 Heroku。
既然我要发布我的产品,我希望能够只推送包含本地收集的信息的特定 table,而不覆盖现有的 table(例如用户)。
是否有一个命令可以让我只将特定的 table 推送到 heroku?
我的建议是直接使用 pg_dump
and psql
命令来使用 PostgreSQL dump/restore 功能。
使用 pg_dump
您可以从本地数据库转储特定的 table
$ 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 检索到的信息在远程数据库中恢复 table。
$ psql -h ec2-117-21-174-214.compute-1.amazonaws.com -p 6212 -U user3123 db982398 < products.sql
您需要自定义 -p
、-h
和 -U
参数以及数据库名称。 psql
.
会提示输入密码
您也可以使用 pg_restore
过滤转储并恢复 table,但我个人更喜欢 psql
。
请注意,Heroku 建议在多个文档中使用 PostgreSQL 工具,例如 Importing and Exporting 用于大数据,或者只要提供的 CLI 命令不涵盖特定情况(如本问题中的情况)。
如果我理解正确,您只需要一个数据库 table 并将其本地创建的数据推送到您的 Rails 生产应用程序。也许这是一种简单的方法,但您可以为 table 创建一个迁移,然后使用 db/seeds.rb
.
进行填充
填充 seeds.rb 文件并将存储库推送到 heroku 后:
heroku run rake db:migrate
heroku run rake db:seed
此外,如果您的本地 table 有大量数据并且您正在使用 Rails 4,请查看种子转储 gem:https://github.com/rroblak/seed_dump。这将获取您现有的数据库数据并将其映射到种子格式。
我编写了从 heroku 中提取数据库 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
我知道 heroku pg:push
命令将整个数据库推送到 Heroku。
既然我要发布我的产品,我希望能够只推送包含本地收集的信息的特定 table,而不覆盖现有的 table(例如用户)。
是否有一个命令可以让我只将特定的 table 推送到 heroku?
我的建议是直接使用 pg_dump
and psql
命令来使用 PostgreSQL dump/restore 功能。
使用 pg_dump
您可以从本地数据库转储特定的 table
$ 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 检索到的信息在远程数据库中恢复 table。
$ psql -h ec2-117-21-174-214.compute-1.amazonaws.com -p 6212 -U user3123 db982398 < products.sql
您需要自定义 -p
、-h
和 -U
参数以及数据库名称。 psql
.
您也可以使用 pg_restore
过滤转储并恢复 table,但我个人更喜欢 psql
。
请注意,Heroku 建议在多个文档中使用 PostgreSQL 工具,例如 Importing and Exporting 用于大数据,或者只要提供的 CLI 命令不涵盖特定情况(如本问题中的情况)。
如果我理解正确,您只需要一个数据库 table 并将其本地创建的数据推送到您的 Rails 生产应用程序。也许这是一种简单的方法,但您可以为 table 创建一个迁移,然后使用 db/seeds.rb
.
填充 seeds.rb 文件并将存储库推送到 heroku 后:
heroku run rake db:migrate
heroku run rake db:seed
此外,如果您的本地 table 有大量数据并且您正在使用 Rails 4,请查看种子转储 gem:https://github.com/rroblak/seed_dump。这将获取您现有的数据库数据并将其映射到种子格式。
我编写了从 heroku 中提取数据库 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