seed-fu-ndo 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Rakefile +1 -3
- data/lib/seed-fu-ndo.rb +8 -12
- data/lib/seed-fu-ndo/railtie.rb +2 -2
- data/lib/seed-fu-ndo/recorder.rb +2 -5
- data/lib/seed-fu-ndo/seeder.rb +13 -15
- data/lib/seed-fu-ndo/version.rb +1 -1
- data/lib/tasks/seed_fu_ndo.rake +8 -10
- data/spec/seeder_spec.rb +43 -50
- data/spec/spec_helper.rb +8 -9
- data/spec/test.sqlite3 +0 -0
- metadata +62 -83
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5c38383e5aff5d95ac0b56d5867a4bf7ee913d0bdb3294d9ba6e0c4745134140
|
4
|
+
data.tar.gz: 206efd0befc60f89d6f05ea4fde57ae878d1569408858942b786639d2938f871
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50565451ee67916c6640ebc43d9696e68557da83897241bc004a325005f3c4b8f709cd753abc967025337eca2de60899a5dfba36da142982161fceacd97b42c5
|
7
|
+
data.tar.gz: b9cc49c08501a06755eaf7f810cc5f6b306a0c905b6ddaa48931022e50f1f8ace854152dc2d03a6a0aa3a21670459ddb1a249c0dde46dc5664c873cd4ff1ec3e
|
data/Rakefile
CHANGED
@@ -21,7 +21,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
21
21
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
22
|
end
|
23
23
|
|
24
|
-
|
25
24
|
Bundler::GemHelper.install_tasks
|
26
25
|
|
27
26
|
require 'rspec/core/rake_task'
|
@@ -29,5 +28,4 @@ require 'rspec/core/rake_task'
|
|
29
28
|
RSpec::Core::RakeTask.new do |t|
|
30
29
|
end
|
31
30
|
|
32
|
-
|
33
|
-
task :default => :spec
|
31
|
+
task default: :spec
|
data/lib/seed-fu-ndo.rb
CHANGED
@@ -1,31 +1,29 @@
|
|
1
1
|
require 'seed-fu'
|
2
2
|
require 'seed-fu-ndo/seeder'
|
3
|
-
require 'seed-fu-ndo/railtie' if defined?(Rails) && Rails.version >=
|
3
|
+
require 'seed-fu-ndo/railtie' if defined?(Rails) && Rails.version >= '3'
|
4
4
|
|
5
5
|
module SeedFuNdo
|
6
6
|
autoload :Recorder, 'seed-fu-ndo/recorder'
|
7
|
-
|
7
|
+
|
8
8
|
mattr_reader :recorder
|
9
|
-
|
9
|
+
|
10
10
|
# Unload seed data from files. Main entry point for third-party libraries.
|
11
11
|
# [fixture_paths] The paths to look for seed files in.
|
12
12
|
# [filter] A regexp. If given, only filenames matching this expression will be loaded.
|
13
13
|
def self.unseed(fixture_paths = SeedFu.fixture_paths, filter = nil)
|
14
|
-
capture(fixture_paths, filter).reverse.each
|
15
|
-
seeder.unseed
|
16
|
-
end
|
14
|
+
capture(fixture_paths, filter).reverse.each(&:unseed)
|
17
15
|
end
|
18
|
-
|
16
|
+
|
19
17
|
# Returns all seed records from the given fixture_paths that currently
|
20
18
|
# exist in the database. No changes to the database are performed.
|
21
19
|
def self.existing_seeds(fixture_paths = SeedFu.fixture_paths, filter = nil)
|
22
|
-
existing = Hash.new {|h, k| h[k] = [] }
|
20
|
+
existing = Hash.new { |h, k| h[k] = [] }
|
23
21
|
capture(fixture_paths, filter).each do |seeder|
|
24
22
|
existing[seeder.model_class] += seeder.existing_records
|
25
23
|
end
|
26
24
|
existing
|
27
25
|
end
|
28
|
-
|
26
|
+
|
29
27
|
# Captures the seed fixtures without actually inserting anything.
|
30
28
|
# Returns a list of seeder objects.
|
31
29
|
def self.capture(fixture_paths = SeedFu.fixture_paths, filter = nil)
|
@@ -35,6 +33,4 @@ module SeedFuNdo
|
|
35
33
|
ensure
|
36
34
|
@@recorder = nil
|
37
35
|
end
|
38
|
-
|
39
|
-
|
40
|
-
end
|
36
|
+
end
|
data/lib/seed-fu-ndo/railtie.rb
CHANGED
data/lib/seed-fu-ndo/recorder.rb
CHANGED
@@ -1,18 +1,15 @@
|
|
1
1
|
module SeedFuNdo
|
2
|
-
|
3
2
|
# Records seeder instances created by Seed Fu.
|
4
3
|
class Recorder
|
5
4
|
attr_reader :seeders
|
6
|
-
|
5
|
+
|
7
6
|
def initialize
|
8
7
|
@seeders = []
|
9
8
|
end
|
10
|
-
|
9
|
+
|
11
10
|
# Record a single seeder.
|
12
11
|
def record(seeder)
|
13
12
|
seeders << seeder
|
14
13
|
end
|
15
|
-
|
16
14
|
end
|
17
|
-
|
18
15
|
end
|
data/lib/seed-fu-ndo/seeder.rb
CHANGED
@@ -1,46 +1,44 @@
|
|
1
1
|
module SeedFu
|
2
|
-
|
3
2
|
# Enhanced class from Seed Fu to allow for recording and destroying seed data.
|
4
3
|
class Seeder
|
5
|
-
|
6
4
|
attr_reader :model_class
|
7
|
-
|
8
|
-
# Record instead of inserting the data if in recording mode.
|
5
|
+
|
6
|
+
# Record instead of inserting the data if in recording mode.
|
9
7
|
def seed_with_undo
|
10
8
|
if r = SeedFuNdo.recorder
|
11
9
|
r.record self
|
12
|
-
|
10
|
+
|
13
11
|
# return existing records in case they are processed by the caller
|
14
12
|
@data.map { |record_data| find_record(record_data) }
|
15
13
|
else
|
16
14
|
seed_without_undo
|
17
15
|
end
|
18
16
|
end
|
19
|
-
|
20
|
-
|
17
|
+
alias seed_without_undo seed
|
18
|
+
alias seed seed_with_undo
|
19
|
+
|
21
20
|
# Destroy the seed data.
|
22
21
|
def unseed
|
23
22
|
@model_class.transaction do
|
24
23
|
existing_records.reverse.each { |record| unseed_record(record) }
|
25
24
|
end
|
26
25
|
end
|
27
|
-
|
26
|
+
|
28
27
|
# List of the seeded records that exist in the database.
|
29
28
|
def existing_records
|
30
29
|
@data.map { |record_data| find_record(record_data.symbolize_keys) }.compact
|
31
30
|
end
|
32
|
-
|
31
|
+
|
33
32
|
private
|
34
|
-
|
33
|
+
|
35
34
|
def unseed_record(record)
|
36
|
-
|
35
|
+
puts " - Remove #{record.inspect}" unless @options[:quiet]
|
37
36
|
|
38
|
-
|
37
|
+
record.destroy || raise(ActiveRecord::ActiveRecordError, record.errors.full_messages.join(', ').presence)
|
39
38
|
end
|
40
|
-
|
39
|
+
|
41
40
|
def find_record(data)
|
42
41
|
@model_class.where(constraint_conditions(data)).first
|
43
42
|
end
|
44
43
|
end
|
45
|
-
|
46
|
-
end
|
44
|
+
end
|
data/lib/seed-fu-ndo/version.rb
CHANGED
data/lib/tasks/seed_fu_ndo.rake
CHANGED
@@ -1,19 +1,17 @@
|
|
1
1
|
require 'seed-fu-ndo'
|
2
2
|
|
3
3
|
namespace :db do
|
4
|
-
desc
|
5
|
-
task :
|
6
|
-
|
7
|
-
filter = /#{ENV["FILTER"].gsub(/,/, "|")}/
|
8
|
-
end
|
4
|
+
desc 'Unloads seed data for the current environment.'
|
5
|
+
task unseed_fu: :environment do
|
6
|
+
filter = /#{ENV["FILTER"].tr(',', "|")}/ if ENV['FILTER']
|
9
7
|
|
10
|
-
if ENV[
|
11
|
-
fixture_paths = [ENV[
|
8
|
+
if ENV['FIXTURE_PATH']
|
9
|
+
fixture_paths = [ENV['FIXTURE_PATH'], ENV['FIXTURE_PATH'] + '/' + Rails.env]
|
12
10
|
end
|
13
11
|
|
14
12
|
SeedFuNdo.unseed(fixture_paths, filter)
|
15
13
|
end
|
16
|
-
|
14
|
+
|
17
15
|
# alias
|
18
|
-
task :
|
19
|
-
end
|
16
|
+
task unseed: 'db:unseed_fu'
|
17
|
+
end
|
data/spec/seeder_spec.rb
CHANGED
@@ -1,36 +1,32 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe SeedFu::Seeder do
|
4
|
-
|
5
|
-
|
6
|
-
it "should run single seeds" do
|
4
|
+
context 'in regular seed' do
|
5
|
+
it 'should run single seeds' do
|
7
6
|
SeededModel.seed(:title) do |s|
|
8
7
|
s.title = 'Foo'
|
9
8
|
s.login = 'Foo'
|
10
9
|
s.first_name = 'Bar'
|
11
10
|
end
|
12
|
-
|
13
|
-
SeededModel.count.
|
11
|
+
|
12
|
+
expect(SeededModel.count).to eq(1)
|
14
13
|
end
|
15
|
-
|
16
|
-
it
|
17
|
-
SeededModel.seed(:title,
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
SeededModel.count.should == 2
|
14
|
+
|
15
|
+
it 'should run multiple seeds' do
|
16
|
+
SeededModel.seed(:title,
|
17
|
+
{ title: 'Foo', login: 'Foo', first_name: 'Foo' },
|
18
|
+
title: 'Bar', login: 'Bar', first_name: 'Bar')
|
19
|
+
|
20
|
+
expect(SeededModel.count).to eq(2)
|
23
21
|
end
|
24
22
|
end
|
25
|
-
|
26
|
-
context
|
27
|
-
|
23
|
+
|
24
|
+
context 'in unseed' do
|
28
25
|
before do
|
29
26
|
SeedFuNdo.send(:class_variable_set, :@@recorder, SeedFuNdo::Recorder.new)
|
30
27
|
end
|
31
|
-
|
32
|
-
context
|
33
|
-
|
28
|
+
|
29
|
+
context 'for single seeds' do
|
34
30
|
before do
|
35
31
|
SeededModel.seed(:title) do |s|
|
36
32
|
s.title = 'Foo'
|
@@ -38,52 +34,49 @@ describe SeedFu::Seeder do
|
|
38
34
|
s.first_name = 'Bar'
|
39
35
|
end
|
40
36
|
end
|
41
|
-
|
42
|
-
it
|
43
|
-
SeededModel.count.
|
37
|
+
|
38
|
+
it 'should only record' do
|
39
|
+
expect(SeededModel.count).to eq(0)
|
44
40
|
end
|
45
|
-
|
46
|
-
it
|
41
|
+
|
42
|
+
it 'should ignore non-existing records' do
|
47
43
|
unseed
|
48
|
-
SeededModel.count.
|
44
|
+
expect(SeededModel.count).to eq(0)
|
49
45
|
end
|
50
|
-
|
51
|
-
it
|
52
|
-
SeededModel.create!(:
|
46
|
+
|
47
|
+
it 'should unseed existing records' do
|
48
|
+
SeededModel.create!(title: 'Foo')
|
53
49
|
unseed
|
54
|
-
SeededModel.count.
|
50
|
+
expect(SeededModel.count).to eq(0)
|
55
51
|
end
|
56
|
-
|
57
|
-
it
|
58
|
-
rec = SeededModel.create!(:
|
59
|
-
SeedFuNdo.recorder.seeders.first.existing_records.
|
52
|
+
|
53
|
+
it 'should find existing records' do
|
54
|
+
rec = SeededModel.create!(title: 'Foo')
|
55
|
+
expect(SeedFuNdo.recorder.seeders.first.existing_records).to eq([rec])
|
60
56
|
end
|
61
57
|
end
|
62
|
-
|
63
|
-
context
|
64
|
-
|
58
|
+
|
59
|
+
context 'for multiple seeds' do
|
65
60
|
before do
|
66
|
-
SeededModel.seed(:title,
|
67
|
-
|
68
|
-
|
69
|
-
)
|
61
|
+
SeededModel.seed(:title,
|
62
|
+
{ title: 'Foo', login: 'Foo', first_name: 'Foo' },
|
63
|
+
title: 'Bar', login: 'Bar', first_name: 'Bar')
|
70
64
|
end
|
71
|
-
|
72
|
-
it
|
73
|
-
SeededModel.count.
|
65
|
+
|
66
|
+
it 'should only record' do
|
67
|
+
expect(SeededModel.count).to eq(0)
|
74
68
|
end
|
75
|
-
|
76
|
-
it
|
77
|
-
SeededModel.create!(:
|
78
|
-
SeededModel.create!(:
|
69
|
+
|
70
|
+
it 'should rollback if one record cannot be destroyed' do
|
71
|
+
SeededModel.create!(title: 'Foo', fail_to_destroy: true)
|
72
|
+
SeededModel.create!(title: 'Bar')
|
79
73
|
expect { unseed }.to raise_error(ActiveRecord::ActiveRecordError)
|
80
|
-
SeededModel.count.
|
74
|
+
expect(SeededModel.count).to eq(2)
|
81
75
|
end
|
82
76
|
end
|
83
|
-
|
84
77
|
end
|
85
78
|
end
|
86
79
|
|
87
80
|
def unseed
|
88
|
-
SeedFuNdo.recorder.seeders.reverse.each
|
81
|
+
SeedFuNdo.recorder.seeders.reverse.each(&:unseed)
|
89
82
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -5,32 +5,31 @@ require 'logger'
|
|
5
5
|
|
6
6
|
SeedFu.quiet = true
|
7
7
|
|
8
|
-
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) +
|
8
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/../debug.log')
|
9
9
|
|
10
10
|
ActiveRecord::Base.establish_connection(
|
11
|
-
:
|
12
|
-
:
|
11
|
+
adapter: 'sqlite3',
|
12
|
+
database: File.dirname(__FILE__) + '/test.sqlite3'
|
13
13
|
)
|
14
14
|
|
15
|
-
ActiveRecord::Schema.define :
|
16
|
-
create_table :seeded_models, :
|
15
|
+
ActiveRecord::Schema.define version: 0 do
|
16
|
+
create_table :seeded_models, force: true do |t|
|
17
17
|
t.column :login, :string
|
18
18
|
t.column :first_name, :string
|
19
19
|
t.column :last_name, :string
|
20
20
|
t.column :title, :string
|
21
|
-
t.column :fail_to_destroy, :boolean, :
|
21
|
+
t.column :fail_to_destroy, :boolean, default: false
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
class SeededModel < ActiveRecord::Base
|
26
26
|
validates_presence_of :title
|
27
|
-
attr_protected :first_name
|
28
27
|
|
29
|
-
before_destroy {
|
28
|
+
before_destroy { throw :abort if fail_to_destroy }
|
30
29
|
end
|
31
30
|
|
32
31
|
RSpec.configure do |config|
|
33
32
|
config.before do
|
34
33
|
SeededModel.delete_all
|
35
34
|
end
|
36
|
-
end
|
35
|
+
end
|
data/spec/test.sqlite3
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,120 +1,99 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: seed-fu-ndo
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 2
|
10
|
-
version: 0.0.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Pascal Zumkehr
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
requirements:
|
11
|
+
date: 2018-06-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: seed-fu
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
24
17
|
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
hash: 7
|
27
|
-
segments:
|
28
|
-
- 2
|
29
|
-
- 2
|
30
|
-
- 0
|
18
|
+
- !ruby/object:Gem::Version
|
31
19
|
version: 2.2.0
|
32
|
-
prerelease: false
|
33
|
-
requirement: *id001
|
34
|
-
name: seed-fu
|
35
20
|
type: :runtime
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
|
-
none: false
|
39
|
-
requirements:
|
40
|
-
- - ">="
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
hash: 3
|
43
|
-
segments:
|
44
|
-
- 0
|
45
|
-
version: "0"
|
46
21
|
prerelease: false
|
47
|
-
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
48
28
|
name: rspec
|
49
|
-
|
50
|
-
|
51
|
-
version_requirements: &id003 !ruby/object:Gem::Requirement
|
52
|
-
none: false
|
53
|
-
requirements:
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
54
31
|
- - ">="
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
|
57
|
-
|
58
|
-
- 0
|
59
|
-
version: "0"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
60
35
|
prerelease: false
|
61
|
-
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
62
42
|
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
63
48
|
type: :development
|
64
|
-
|
65
|
-
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: While Seed Fu solves the problem for inserting and maintaining seed data
|
56
|
+
in a database, Seed Fu-ndo adds the possiblity to remove this data again.
|
57
|
+
email:
|
66
58
|
- spam@codez.ch
|
67
59
|
executables: []
|
68
|
-
|
69
60
|
extensions: []
|
70
|
-
|
71
61
|
extra_rdoc_files: []
|
72
|
-
|
73
|
-
|
74
|
-
- lib/tasks/seed_fu_ndo.rake
|
62
|
+
files:
|
63
|
+
- Rakefile
|
75
64
|
- lib/seed-fu-ndo.rb
|
76
|
-
- lib/seed-fu-ndo/
|
65
|
+
- lib/seed-fu-ndo/railtie.rb
|
77
66
|
- lib/seed-fu-ndo/recorder.rb
|
67
|
+
- lib/seed-fu-ndo/seeder.rb
|
78
68
|
- lib/seed-fu-ndo/version.rb
|
79
|
-
- lib/
|
80
|
-
- Rakefile
|
81
|
-
- spec/test.sqlite3
|
69
|
+
- lib/tasks/seed_fu_ndo.rake
|
82
70
|
- spec/seeder_spec.rb
|
83
71
|
- spec/spec_helper.rb
|
72
|
+
- spec/test.sqlite3
|
84
73
|
homepage: http://github.com/codez/seed-fu-ndo
|
85
74
|
licenses: []
|
86
|
-
|
75
|
+
metadata: {}
|
87
76
|
post_install_message:
|
88
77
|
rdoc_options: []
|
89
|
-
|
90
|
-
require_paths:
|
78
|
+
require_paths:
|
91
79
|
- lib
|
92
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
-
|
94
|
-
requirements:
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
95
82
|
- - ">="
|
96
|
-
- !ruby/object:Gem::Version
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
version: "0"
|
101
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
-
none: false
|
103
|
-
requirements:
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
104
87
|
- - ">="
|
105
|
-
- !ruby/object:Gem::Version
|
106
|
-
|
107
|
-
segments:
|
108
|
-
- 0
|
109
|
-
version: "0"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
110
90
|
requirements: []
|
111
|
-
|
112
91
|
rubyforge_project:
|
113
|
-
rubygems_version:
|
92
|
+
rubygems_version: 2.7.6
|
114
93
|
signing_key:
|
115
|
-
specification_version:
|
94
|
+
specification_version: 4
|
116
95
|
summary: Seed Fu-ndo adds undo functionality to Seed Fu
|
117
|
-
test_files:
|
96
|
+
test_files:
|
97
|
+
- spec/spec_helper.rb
|
118
98
|
- spec/test.sqlite3
|
119
99
|
- spec/seeder_spec.rb
|
120
|
-
- spec/spec_helper.rb
|