Skip to content
This repository
tree: b2897ba345
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 149 lines (124 sloc) 5.344 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
# TODO: DRY these up
namespace :db do
  namespace :schema do
    desc "Create a db/schema.rb file that can be portably used against any DB supported by Sequel"
    task :dump do
      Sequel.extension :schema_dumper
      db = Sequel.connect(Rails.configuration.database_configuration[Rails.env])
      File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
        file.write(db.dump_schema_migration)
      end
      Rake::Task["db:schema:dump"].reenable
    end
    
    desc "Load a schema.rb file into the database"
    task :load, :needs => :environment do
      require 'sequel-rails/storage'
      Rails::Sequel::Storage.new(Rails.env).create
      
      file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
      if File.exists?(file)
        load(file)
      else
        abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/boot.rb to limit the frameworks that will be loaded}
      end
    end
  end

  namespace :create do
    desc 'Create all the local databases defined in config/database.yml'
    task :all, :needs => :environment do
      require 'sequel-rails/storage'
      Rails::Sequel::Storage.create_all
    end
  end

  desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?"
  task :create, :env, :needs => :environment do |t, args|
    args.with_defaults(:env => Rails.env)
    
    require 'sequel-rails/storage'
    Rails::Sequel::Storage.new(args.env).create
    
    if Rails.env.development? && Rails.configuration.database_configuration['test']
      Rails::Sequel::Storage.new('test').create
    end
  end
  
  namespace :drop do
    desc 'Drops all the local databases defined in config/database.yml'
    task :all, :needs => :environment do
      require 'sequel-rails/storage'
      Rails::Sequel::Storage.drop_all
    end
  end
  
  desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?"
  task :drop, :env, :needs => :environment do |t, args|
    args.with_defaults(:env => Rails.env)
    
    require 'sequel-rails/storage'
    Rails::Sequel::Storage.new(args.env).drop
    
    if Rails.env.development? && Rails.configuration.database_configuration['test']
      Rails::Sequel::Storage.new('test').drop
    end
  end

  namespace :migrate do
    task :load => :environment do
      require 'sequel-rails/migrations'
    end

    desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x. Target specific version with VERSION=x.'
    task :redo => :load do
      if ENV["VERSION"]
        Rake::Task["db:migrate:down"].invoke
        Rake::Task["db:migrate:up"].invoke
      else
        Rake::Task["db:rollback"].invoke
        Rake::Task["db:migrate"].invoke
      end
    end


    desc 'Resets your database using your migrations for the current environment'
    task :reset => ["db:drop", "db:create", "db:migrate"]

    desc 'Runs the "up" for a given migration VERSION.'
    task :up => :load do
      version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
      raise "VERSION is required" unless version
      Rails::Sequel::Migrations.migrate_up!(version)
      Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
    end

    desc 'Runs the "down" for a given migration VERSION.'
    task :down => :load do
      version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
      raise "VERSION is required" unless version
      Rails::Sequel::Migrations.migrate_down!(version)
      Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
    end
  end
  
  desc 'Migrate the database to the latest version'
  task :migrate => :'migrate:load' do
    Rails::Sequel::Migrations.migrate_up!(ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
    Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
  end

  desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
  task :rollback => :'migrate:load' do
    step = ENV['STEP'] ? ENV['STEP'].to_i : 1
    Sequel::Migrator.rollback('db/migrate/', step)
    Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
  end

  desc 'Pushes the schema to the next version. Specify the number of steps with STEP=n'
  task :forward => :'migrate:load' do
    step = ENV['STEP'] ? ENV['STEP'].to_i : 1
    Sequel::Migrator.forward('db/migrate/', step)
    Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
  end
  
  desc 'Load the seed data from db/seeds.rb'
  task :seed => :environment do
    seed_file = File.join(Rails.root, 'db', 'seeds.rb')
    load(seed_file) if File.exist?(seed_file)
  end
  
  desc 'Create the database, load the schema, and initialize with the seed data'
  task :setup => [ 'db:create', 'db:migrate', 'db:seed' ]
  
  desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
  task :reset => [ 'db:drop', 'db:setup' ]
  
  namespace :test do
    task :prepare do
      Rails.env = 'test'
      Rake::Task['db:reset'].invoke()
      Sequel::DATABASES.each do |db|
        db.disconnect
      end
    end
  end
end

task 'test:prepare' => 'db:test:prepare'
Something went wrong with that request. Please try again.