Pushing a single table to Heroku

前端 未结 3 603
我在风中等你
我在风中等你 2021-02-06 00:27

I am aware of the heroku pg:push command which pushes an entire database up to Heroku.

Now that I am launching my product, I would like to be able to push u

3条回答
  •  温柔的废话
    2021-02-06 00:42

    I wrote script which extracts DB url from heroku. Then it dumps single tables from production and restores them on development/localhost. Run it like this:

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

    Code:

    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
    

提交回复
热议问题