Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Created April 30, 2014 06:49
Show Gist options
  • Select an option

  • Save stevenyap/11419669 to your computer and use it in GitHub Desktop.

Select an option

Save stevenyap/11419669 to your computer and use it in GitHub Desktop.
RakeTask - Use your own rake task to automate frequent manual tasks

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
@alois-gaucher
Copy link
Copy Markdown

WTF BRO WHO IS DOING THIS KIND OF OBSCURE STUFF

@stevenyap
Copy link
Copy Markdown
Author

7 years ago bro... mind your language :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment