Sha256: 676402868311c9d50293ba910b4724ccf30a47abb1d6d6f79a4c1630ec3a79b4

Contents?: true

Size: 1.98 KB

Versions: 6

Compression:

Stored size: 1.98 KB

Contents

require 'securerandom'

namespace :deploy do
  namespace :check do
    task :linked_files => 'config/database.yml'
  end
end

namespace :db do
  desc 'Created the DB'
  task :create do
    on primary fetch(:migration_role) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, 'db:create'
        end
      end
    end
  end

  desc 'Create backup of the DB'
  task :backup do
    on primary fetch(:migration_role) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, 'db:backup'
        end
      end
    end
  end

  desc 'Restore the latest dump of the DB'
  task :restore do
    on primary fetch(:migration_role) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, 'db:restore'
        end
      end
    end
  end

  desc 'Downlaod to local machine the latest backup'
  task :dump_download do
    on primary fetch(:migration_role) do
      within release_path do
        bakup_file = "db/backups/#{fetch(:application)}_#{fetch(:rails_env).to_s}_latest.dump"
        download! "#{shared_path}/#{bakup_file}", bakup_file
      end
    end
  end

end

remote_file 'config/database.yml' => '/tmp/database.yml', roles: :app
after 'config/database.yml', :remove_db_tmp_file do
  File.delete '/tmp/database.yml'
end

file '/tmp/database.yml' do |t|
  default_template = <<-EOF
      base: &base
        adapter: sqlite3
        timeout: 5000
      development:
        database: #{shared_path}/db/development.sqlite3
        <<: *base
      test:
        database: #{shared_path}/db/test.sqlite3
        <<: *base
      production:
        database: #{shared_path}/db/production.sqlite3
        <<: *base
  EOF

  location = fetch(:template_dir, 'lib/capistrano/tasks/templates') + '/database.yml.erb'
  template = File.file?(location) ? File.read(location) : default_template

  config = ERB.new(template)
  File.open t.name, 'w' do |f|
    f.puts config.result(binding)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
j-cap-recipes-0.0.7 lib/j-cap-recipes/tasks/database.rake
j-cap-recipes-0.0.5 lib/j-cap-recipes/tasks/database.rake
j-cap-recipes-0.0.4 lib/j-cap-recipes/tasks/database.rake
j-cap-recipes-0.0.3 lib/j-cap-recipes/tasks/database.rake
j-cap-recipes-0.0.2 lib/capistrano-recipes/tasks/database.rake
j-cap-recipes-0.0.1 lib/capistrano-recipes/tasks/database.rake