Skip to content

Instantly share code, notes, and snippets.

@bf4
Forked from njakobsen/live_database_dump.rb
Last active June 19, 2019 19:22
Show Gist options
  • Select an option

  • Save bf4/7e02f97b60c9e0bd09ce0f2ae8bdb662 to your computer and use it in GitHub Desktop.

Select an option

Save bf4/7e02f97b60c9e0bd09ce0f2ae8bdb662 to your computer and use it in GitHub Desktop.

Revisions

  1. bf4 revised this gist Jun 19, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion live_database_dump.rb
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ def database_dump

    pipe = IO.popen("pg_dump '#{database}' -F c")
    stream = response.stream
    while (line = pipe.gets)
    while (line = pipe.read(1024)) # per https://gist.github.com/njakobsen/6257887#gistcomment-1238467
    stream.write line
    Thread.pass # per https://gist.github.com/njakobsen/6257887#gistcomment-1235792
    end
  2. bf4 revised this gist Jun 19, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion live_database_dump.rb
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ def database_dump
    stream = response.stream
    while (line = pipe.gets)
    stream.write line
    sleep 0.0001 # HACK: Prevent server instance from sleeping forever if client disconnects during download
    Thread.pass # per https://gist.github.com/njakobsen/6257887#gistcomment-1235792
    end
    rescue IOError
    # Client Disconnected
  3. @njakobsen njakobsen revised this gist Aug 29, 2013. 1 changed file with 1 addition and 9 deletions.
    10 changes: 1 addition & 9 deletions live_database_dump.rb
    Original file line number Diff line number Diff line change
    @@ -19,15 +19,7 @@ def database_dump
    # Code that allows us to only mix in the live methods if we're accessing the desired action

    def dispatch(name, *args)
    extend ActionController::Live if Array(@@live_actions).include?(name.to_s)
    extend ActionController::Live if name.to_s == 'database_dump'
    super
    end

    @@live_actions = []
    def self.live(actions)
    @@live_actions |= Array(actions).collect(&:to_s)
    end

    live :database_dump

    end
  4. @njakobsen njakobsen revised this gist Aug 29, 2013. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions live_database_dump.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    class DatabaseController < ApplicationController
    def database_dump
    database = Rails.configuration.database_configuration[Rails.env]["database"]
    send_file_headers!(:type => 'application/octet-stream', :filename => "#{database}_#{Time.now.to_s(:human)}.backup")

    @@ -13,4 +14,20 @@ class DatabaseController < ApplicationController
    ensure
    pipe.close
    response.stream.close
    end

    # Code that allows us to only mix in the live methods if we're accessing the desired action

    def dispatch(name, *args)
    extend ActionController::Live if Array(@@live_actions).include?(name.to_s)
    super
    end

    @@live_actions = []
    def self.live(actions)
    @@live_actions |= Array(actions).collect(&:to_s)
    end

    live :database_dump

    end
  5. @njakobsen njakobsen revised this gist Aug 21, 2013. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions live_database_dump.rb
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,16 @@
    class DatabaseController < ApplicationController
    include ActionController::Live
    def database_dump
    database = Rails.configuration.database_configuration[Rails.env]["database"]
    send_file_headers!(:type => 'application/octet-stream', :filename => "#{database}_#{Time.now.to_s(:human)}.backup")

    pipe = IO.popen("pg_dump '#{database}' -F c")
    stream = response.stream
    while (line = pipe.gets)
    response.stream.write line
    stream.write line
    sleep 0.0001 # HACK: Prevent server instance from sleeping forever if client disconnects during download
    end
    rescue IOError
    # Client Disconnected
    ensure
    pipe.close
    response.stream.close
    end
    end
  6. @njakobsen njakobsen revised this gist Aug 17, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions live_database_dump.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    class DatabaseController < ApplicationController
    include ActionController::Live
    def database_dump
    database = Rails.configuration.database_configuration[Rails.env]["database"]
    @@ -9,3 +10,4 @@ def database_dump
    end
    response.stream.close
    end
    end
  7. @njakobsen njakobsen renamed this gist Aug 17, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. @njakobsen njakobsen created this gist Aug 17, 2013.
    11 changes: 11 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    include ActionController::Live
    def database_dump
    database = Rails.configuration.database_configuration[Rails.env]["database"]
    send_file_headers!(:type => 'application/octet-stream', :filename => "#{database}_#{Time.now.to_s(:human)}.backup")

    pipe = IO.popen("pg_dump '#{database}' -F c")
    while (line = pipe.gets)
    response.stream.write line
    end
    response.stream.close
    end