Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. # frozen_string_literal: true
  2. namespace :db do
  3. namespace :structure do
  4.  
  5. STRUCTURE_PATH = 'db/structure.sql'
  6.  
  7. def clean_structure_file
  8. original = File.read(STRUCTURE_PATH)
  9. cleaned = original.dup
  10.  
  11. # trailing whitespace
  12. cleaned.gsub!(/ +$/, '')
  13.  
  14. # last line
  15. cleaned.gsub!(/\n+\Z/, "\n")
  16.  
  17. # postgresql 9.5 dumps the row lock status
  18. cleaned.gsub!(/SET row_security = off;\n/, "")
  19.  
  20. # postgresql 9.6 dumps the idle transaction timeout
  21. cleaned.gsub!(/SET idle_in_transaction_session_timeout = 0;\n/, "")
  22.  
  23. # postgresql 9.5 dumps its details
  24. cleaned.gsub!(/\-\- Dumped from database version 9\.\d\.\d\n\-\- Dumped by pg_dump version 9.\d.\d\n\n/, "")
  25.  
  26. # postgresql 9.5 doesn't show empty Tablespace
  27. cleaned.gsub!(/\-\- Name: ([A-Za-z0-9_ ]+); Type: (TABLE|CONSTRAINT|INDEX); Schema: public; Owner: \-$/) do |_|
  28. "-- Name: #{$1}; Type: #{$2}; Schema: public; Owner: -; Tablespace:"
  29. end
  30.  
  31. # postgresql 9.6 shows the table and the name
  32. cleaned.gsub!(/\-\- Name: ([A-Za-z0-9_]+) ([A-Za-z0-9_]+);/) do |_|
  33. if $1 == "EXTENSION"
  34. "-- Name: #{$1} #{$2};"
  35. else
  36. "-- Name: #{$2};"
  37. end
  38. end
  39.  
  40. # postgresql 9.5 puts a space here
  41. cleaned.gsub!(/SET search_path TO "\$user", public;/, "SET search_path TO \"$user\",public;")
  42.  
  43. # postgresql 9.6 quotes the position field
  44. cleaned.gsub!(/"position" integer,/, "position integer,")
  45.  
  46. return if original == cleaned
  47.  
  48. File.open(STRUCTURE_PATH, 'w') do |f|
  49. f.write(cleaned)
  50. end
  51. end
  52.  
  53. Rake::Task['dump'].enhance do
  54. clean_structure_file
  55. end
  56.  
  57. desc "clean the structure.sql file"
  58. task :clean do
  59. clean_structure_file
  60. end
  61. end
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement