Advertisement
Guest User

Untitled

a guest
Apr 15th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. # This pours through the db/seeds directory and loads all files. SQL, CSV, and Ruby. The assumption is that CSV files are named exactly
  2. # as the tables that they are to populate. SQL files are executed and Ruby files loaded and run.
  3.  
  4. # Put this line in the RSpec rails_helper.rb in the config section:
  5. # config.before(:suite) do
  6. # DatabaseCleaner.clean
  7. # load(File.join(Rails.root, "spec/seeds.rb"))
  8. # end
  9.  
  10. # You can also load this file in the db/seeds.rb files to call it when run rake db:seed, etc.
  11. # load(File.join(Rails.root, "spec/seeds.rb"))
  12.  
  13. # A helper to print things out nicely.
  14. def print_feedback(message = "", options = {:banner_char => "-"})
  15. banner = options[:banner_char] * 80
  16.  
  17. puts banner
  18. message.empty? ? puts("\n") : puts(message + "\n")
  19.  
  20. if block_given?
  21. yield
  22. puts "\n...done."
  23. end
  24.  
  25. puts banner + "\n\n"
  26. end
  27.  
  28.  
  29. # Only allow seeding from environments deemed safe for such drastic action.
  30. allowed_environments = %W[test development]
  31.  
  32. if !allowed_environments.include?(Rails.env)
  33. print_feedback("Seeding only allowed in #{allowed_environments.to_sentence} environments.", :banner_char => "!")
  34. abort
  35. end
  36.  
  37. # Use the proper creds for this environment.
  38. db_config = Rails.configuration.database_configuration[Rails.env].symbolize_keys
  39.  
  40. print_feedback("Bulk loading files into #{db_config[:database]} as #{db_config[:username]}:#{db_config[:password] || "nil"} (#{Rails.env} environment)", :banner_char => "#")
  41.  
  42. # Load all of the SQL dump files in /db/seeds
  43. # Uses the database configuration for settings
  44. # Of course, requires the MySQL command line client to be installed.
  45.  
  46. print_feedback("Loading seeds...") do
  47. # This more traditional way of loading the db was failing because of BLOB fields in the data
  48. # and the way ActiveRecord chokes on these sorts of things, and huge files were problematic
  49. # as it required MySql configuration changes to get things to work.
  50. # Left here for reference.
  51.  
  52. # Dir[File.join(Rails.root, 'db', 'seeds', '*.sql')].each do |sql_file|
  53. # puts "Loading #{sql_file}..."
  54. # ActiveRecord::Base.connection.execute(File.read(sql_file).strip)
  55. # end
  56.  
  57. # Load all sql seeds using mysql command line util
  58. sql_files = Dir[File.join(Rails.root, 'db', 'seeds', '*.sql')]
  59. puts "\nLoading SQL seeds".upcase if sql_files.any?
  60.  
  61. user_string = ''
  62. user_string += "--user='#{db_config[:username]}'" if db_config[:username].present?
  63. user_string += " --password='#{db_config[:password]}'" if db_config[:password].present?
  64. user_string += " --socket='#{db_config[:socket]}'" if db_config[:socket].present?
  65. user_string += " --port='#{db_config[:port]}'" if db_config[:port].present?
  66. user_string += " --host='#{db_config[:host]}'" if db_config[:host].present?
  67.  
  68. sql_files.each do |sql_file|
  69. puts File.basename(sql_file)
  70. `mysql #{user_string} '#{db_config[:database]}' < #{sql_file}`
  71. end
  72.  
  73. # Load all of the CSV dump files in /db/seeds
  74. # file names have to be the same name as the table/model.
  75. csv_files = Dir[File.join(Rails.root, 'db', 'seeds', '*.csv')]
  76. puts "\nLoading CSV seeds".upcase if csv_files.any?
  77.  
  78. csv_files.each do |csv_file|
  79. puts File.basename(csv_file)
  80. klass = File.basename(csv_file, ".*").classify.constantize
  81.  
  82.  
  83. CSV.foreach(csv_file, {:headers => true, :header_converters => :symbol}) do |row|
  84. attributes = {}
  85. row.headers.each do |header|
  86. attributes[header] = row[header]
  87. end
  88.  
  89. klass.new(attributes).save(:validate => false)
  90. end
  91. end
  92.  
  93. # Run Ruby seeding scripts
  94. # Things not well represented in SQL dumps. These get run after all of the data seed files
  95. # so that data dependencies are met before Ruby code runs.
  96. ruby_scripts = Dir[File.join(Rails.root, 'db', 'seeds', "ruby_scripts", '*.rb')]
  97. puts "\nLoading Ruby script seeds".upcase if ruby_scripts.any?
  98.  
  99. ruby_scripts.sort.each do |seed|
  100. puts "ruby_scripts/#{File.basename(seed)}"
  101. load seed
  102. end
  103. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement