Advertisement
Guest User

Untitled

a guest
Mar 11th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #
  2. # Exports the schema of the db given in config to a file. The config
  3. # has the same structure as the db config that is used by Sequel.
  4. #
  5. # {
  6. # :user => 'linkuser',
  7. # :password => '',
  8. # :host => '127.0.0.1',
  9. # :database => 'timeline'
  10. # }
  11. #
  12. # By default the export will be run every time the app is started in development
  13. # mode
  14. #
  15. # The only thing needed to use this script is to require it, and make sure that
  16. # the DB_CONFIG constant is set
  17. #
  18. def export_db_schema(config)
  19. return false if Gem.win_platform?
  20.  
  21. filename = "#{config[:database]}.sql"
  22.  
  23. # if the config dir exists, then we put the schema in there
  24. # otherwise just in the root dir
  25. if Dir.exist? 'config'
  26. path = File.join 'config', filename
  27. else
  28. path = filename
  29. end
  30.  
  31. schema = `mysqldump --host #{config[:host]} -u #{config[:user]} --no-data --skip-comments #{config[:database]}`
  32. schema.gsub!(/ AUTO_INCREMENT=[0-9]*/, '')
  33. File.write path, schema
  34.  
  35. return true
  36. end
  37.  
  38. configure :development do
  39. result = export_db_schema DB_CONFIG
  40. puts 'db schema exported' if result
  41. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement