Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rake task to setup DB layer for non-rails deployments #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,32 @@ That's it!

### Non Rails apps

Although originally built for Rails, Seedbank should work fine in other environments such as Padrino, Grape or the new new hotness. please let us know how you get on.
Although originally built for Rails, Seedbank should work fine in other environments such as Padrino, Grape or the new new hotness.

Add the seedbank gem to your Gemfile. In Gemfile:

```ruby
gem "seedbank"
```

In your Rakefile create a `db:seed:setup` task that configures your database layer before the seed files are executed.

The example below is for an application using DRY-rb and ROM to provide database access, your setup will probably be different. In Rakefile:

```ruby
namespace :db do
namespace :seed do
task :setup do
# require our DRY container and boot our repositories
require 'container'
Application.boot!(:repository)
end
end
end

require 'seedbank'
Seedbank.load_tasks
```

### Rails 3.x

Expand Down
8 changes: 6 additions & 2 deletions lib/tasks/seed.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace :db do
override_dependency = ['db:seed:common']

namespace :seed do
task :setup do
# override in your Rakefile for your own DB backend setup
end

# Create seed tasks for all the seeds in seeds_path and add them to the dependency
# list along with the original db/seeds.rb.
common_dependencies = seed_tasks_matching(Seedbank.matcher)
Expand All @@ -15,7 +19,7 @@ namespace :db do
end

desc "Load the seed data from db/seeds.rb and db/seeds/#{Seedbank.matcher}."
task 'common' => common_dependencies
task 'common' => ['db:seed:setup'] + common_dependencies

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


# Glob through the directories under seeds_path and create a task for each adding it to the dependency list.
# Then create a task for the environment
Expand All @@ -25,7 +29,7 @@ namespace :db do
environment_dependencies = seed_tasks_matching(environment, Seedbank.matcher)

desc "Load the seed data from db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/#{environment}/#{Seedbank.matcher}."
task environment => ['db:seed:common'] + environment_dependencies
task environment => ['db:seed:setup', 'db:seed:common'] + environment_dependencies

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


override_dependency << "db:seed:#{environment}" if defined?(Rails) && Rails.env == environment
end
Expand Down
8 changes: 4 additions & 4 deletions test/lib/tasks/seed_rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.glob_dummy_seeds
it 'creates all the seed tasks' do
seeds = %w(db:seed:circular1 db:seed:circular2 db:seed:common db:seed:dependency db:seed:dependency2
db:seed:dependent db:seed:dependent_on_nested db:seed:dependent_on_several db:seed:development
db:seed:development:users db:seed:no_block db:seed:original db:seed:reference_memos db:seed:with_block_memo db:seed:with_inline_memo)
db:seed:development:users db:seed:no_block db:seed:original db:seed:reference_memos db:seed:setup db:seed:with_block_memo db:seed:with_inline_memo)

subject.map(&:to_s).must_equal seeds
end
Expand All @@ -48,7 +48,7 @@ def self.glob_dummy_seeds
it 'is dependent on the common seeds and db:seed:original' do
prerequisite_seeds = self.class.glob_dummy_seeds.sort.map do |seed_file|
['db', 'seed', File.basename(seed_file, '.seeds.rb')].join(':')
end.unshift('db:seed:original')
end.unshift('db:seed:original').unshift('db:seed:setup')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


subject.prerequisites.must_equal prerequisite_seeds
end
Expand All @@ -68,7 +68,7 @@ def setup
it 'is dependent on only the common seeds' do
prerequisite_seeds = self.class.glob_dummy_seeds.sort.map do |seed_file|
['db', 'seed', File.basename(seed_file, '.seeds.rb')].join(':')
end
end.unshift('db:seed:setup')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


subject.prerequisites.must_equal prerequisite_seeds
end
Expand Down Expand Up @@ -134,7 +134,7 @@ def setup
it 'is dependent on the seeds in the environment directory' do
prerequisite_seeds = Pathname.glob(environment_directory.join(Seedbank.matcher), Seedbank.nesting).sort.map do |seed_file|
['db', 'seed', environment, File.basename(seed_file, '.seeds.rb')].join(':')
end.unshift('db:seed:common')
end.unshift('db:seed:common').unshift('db:seed:setup')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


subject.prerequisites.must_equal prerequisite_seeds
end
Expand Down