Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ensure connections get closed.

Account for setting schema when calling psql.
  • Loading branch information...
commit a211d2d0e05777480c8710844c1ae6bd4abea55e 1 parent 8176af7
@wayneeseguin authored
Showing with 22 additions and 3 deletions.
  1. +2 −0  lib/psql-cm/database.rb
  2. +20 −3 lib/psql-cm/restore.rb
View
2  lib/psql-cm/database.rb
@@ -24,6 +24,8 @@ def db
def connect!
@db = PG.connect(@config)
+ ObjectSpace.define_finalizer(self, proc { @db.close })
+ @db
end
def reconnect!(name = @config[:dbname])
View
23 lib/psql-cm/restore.rb
@@ -32,11 +32,28 @@ def restore!
end
sql = "SELECT content from #{schema}.#{config.cm_table} where is_base IS false ORDER BY created_at ASC;"
+ debug "sql> #{sql}"
db(database).exec(sql).each do |row|
- debug "restoring cm row: #{row}"
+ debug "change>\n#{row['content']}"
Tempfile.open('base.sql') do |temp_file|
- temp_file.write(row)
- sh "psql #{db(database).psql_args} #{database} < #{temp_file.path}"
+ temp_file.write(row['content'])
+ temp_file.close
+
+ psqlrc_file = File.join(ENV['HOME'],'.psqlrc')
+ FileUtils.touch(psqlrc_file) unless File.exists?(psqlrc_file)
+ psqlrc = File.read(psqlrc_file)
+ File.open(psqlrc_file,'w') do |file|
+ file.rewind
+ file.write "SET search_path TO #{schema}; "
+ end
+ begin
+ sh "psql #{db(database).psql_args} #{database} < #{temp_file.path}"
+ ensure
+ File.open(psqlrc_file,'w') do |file|
+ file.rewind
+ file.write psqlrc
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.