Helps managing databases on local and remote stages, also on remote docker container for Capistrano 3.x.
This project is in an early stage but helps me a lot dealing with my container deployments and keeps my code clean. It is not only meant for mysql, but at the moment there is only support for mysql, feel free to contribute =)
This gem depends on capistrano-container.
Installation
Add this line to your application's Gemfile
(make sure you have installed ruby and bundler ;):
gem 'capistrano', '>= 3.0.0'
gem 'capistrano-container-db'
And then execute:
$ bundle
Or install it yourself as:
$ gem install capistrano-container-db
Dont forget to require the module in your Capfile:
require 'capistrano/container/db'
Usage
definition
Define and register a container by doing the following in your deploy.rb
(or [stage].rb
):
...
server 'www.example.com', user: 'root', roles: %w{web}
container 'db', roles: %w{db},
container_id: 'website_company_beta_db',
server: ['www.example.com']
# here is the capistrano-container-db config you need
set :db_is_container, true
set :db_user, 'wordpress'
set :db_pass, 'wordpress'
set :db_name, 'my_wordpress_db_inside_docker'
...
This configures the db access for the db container. If :db_is_container
is true, the gem uses the capistrano-container
extension to select the container by name fetch(:db_container_name)
(defaults to 'db').
If the stage name is equal :local
, export/import tasks will run on your local host (no matter if it's a dockaer container or local mysql installation).
Dont forget to add a server (even for local stage) with the role db
. server 'localhost', user: 'any', roles: %w{web db php}
commandline tasks
cap db:export # export a local, remote or remote container mysql db
cap db:import # import a local, remote or remote container mysql db
cap db:execute # execute a mysql command local, remote or container host
default configuration
set :db_user, 'root'
set :db_pass, ''
set :db_additional_auth_args, []
set :db_name, ''
set :db_additional_restore_args, []
# dont use --database statement, so no use '...' will be generated and we
# can have different db names local and remote
set :db_additional_dump_args, ['--no-create-db']
set :db_remote_dump, '/tmp/dump.sql'
set :db_local_dump, 'config/db/dump.sql'
set :db_is_container, false
set :db_container_name, 'db'
set :local_stage_name, :local
set :filter_on_import, lambda{ |sql_dump| return sql_dump } -> !not implemented yet
TODO
- adapter pattern for other db engines.
- integration tests.
- :filter_on_import
- add db:execute task
Changes
Version 0.0.6
- use clean module namespaces
- add
--lock-tables=false
as default dump arg
Version 0.0.5
- add "CREATE DATABASE IF NOT EXISTS" to import statements
- provide additional mysql auth/dump/restore args
Version 0.0.4
- readme
Version 0.0.3
- remove debug expression
Version 0.0.2
- autodetect local and remote container
Version 0.0.1
- initial release
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request