Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.mkdn
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ Then you can use the `SHARD` environment variable to override the `master_shard`
SHARD=shard1 rake db:setup && SHARD=shard2 rake db:setup
```

For creating specific shard DB use:

```bash
SHARD=shard1 rake db:shards:create
```

For droping specific shard DB use:

```bash
SHARD=shard1 rake db:shards:drop
```

### Rails Controllers

If you want to send a specified action, or all actions from a controller, to a specific shard, use this syntax:
Expand Down
38 changes: 38 additions & 0 deletions lib/tasks/shards_db.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace :db do
namespace :shards do
desc 'Create shard DB'
task create: :load_config do
include ActiveRecord::Tasks

shard = ENV['SHARD']
rails_env = ENV['RAILS_ENV'] || "development"

DatabaseTasks.database_configuration = Octopus.config[rails_env][rails_env]

if shard
DatabaseTasks.create(DatabaseTasks.database_configuration[shard]) unless DatabaseTasks.database_configuration[shard].nil?
else
DatabaseTasks.database_configuration.each do |_, value|
DatabaseTasks.create(value)
end
end
end

desc 'Drop shard DB'
task drop: :load_config do
include ActiveRecord::Tasks

shard = ENV['SHARD']
rails_env = ENV['RAILS_ENV'] || "development"

DatabaseTasks.database_configuration = Octopus.config[rails_env][rails_env]

if shard
DatabaseTasks.drop(DatabaseTasks.database_configuration[shard]) unless DatabaseTasks.database_configuration[shard].nil?
else
DatabaseTasks.database_configuration.each do |_, value|
DatabaseTasks.drop(value)
end
end
end
end