Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #
  2. # PostgreSQL writes two optional commands to the database schema
  3. # file, called db/structure.sql, that can only be run as a root
  4. # database user. These are not needed actually, so comment them
  5. # out automatically
  6. #
  7. # CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
  8. # COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
  9. #
  10. namespace :db do
  11. namespace :structure do
  12. desc 'Comment out the plpgsql lines from structure.sql so that a non-root user can create the test database'
  13. task :fix_plpgsql do
  14.  
  15. lines_to_strike = [
  16. 'CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;',
  17. "COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';",
  18. ]
  19.  
  20. schema_file = File.join(File.dirname(__FILE__), '..', '..', 'db', 'structure.sql')
  21. if File.exist?(schema_file) and (input = File.open(schema_file))
  22. # Create a temp file, read through the original, commenting out the target lines.
  23. lines = Array.new
  24. line_count = 0
  25. input.each_line do |line|
  26. if line
  27. line_count += 1
  28. if lines_to_strike.include?(line.strip)
  29. lines << "-- The following was commented out by rake db:structure:fix_plpgsql\n"
  30. lines << '-- ' + line
  31. else
  32. lines << line
  33. end
  34. end
  35. end # each
  36.  
  37. input.close
  38.  
  39. if lines.count > line_count
  40. # Lines were commented out, so write the new content to the file
  41. File.write(schema_file, lines.join)
  42. else
  43. # No lines were commented out, so there is no need to rewrite the file
  44. STDERR.puts "No changes are needed to #{schema_file}, it's left unchanged."
  45. end
  46.  
  47.  
  48. end
  49.  
  50. end # task
  51. end # namespace
  52. end # namespace
  53.  
  54. # Inform Rake that this should be run every time rake db:structure:dump is run
  55. Rake::Task['db:structure:dump'].enhance do
  56. Rake::Task['db:structure:fix_plpgsql'].invoke
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement