Skip to content
Browse files

Wrap entire schema portion of restore in psqlrc block.

  • Loading branch information...
1 parent 22f94fa commit dd40382f0dc3c38b3c4ed569070313f455ebf85e @wayneeseguin committed
Showing with 33 additions and 30 deletions.
  1. +33 −30 lib/psql-cm/restore.rb
View
63 lib/psql-cm/restore.rb
@@ -19,48 +19,51 @@ def restore!
schema = cm_file.sub(".sql",'')
ensure_schema_exists(database,schema)
- debug "restore> #{database}:#{schema} < #{cm_file}"
- sh "psql #{db(database).psql_args} #{database} < #{cm_file}"
- ensure_cm_table_exists(database,schema)
- Tempfile.open('base.sql') do |temp_file|
- row = db(database).exec("SELECT content from #{schema}.#{config.cm_table}
- WHERE is_base IS true ORDER BY created_at
- DESC LIMIT 1;")
- temp_file.write(row)
- sh "psql #{db(database).psql_args} #{database} < #{temp_file.path}"
+ 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
+
+ debug "restore> #{database}:#{schema} < #{cm_file}"
+ sh "psql #{db(database).psql_args} #{database} < #{cm_file}"
+
+ ensure_cm_table_exists(database,schema)
+
+ sql = "SELECT content from #{schema}.#{config.cm_table}
+ WHERE is_base IS $1 ORDER BY created_at ASC;"
- 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 "change>\n#{row['content']}"
Tempfile.open('base.sql') do |temp_file|
- temp_file.write(row['content'])
- temp_file.close
+ row = db(database).exec(sql, [true])
+ temp_file.write(row)
+ sh "psql #{db(database).psql_args} #{database} < #{temp_file.path}"
+ end
- 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
+ debug "sql> #{sql}"
+ db(database).exec(sql,[false]).each do |row|
+ debug "change>\n#{row['content']}"
+ Tempfile.open('base.sql') do |temp_file|
+ temp_file.write(row['content'])
+ temp_file.close
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
+ ensure
+ File.open(psqlrc_file,'w') do |file|
+ file.rewind
+ file.write psqlrc
+ end
end
+
end
end
end
end
end # def restore!
- end
-end
+ end # class << self
+end # module PSQLCM

0 comments on commit dd40382

Please sign in to comment.
Something went wrong with that request. Please try again.