sequel-seed 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 69f6d93c61eea344cdf8319b68f627bc9abf8b97
4
- data.tar.gz: 7e3042d71cc4c888924bc7603085dd8606e00a2f
3
+ metadata.gz: fa4c96943080141c916b61f92f9c1a8757b1de76
4
+ data.tar.gz: 1286b7fb1157a44fb6ebaa0e0b6c008b410d370e
5
5
  SHA512:
6
- metadata.gz: b41ae81873403d6688ac393b75859767eac89acbeade98f67724e307c1bf18cf6d51643164581c5baafe07f6b0d4e3b18f76f09b41f6ab22a0256ca3294a92bc
7
- data.tar.gz: 243e5ae79b93013e95925b20fc98019b78680be3217a4dfa85f3071c37474bb67f515b9ad9c81df5c13f8045fb456bfd47401d17fffa6607b32619832a4f5df3
6
+ metadata.gz: 31a90821eaeba184b477f362163d1b6e9c4a6fb4e68833097c11d5dab9a3cec1a678f6f0aff776753ad7e2b86239ea4602251ae1bdb898da34e8fdb61f79709c
7
+ data.tar.gz: e27f4f75326aa710e6ff337f8d9d99fd0a21285c5f829c13c77a088d284ec32a35a64b1f0282284fdad3aaf5555bdb4c88303f4a76266e47d30b87a55ab9ff0e
data/CHANGELOG CHANGED
@@ -1 +1,10 @@
1
1
  ## Changelog
2
+
3
+ ### 0.1.4
4
+ - Environment references could be a Symbol or String
5
+ - Improve test coverage
6
+ - New project website with documentation
7
+
8
+ ### 0.1.2
9
+ - Initial version
10
+ - Seed descriptor only available as Ruby code
data/README.md CHANGED
@@ -1,22 +1,13 @@
1
- # Sequel::Seed
1
+ # Sequel::Seed [![Gem Version](https://badge.fury.io/rb/sequel-seed.svg)](http://badge.fury.io/rb/sequel-seed)
2
2
 
3
3
  > A Sequel extension to make seeds/fixtures manageable like migrations
4
4
 
5
5
  ## Usage
6
6
 
7
- Load the extension
8
-
9
- ```rb
10
- require 'sequel'
11
- require 'sequel/extensions/seed'
12
-
13
- Sequel.extension :seed
14
- ```
15
-
16
- Create a seed file (eg. `/path/to/seeds/20150928071637_currencies`)
7
+ Create a seed file (eg. `/path/to/seeds/20150928071637_currencies.rb`)
17
8
 
18
9
  ```rb
19
- Sequel.seed(:development, :test, :production) do
10
+ Sequel.seed(:development, :test) do # Applies only to "development" and "test" environments
20
11
  def run
21
12
  [
22
13
  ['USD', 'United States dollar'],
@@ -34,6 +25,15 @@ Set the environment
34
25
  Sequel::Seed.environment = :development
35
26
  ```
36
27
 
28
+ Load the extension
29
+
30
+ ```rb
31
+ require 'sequel'
32
+ require 'sequel/extensions/seed'
33
+
34
+ Sequel.extension :seed
35
+ ```
36
+
37
37
  Apply the seeds/fixtures
38
38
 
39
39
  ```rb
@@ -41,10 +41,21 @@ DB = Sequel.connect(...)
41
41
  Sequel::Seeder.apply(DB, "/path/to/seeds")
42
42
  ```
43
43
 
44
+ For more information, please check the [project website](//github.com/earaujoassis/sequel-seed/).
45
+
44
46
  ## Limitations
45
47
 
46
48
  Only timestamped seeds files
47
49
 
50
+ ## What's next?
51
+
52
+ JSON & YAML files as fixtures/seeds
53
+
54
+ ## Support
55
+
56
+ If you need any help (or have found any bug 🐞), please post it on
57
+ [/issues](//github.com/earaujoassis/sequel-seed/issues). Thank you!
58
+
48
59
  ## License
49
60
 
50
61
  [MIT License](http://earaujoassis.mit-license.org/) © Ewerton Assis
@@ -1,18 +1,22 @@
1
+ ##
1
2
  # Extension based upon Sequel::Migration and Sequel::Migrator
2
3
  #
3
4
  # Adds the Sequel::Seed and Sequel::Seeder classes, which allow
4
- # the user to easily group entity changes and seed the database
5
- # to a newer version only (migrations are directional, up or down;
6
- # seeds are always applied).
5
+ # the user to easily group entity changes and seed/fixture the database
6
+ # to a newer version only (unlike migrations, seeds are not directional).
7
7
  #
8
8
  # To load the extension:
9
9
  #
10
10
  # Sequel.extension :seed
11
+ #
12
+ # It is also important to set the environment:
13
+ #
14
+ # Sequel::Seed.environment = :development
11
15
 
12
16
  module Sequel
13
17
  class Seed
14
18
  class << self
15
- attr_accessor :environment
19
+ attr_reader :environment
16
20
  end
17
21
 
18
22
  def self.apply
@@ -27,21 +31,62 @@ module Sequel
27
31
  descendants << base
28
32
  end
29
33
 
34
+ def self.environment=(env)
35
+ @environment = env.to_sym
36
+ end
37
+
30
38
  def run
31
39
  end
32
40
  end
33
41
 
42
+ ##
43
+ # Creates a Seed subclass according to the given +block+.
44
+ #
45
+ # The +env_labels+ lists on which environments the seed should be applicable.
46
+ # If the current environment is not applicable, the seed is ignored. On the
47
+ # other hand, if it is applicable, it will be listed in Seed.descendants and
48
+ # subject to application (if it was not applied yet).
49
+ #
50
+ # Expected seed call:
51
+ #
52
+ # Sequel.seed(:test) do # seed is only applicable to the test environment
53
+ # def run
54
+ # Entity.create attribute: value
55
+ # end
56
+ # end
57
+ #
58
+ # Wildcard seed:
59
+ #
60
+ # Sequel.seed do # seed is applicable to every environment, or no environment
61
+ # def run
62
+ # Entity.create attribute: value
63
+ # end
64
+ # end
65
+ #
66
+
34
67
  def self.seed *env_labels, &block
35
- return if env_labels.length > 0 && !env_labels.include?(Seed.environment)
68
+ return if env_labels.length > 0 && !env_labels.map(&:to_sym).include?(Seed.environment)
36
69
 
37
70
  seed = Class.new(Seed)
38
71
  seed.class_eval(&block) if block_given?
39
- Seed.inherited(seed) unless Seed.descendants.include? seed
72
+ Seed.inherited(seed) unless Seed.descendants.include?(seed)
40
73
  seed
41
74
  end
42
75
 
76
+ ##
77
+ # Class resposible for applying all the seeds related to the current environment,
78
+ # if and only if they were not previously applied.
79
+ #
80
+ # To apply the seeds/fixtures:
81
+ #
82
+ # Sequel::Seeder.apply(db, directory)
83
+ #
84
+ # +db+ holds the Sequel database connection
85
+ #
86
+ # +directory+ the path to the seeds/fixtures files
87
+
43
88
  class Seeder
44
- SEED_FILE_PATTERN = /\A(\d+)_.+\.rb\z/i.freeze
89
+ SEED_FILE_PATTERN = /\A(\d+)_.+\.(rb|json|yml)\z/i.freeze
45
90
  SEED_SPLITTER = '_'.freeze
46
91
  MINIMUM_TIMESTAMP = 20000101
47
92
 
@@ -61,7 +106,7 @@ module Sequel
61
106
  next unless SEED_FILE_PATTERN.match(file)
62
107
  return TimestampSeeder if file.split(SEED_SPLITTER, 2).first.to_i > MINIMUM_TIMESTAMP
63
108
  end
64
- raise(Error, 'seeder not available for files')
109
+ raise(Error, "seeder not available for files")
65
110
  else
66
111
  self
67
112
  end
@@ -120,6 +165,18 @@ module Sequel
120
165
  end
121
166
  end
122
167
 
168
+ ##
169
+ # A Seeder subclass to apply timestamped seeds/fixtures files.
170
+ # It follows the same syntax & semantics for the Seeder superclass.
171
+ #
172
+ # To apply the seeds/fixtures:
173
+ #
174
+ # Sequel::TimestampSeeder.apply(db, directory)
175
+ #
176
+ # +db+ holds the Sequel database connection
177
+ #
178
+ # +directory+ the path to the seeds/fixtures files
179
+
123
180
  class TimestampSeeder < Seeder
124
181
  DEFAULT_SCHEMA_COLUMN = :filename
125
182
  DEFAULT_SCHEMA_TABLE = :schema_seeds
@@ -4,20 +4,17 @@ Sequel::Seed.environment = :test
4
4
 
5
5
  describe Sequel.seed do
6
6
  it 'should create a Seed descendant according to the current environment' do
7
- seed = Sequel.seed(:test) do
8
- end
7
+ seed = Sequel.seed(:test) {}
9
8
  expect(Sequel::Seed.descendants).to include seed
10
9
  end
11
10
 
12
11
  it 'should ignore a Seed not applicable to the current environment' do
13
- seed = Sequel.seed(:development) do
14
- end
12
+ seed = Sequel.seed(:development) {}
15
13
  expect(Sequel::Seed.descendants).not_to include seed
16
14
  end
17
15
 
18
16
  it 'should create a Seed applicable to every environment' do
19
- seed = Sequel.seed do
20
- end
17
+ seed = Sequel.seed {}
21
18
  expect(Sequel::Seed.descendants).to include seed
22
19
  end
23
20
  end
@@ -25,6 +22,98 @@ end
25
22
  describe Sequel::Seeder do
26
23
  let(:DB) {Sequel.sqlite}
27
24
 
25
+ describe 'environment references should be indistinguishable between Symbol and String' do
26
+ context 'when the environment is defined using a String' do
27
+ it 'should apply the Seed accordingly' do
28
+ Sequel::Seed.environment = "test"
29
+
30
+ Sequel.seed(:test) do
31
+ def run
32
+ SpecModel.create :name => 'environment defined by String'
33
+ end
34
+ end
35
+
36
+ expect(Sequel::Seed.descendants.length).to be 1
37
+ expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
38
+ expect(Sequel::Seed.descendants.length).to be 0
39
+ expect(SpecModel.dataset.all.length).to be 1
40
+ expect(SpecModel.dataset.first.name).to eq 'environment defined by String'
41
+ end
42
+ end
43
+
44
+ context 'when the Seed is defined using a String' do
45
+ it 'should apply the Seed accordingly' do
46
+ Sequel::Seed.environment = :test
47
+
48
+ Sequel.seed("test") do
49
+ def run
50
+ SpecModel.create :name => 'Seed defined by String'
51
+ end
52
+ end
53
+
54
+ expect(Sequel::Seed.descendants.length).to be 1
55
+ expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
56
+ expect(Sequel::Seed.descendants.length).to be 0
57
+ expect(SpecModel.dataset.all.length).to be 1
58
+ expect(SpecModel.dataset.first.name).to eq 'Seed defined by String'
59
+ end
60
+ end
61
+
62
+ context 'when both Seed and environment are defined using a String' do
63
+ it 'should apply the Seed accordingly' do
64
+ Sequel::Seed.environment = "test"
65
+
66
+ Sequel.seed("test") do
67
+ def run
68
+ SpecModel.create :name => 'Seed and environment defined by String'
69
+ end
70
+ end
71
+
72
+ expect(Sequel::Seed.descendants.length).to be 1
73
+ expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
74
+ expect(Sequel::Seed.descendants.length).to be 0
75
+ expect(SpecModel.dataset.all.length).to be 1
76
+ expect(SpecModel.dataset.first.name).to eq 'Seed and environment defined by String'
77
+ end
78
+ end
79
+
80
+ context 'when both Seed and environment are defined using a Symbol' do
81
+ it 'should apply the Seed accordingly' do
82
+ Sequel::Seed.environment = :test
83
+
84
+ Sequel.seed(:test) do
85
+ def run
86
+ SpecModel.create :name => 'Seed and environment defined by Symbol'
87
+ end
88
+ end
89
+
90
+ expect(Sequel::Seed.descendants.length).to be 1
91
+ expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
92
+ expect(Sequel::Seed.descendants.length).to be 0
93
+ expect(SpecModel.dataset.all.length).to be 1
94
+ expect(SpecModel.dataset.first.name).to eq 'Seed and environment defined by Symbol'
95
+ end
96
+ end
97
+
98
+ context 'when the environment is defined using a String and we have a wildcard Seed' do
99
+ it 'should apply the Seed accordingly' do
100
+ Sequel::Seed.environment = "test"
101
+
102
+ Sequel.seed do
103
+ def run
104
+ SpecModel.create :name => 'Wildcard Seed and environment defined by String'
105
+ end
106
+ end
107
+
108
+ expect(Sequel::Seed.descendants.length).to be 1
109
+ expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
110
+ expect(Sequel::Seed.descendants.length).to be 0
111
+ expect(SpecModel.dataset.all.length).to be 1
112
+ expect(SpecModel.dataset.first.name).to eq 'Wildcard Seed and environment defined by String'
113
+ end
114
+ end
115
+ end
116
+
28
117
  context 'when there\'s no Seed created' do
29
118
  it 'should not make any change to the database' do
30
119
  expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
@@ -36,7 +125,7 @@ describe Sequel::Seeder do
36
125
  it 'should change the database accordingly only once' do
37
126
  Sequel.seed do
38
127
  def run
39
- SpecModel.create name: 'some name'
128
+ SpecModel.create :name => 'should have changed'
40
129
  end
41
130
  end
42
131
 
@@ -44,21 +133,18 @@ describe Sequel::Seeder do
44
133
  expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
45
134
  expect(Sequel::Seed.descendants.length).to be 0
46
135
  expect(SpecModel.dataset.all.length).to be 1
47
- expect(SpecModel.dataset.first.name).to eq 'some name'
136
+ expect(SpecModel.dataset.first.name).to eq 'should have changed'
48
137
  end
49
138
  end
50
139
 
51
140
  context 'when the specified Seed is not applicable to the current environment' do
52
- let(:seed) {
141
+ it 'should not make any change to the database' do
53
142
  Sequel.seed(:hithere) do
54
143
  def run
55
- SpecModel.create name: 'some name'
144
+ SpecModel.create :name => 'should not have changed'
56
145
  end
57
146
  end
58
- }
59
147
 
60
- it 'should not make any change to the database' do
61
- seed
62
148
  expect(Sequel::Seed.descendants.length).to be 0
63
149
  expect {Sequel::Seeder.apply(DB, '/')}.not_to raise_error
64
150
  expect(SpecModel.dataset.all.length).to be 0
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,7 @@ require 'bundler/setup'
2
2
  Bundler.setup(:default, :development)
3
3
 
4
4
  require 'sequel'
5
+ require 'faker'
5
6
  require File.expand_path(File.dirname(__FILE__) + '/../lib/sequel/extensions/seed.rb')
6
7
 
7
8
  RSpec.configure do |config|
@@ -42,7 +43,7 @@ RSpec.configure do |config|
42
43
 
43
44
  DB.create_table(:spec_models) do
44
45
  primary_key :id, :auto_increment => true
45
- String :name
46
+ String :name
46
47
  end
47
48
 
48
49
  config.before(:each) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-seed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ewerton Assis