versioned_seeds 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/README.mdown +52 -0
- data/Rakefile +2 -0
- data/lib/tasks/versioned_seeds.rake +18 -0
- data/lib/versioned_seeds/railtie.rb +12 -0
- data/lib/versioned_seeds/version.rb +3 -0
- data/lib/versioned_seeds.rb +54 -0
- data/versioned_seeds.gemspec +17 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.mdown
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Versioned Seeds
|
2
|
+
|
3
|
+
Versioned Seeds is an alternative to Rails seeds. It allows to store your seeds in several files and prevent from re-seeding.
|
4
|
+
|
5
|
+
Rails seeds are great when you create a project but what about new ones when the project is already running ? When you have to import some data from a CSV file ?
|
6
|
+
|
7
|
+
Versioned Seeds provides a simple, conventions based, way to do that.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
gem 'versioned_seeds', :require => false
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
You can get the last imported seeds version :
|
16
|
+
|
17
|
+
$ rake vs:status
|
18
|
+
Last seeds: 0
|
19
|
+
|
20
|
+
Given two seeds files :
|
21
|
+
|
22
|
+
``` ruby
|
23
|
+
# db/seeds/001_create_users.rb
|
24
|
+
User.create!(:username => 'admin', :password => 'password', :admin => true)
|
25
|
+
User.create!(:username => 'user1', :password => 'password')
|
26
|
+
|
27
|
+
# db/seeds/002_import_articles.rb
|
28
|
+
require 'csv'
|
29
|
+
|
30
|
+
CSV.foreach('/some/file.csv') do |line|
|
31
|
+
# ...
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
You can import only the first one by using the `vs:next` task :
|
36
|
+
|
37
|
+
$ rake vs:next
|
38
|
+
Loading: 001_create_users.rb
|
39
|
+
|
40
|
+
$ rake vs:status
|
41
|
+
Last seeds: 1
|
42
|
+
|
43
|
+
Or load all files using the `vs:all` task :
|
44
|
+
|
45
|
+
$ rake vs:all
|
46
|
+
Loading: 001_create_users.rb
|
47
|
+
Loading: 002_import_articles.rb
|
48
|
+
|
49
|
+
$ rake vs:status
|
50
|
+
Last seeds: 2
|
51
|
+
|
52
|
+
Once you're at some version, any file with a version equal or inferior to that version will not be imported next time.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'versioned_seeds'
|
2
|
+
|
3
|
+
namespace :vs do
|
4
|
+
desc "Display current seeds version"
|
5
|
+
task :status do
|
6
|
+
puts "Last seeds: #{VersionedSeeds.current_version}"
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Task description"
|
10
|
+
task :next => :environment do
|
11
|
+
VersionedSeeds.next
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Task description"
|
15
|
+
task :all => :environment do
|
16
|
+
VersionedSeeds.all
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
require "versioned_seeds/version"
|
3
|
+
|
4
|
+
module VersionedSeeds
|
5
|
+
require "versioned_seeds/railtie" if defined?(Rails)
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def next(version=current_version)
|
9
|
+
load next_seeds(version)
|
10
|
+
end
|
11
|
+
|
12
|
+
def all(version=current_version)
|
13
|
+
load all_seeds(version)
|
14
|
+
end
|
15
|
+
|
16
|
+
def load(seeds)
|
17
|
+
last_version = seeds.inject(false) do |version, seed|
|
18
|
+
puts "Loading: #{File.basename seed.file}"
|
19
|
+
require seed.file
|
20
|
+
seed.version
|
21
|
+
end
|
22
|
+
write_version last_version if last_version
|
23
|
+
end
|
24
|
+
|
25
|
+
def next_seeds(version=current_version)
|
26
|
+
seeds.keep_if { |seed| seed.version == version.next }
|
27
|
+
end
|
28
|
+
|
29
|
+
def all_seeds(version=current_version)
|
30
|
+
seeds.keep_if { |seed| seed.version > version }
|
31
|
+
end
|
32
|
+
|
33
|
+
def seeds
|
34
|
+
Dir[Rails.root+'db/seeds/*.rb'].inject([]) { |list, file|
|
35
|
+
filename = File.basename(file)
|
36
|
+
version = filename.to_i
|
37
|
+
list << OpenStruct.new(:file => file, :version => version)
|
38
|
+
list
|
39
|
+
}.sort { |seed1, seed2| seed1.version <=> seed2.version }
|
40
|
+
end
|
41
|
+
|
42
|
+
def write_version(version)
|
43
|
+
File.open(Rails.root+'.versioned_seeds', 'w') do |f|
|
44
|
+
f.puts version
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def current_version
|
49
|
+
file = Rails.root+'.versioned_seeds'
|
50
|
+
return 0 unless File.exists?(file)
|
51
|
+
File.read(file).to_i
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/versioned_seeds/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Simon COURTOIS"]
|
6
|
+
gem.email = ["scourtois@cubyx.fr"]
|
7
|
+
gem.description = %q{Manage your seed scripts by versioning them}
|
8
|
+
gem.summary = %q{Versioned Seeds provides a very simple way to manage your seed scripts by versioning them. It provides rake tasks to load them.}
|
9
|
+
gem.homepage = "http://github.com/simonc/versioned_seeds"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "versioned_seeds"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = VersionedSeeds::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: versioned_seeds
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Simon COURTOIS
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-27 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Manage your seed scripts by versioning them
|
15
|
+
email:
|
16
|
+
- scourtois@cubyx.fr
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- README.mdown
|
24
|
+
- Rakefile
|
25
|
+
- lib/tasks/versioned_seeds.rake
|
26
|
+
- lib/versioned_seeds.rb
|
27
|
+
- lib/versioned_seeds/railtie.rb
|
28
|
+
- lib/versioned_seeds/version.rb
|
29
|
+
- versioned_seeds.gemspec
|
30
|
+
homepage: http://github.com/simonc/versioned_seeds
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.10
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Versioned Seeds provides a very simple way to manage your seed scripts by
|
54
|
+
versioning them. It provides rake tasks to load them.
|
55
|
+
test_files: []
|