This file is placed in /lib/tasks and it doesn't matter what is the filename but it must end in .rake eg. some_name.rake
namespace :db do
desc 'Export database into db/seeds.rb'
task :export do
backup_file = 'db/seeds-bkup.rb'
seed_file = 'db/seeds.rb'
puts '*** Exporting database into db/seeds.rb ***'
puts 'Remove backup file - seeds-bkup.rb'
File.delete(backup_file) if File.exists? backup_file
puts 'Rename current seeds.rb to seeds-bkup.rb'
File.rename(seed_file, backup_file) if File.exists? seed_file
puts 'Generate seed data using gem seed_dump'
ENV['MODELS'] = 'Category,Winner,Credit,Portfolio,Judge'
ENV['EXCLUDE'] = ''
Rake::Task['db:seed:dump'].invoke
puts 'Modify seeds.rb with custom data'
contents =
<<-eos
User.destroy_all
Judge.delete_all
Category.destroy_all
Winner.delete_all
Credit.delete_all
Portfolio.delete_all
User.create!(email: 'admin@example.com', password: '123123', password_confirmation: '123123')
eos
contents << File.open(seed_file, "r:UTF-8", &:read)
contents << 'ActiveRecord::Base.connection.tables.each { |t| ActiveRecord::Base.connection.reset_pk_sequence!(t) }'
File.delete(seed_file)
File.open(seed_file, 'a') { |f| f.write(contents) }
puts '*** Export database completed ***'
end
end
WTF BRO WHO IS DOING THIS KIND OF OBSCURE STUFF