Class: Yad::Db::Rails

Inherits:
Object
  • Object
show all
Defined in:
lib/yad/db/rails.rb

Class Method Summary collapse

Class Method Details

.build_create_db_command(release_directory, options = {}) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/yad/db/rails.rb', line 4

def self.build_create_db_command(release_directory, options = {})
  default_options = { :app_env  => 'production',
    :rake_cmd => 'rake'
  }
  options = default_options.merge(options)
  "cd #{release_directory}; #{options[:rake_cmd]} RAILS_ENV=#{options[:app_env]} db:create"
end

.build_migrate_db_command(release_directory, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/yad/db/rails.rb', line 12

def self.build_migrate_db_command(release_directory, options = {})
  default_options = { :app_env      => 'production',
    :rake_cmd     => 'rake',
    :migrate_args => '',
  }
  options = default_options.merge(options)
  "cd #{release_directory}; #{options[:rake_cmd]} RAILS_ENV=#{options[:app_env]} db:migrate #{options[:migrate_args]}"
end

.define_tasksObject



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
# File 'lib/yad/db/rails.rb', line 21

def self.define_tasks
  return if @tasks_already_defined
  @tasks_already_defined = true
  namespace :yad do
    namespace :db do

      desc "Creates the database for the application"
      remote_task :create, :roles => :app do
        break unless target_host == Rake::RemoteTask.hosts_for(:app).first
        options = Rake::RemoteTask.get_options_hash(:app_env, :rake_cmd)
        cmd = Yad::Db::Rails.build_create_db_command(current_path, options)
        run(cmd)
        puts("database created")
      end
      
      desc "Runs migrations on the database"
      remote_task :migrate, :roles => :app do
        break unless target_host == Rake::RemoteTask.hosts_for(:app).first
        options = Rake::RemoteTask.get_options_hash(:app_env, :rake_cmd, :migrate_args)
        target = Rake::RemoteTask.fetch(:migrate_target, false)
        if target
          target_directory = case target.to_sym
                             when :current then current_path
                             when :latest  then latest_release
                             else raise ArgumentError, "unknown migration target #{target.inspect}"
                             end
        else
          target_directory = latest_release
        end
        cmd = Yad::Db::Rails.build_migrate_db_command(target_directory, options)
        run(cmd)
        puts("database migrations completed")
      end
      
    end
  end
end