ar_loader 0.0.4 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +9 -9
- data/README.markdown +221 -210
- data/Rakefile +76 -76
- data/lib/VERSION +1 -1
- data/lib/ar_loader.rb +66 -53
- data/lib/engine/file_definitions.rb +353 -353
- data/lib/engine/jruby/jexcel_file.rb +181 -181
- data/lib/engine/jruby/method_mapper_excel.rb +43 -43
- data/lib/engine/mapping_file_definitions.rb +87 -87
- data/lib/engine/method_detail.rb +140 -139
- data/lib/engine/method_mapper.rb +156 -156
- data/lib/engine/method_mapper_csv.rb +27 -27
- data/lib/engine/word.rb +70 -70
- data/lib/loaders/loader_base.rb +73 -60
- data/lib/loaders/spree/image_loader.rb +41 -46
- data/lib/loaders/spree/product_loader.rb +91 -92
- data/lib/to_b.rb +24 -24
- data/spec/database.yml +6 -0
- data/spec/db/migrate/20110803201325_create_testbed.rb +25 -0
- data/spec/excel_loader_spec.rb +98 -137
- data/spec/spec_helper.rb +18 -36
- data/spec/spree_loader_spec.rb +158 -0
- data/tasks/{seed_fu_product_template.erb → config/seed_fu_product_template.erb} +15 -15
- data/tasks/{tidy_config.txt → config/tidy_config.txt} +12 -12
- data/tasks/db_tasks.rake +64 -64
- data/tasks/excel_loader.rake +113 -100
- data/tasks/file_tasks.rake +37 -37
- data/tasks/spree/image_load.rake +107 -102
- data/tasks/spree/product_loader.rake +107 -106
- data/tasks/word_to_seedfu.rake +166 -166
- metadata +61 -47
data/spec/excel_loader_spec.rb
CHANGED
@@ -1,138 +1,99 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
MethodMapper.
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
MethodMapper.find_operators( @klazz )
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
mmap = MethodMapper.determine_calls( @klazz, 'SKU' )
|
101
|
-
mmap.assignment.should == 'sku='
|
102
|
-
x.send( mmap.assignment, 'TEST_SK 001' )
|
103
|
-
x.sku.should == 'TEST_SK 001'
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should enable correct assignment and sending of association to AR model" do
|
107
|
-
|
108
|
-
MethodMapper.find_operators( @klazz )
|
109
|
-
|
110
|
-
mmap = MethodMapper.determine_calls( @klazz, 'taxons' )
|
111
|
-
mmap.has_many.should == 'taxons'
|
112
|
-
|
113
|
-
x = @klazz.new
|
114
|
-
|
115
|
-
# NEW ASSOCIATION ASSIGNMENT v.,mn
|
116
|
-
x.send( mmap.has_many ) << Taxon.new
|
117
|
-
x.taxons.size.should == 1
|
118
|
-
|
119
|
-
x.send( mmap.has_many ) << [Taxon.new, Taxon.new]
|
120
|
-
x.taxons.size.should == 3
|
121
|
-
|
122
|
-
# EXISTING ASSOCIATIONS
|
123
|
-
x = Product.find :first
|
124
|
-
|
125
|
-
t = Taxonomy.find_or_create_by_name( 'BlahSpecTest' )
|
126
|
-
|
127
|
-
if x
|
128
|
-
sz = x.taxons.size
|
129
|
-
x.send(mmap.has_many) << t.root
|
130
|
-
x.taxons.size.should == sz + 1
|
131
|
-
else
|
132
|
-
puts "WARNING : Test not run could not find any Test Products"
|
133
|
-
end
|
134
|
-
|
135
|
-
end
|
136
|
-
|
137
|
-
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
require 'erb'
|
4
|
+
require File.dirname(__FILE__) + '/../lib/ar_loader'
|
5
|
+
|
6
|
+
# We are not setup as a Rails project so need to mimic an active record database setup so
|
7
|
+
# we have some AR models top test against. Create an in memory database from scratch.
|
8
|
+
#
|
9
|
+
def db_connect( env = 'test')
|
10
|
+
|
11
|
+
configuration = {:rails_env => env }
|
12
|
+
|
13
|
+
# Some active record stuff seems to rely on the RAILS_ENV being set ?
|
14
|
+
|
15
|
+
ENV['RAILS_ENV'] = configuration[:rails_env]
|
16
|
+
|
17
|
+
configuration[:database_configuration] = YAML::load(ERB.new(IO.read( File.dirname(__FILE__) + '/database.yml')).result)
|
18
|
+
db = configuration[:database_configuration][ configuration[:rails_env] ]
|
19
|
+
|
20
|
+
puts "Setting DB Config - #{db.inspect}"
|
21
|
+
ActiveRecord::Base.configurations = db
|
22
|
+
|
23
|
+
puts "Connecting to DB"
|
24
|
+
ActiveRecord::Base.establish_connection( db )
|
25
|
+
|
26
|
+
puts "Connected to DB Config - #{configuration[:rails_env]}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_in_memory_database
|
30
|
+
ActiveRecord::Migrator.up('db/migrate')
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
class TestModel < ActiveRecord::Base
|
35
|
+
has_many :TestAssociationModel
|
36
|
+
end
|
37
|
+
|
38
|
+
class TestAssociationModel < ActiveRecord::Base
|
39
|
+
belongs_to :test_model
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'ExcelLoader' do
|
43
|
+
|
44
|
+
before do
|
45
|
+
db_connect
|
46
|
+
create_in_memory_database
|
47
|
+
@klazz = TestModel
|
48
|
+
MethodMapper.clear
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should populate operators for a given AR model" do
|
52
|
+
MethodMapper.find_operators( @klazz )
|
53
|
+
|
54
|
+
MethodMapper.has_many.should_not be_empty
|
55
|
+
MethodMapper.assignments.should_not be_empty
|
56
|
+
|
57
|
+
hmf = MethodMapper.has_many_for(@klazz)
|
58
|
+
arf = MethodMapper.assignments_for(@klazz)
|
59
|
+
|
60
|
+
(hmf & arf).should_not be_empty # Associations provide << or =
|
61
|
+
|
62
|
+
MethodMapper.column_types.should be_is_a(Hash)
|
63
|
+
MethodMapper.column_types.should_not be_empty
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should populate operators respecting unique option" do
|
68
|
+
MethodMapper.find_operators( @klazz, :unique => true )
|
69
|
+
|
70
|
+
hmf = MethodMapper.has_many_for(@klazz)
|
71
|
+
arf = MethodMapper.assignments_for(@klazz)
|
72
|
+
|
73
|
+
(hmf & arf).should be_empty
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should populate assignment method and col type for different forms of a column name" do
|
77
|
+
|
78
|
+
MethodMapper.find_operators( @klazz )
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should populate both methods for different forms of an association name" do
|
82
|
+
|
83
|
+
MethodMapper.find_operators( @klazz )
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should not populate anything when non existent column name" do
|
87
|
+
MethodMapper.find_operators( @klazz )
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should enable correct assignment and sending of a value to AR model" do
|
91
|
+
MethodMapper.find_operators( @klazz )
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should enable correct assignment and sending of association to AR model" do
|
95
|
+
MethodMapper.find_operators( @klazz )
|
96
|
+
end
|
97
|
+
|
98
|
+
|
138
99
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,37 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
Spec::Runner.configure do |config|
|
22
|
-
# config.use_transactional_fixtures = true
|
23
|
-
# config.use_instantiated_fixtures = false
|
24
|
-
# config.fixture_path = RAILS_ROOT + '/spec/fixtures'
|
25
|
-
|
26
|
-
# You can declare fixtures for each behaviour like this:
|
27
|
-
# describe "...." do
|
28
|
-
# fixtures :table_a, :table_b
|
29
|
-
#
|
30
|
-
# Alternatively, if you prefer to declare them only once, you can
|
31
|
-
# do so here, like so ...
|
32
|
-
#
|
33
|
-
# config.global_fixtures = :table_a, :table_b
|
34
|
-
#
|
35
|
-
# If you declare global fixtures, be aware that they will be declared
|
36
|
-
# for all of your examples, even those that don't use them.
|
1
|
+
|
2
|
+
|
3
|
+
RSpec::Runner.configure do |config|
|
4
|
+
# config.use_transactional_fixtures = true
|
5
|
+
# config.use_instantiated_fixtures = false
|
6
|
+
# config.fixture_path = RAILS_ROOT + '/spec/fixtures'
|
7
|
+
|
8
|
+
# You can declare fixtures for each behaviour like this:
|
9
|
+
# describe "...." do
|
10
|
+
# fixtures :table_a, :table_b
|
11
|
+
#
|
12
|
+
# Alternatively, if you prefer to declare them only once, you can
|
13
|
+
# do so here, like so ...
|
14
|
+
#
|
15
|
+
# config.global_fixtures = :table_a, :table_b
|
16
|
+
#
|
17
|
+
# If you declare global fixtures, be aware that they will be declared
|
18
|
+
# for all of your examples, even those that don't use them.
|
37
19
|
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
unless defined? SPREE_ROOT
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
3
|
+
case
|
4
|
+
when ENV["SPREE_ENV_FILE"]
|
5
|
+
require ENV["SPREE_ENV_FILE"]
|
6
|
+
when File.dirname(__FILE__) =~ %r{vendor/SPREE/vendor/extensions}
|
7
|
+
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../../")}/config/environment"
|
8
|
+
else
|
9
|
+
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../")}/config/environment"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
require "#{SPREE_ROOT}/spec/spec_helper"
|
13
|
+
|
14
|
+
if File.directory?(File.dirname(__FILE__) + "/scenarios")
|
15
|
+
Scenario.load_paths.unshift File.dirname(__FILE__) + "/scenarios"
|
16
|
+
end
|
17
|
+
if File.directory?(File.dirname(__FILE__) + "/matchers")
|
18
|
+
Dir[File.dirname(__FILE__) + "/matchers/*.rb"].each {|file| require file }
|
19
|
+
end
|
20
|
+
|
21
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
22
|
+
|
23
|
+
describe 'ExcelLoader' do
|
24
|
+
|
25
|
+
before do
|
26
|
+
@klazz = Product
|
27
|
+
MethodMapper.clear
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should populate operators for a given AR model" do
|
31
|
+
MethodMapper.find_operators( @klazz )
|
32
|
+
|
33
|
+
MethodMapper.has_many.should_not be_empty
|
34
|
+
MethodMapper.assignments.should_not be_empty
|
35
|
+
|
36
|
+
hmf = MethodMapper.has_many_for(@klazz)
|
37
|
+
arf = MethodMapper.assignments_for(@klazz)
|
38
|
+
|
39
|
+
(hmf & arf).should_not be_empty # Associations provide << or =
|
40
|
+
|
41
|
+
hmf.should include('properties')
|
42
|
+
arf.should include('count_on_hand') # example of a column
|
43
|
+
arf.should include('cost_price') # example of delegated assignment (available through Variant)
|
44
|
+
|
45
|
+
MethodMapper.column_types.should be_is_a(Hash)
|
46
|
+
MethodMapper.column_types.should_not be_empty
|
47
|
+
|
48
|
+
MethodMapper.column_type_for(@klazz, 'count_on_hand').should_not be_nil
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should populate operators respecting unique option" do
|
52
|
+
MethodMapper.find_operators( @klazz, :unique => true )
|
53
|
+
|
54
|
+
hmf = MethodMapper.has_many_for(@klazz)
|
55
|
+
arf = MethodMapper.assignments_for(@klazz)
|
56
|
+
|
57
|
+
(hmf & arf).should be_empty
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should populate assignment method and col type for different forms of a column name" do
|
61
|
+
|
62
|
+
MethodMapper.find_operators( @klazz )
|
63
|
+
|
64
|
+
["Count On hand", 'count_on_hand', "Count OnHand", "COUNT ONHand"].each do |format|
|
65
|
+
mmap = MethodMapper.determine_calls( @klazz, format )
|
66
|
+
|
67
|
+
mmap.class.should == MethodDetail
|
68
|
+
|
69
|
+
mmap.assignment.should == 'count_on_hand='
|
70
|
+
mmap.has_many.should be_nil
|
71
|
+
|
72
|
+
mmap.col_type.should_not be_nil
|
73
|
+
mmap.col_type.name.should == 'count_on_hand'
|
74
|
+
mmap.col_type.default.should == 0
|
75
|
+
mmap.col_type.sql_type.should == 'int(10)'
|
76
|
+
mmap.col_type.type.should == :integer
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should populate both methods for different forms of an association name" do
|
81
|
+
|
82
|
+
MethodMapper.find_operators( @klazz )
|
83
|
+
["product_option_types", "product option types", 'product Option_types', "ProductOptionTypes", "Product_Option_Types"].each do |format|
|
84
|
+
mmap = MethodMapper.determine_calls( @klazz, format )
|
85
|
+
|
86
|
+
mmap.assignment.should == 'product_option_types='
|
87
|
+
mmap.has_many.should == 'product_option_types'
|
88
|
+
|
89
|
+
mmap.col_type.should be_nil
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
it "should not populate anything when non existent column name" do
|
95
|
+
["On sale", 'on_sale'].each do |format|
|
96
|
+
mmap = MethodMapper.determine_calls( @klazz, format )
|
97
|
+
|
98
|
+
mmap.class.should == MethodDetail
|
99
|
+
mmap.assignment.should be_nil
|
100
|
+
mmap.has_many.should be_nil
|
101
|
+
mmap.col_type.should be_nil
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should enable correct assignment and sending of a value to AR model" do
|
106
|
+
|
107
|
+
MethodMapper.find_operators( @klazz )
|
108
|
+
|
109
|
+
mmap = MethodMapper.determine_calls( @klazz, 'count on hand' )
|
110
|
+
mmap.assignment.should == 'count_on_hand='
|
111
|
+
|
112
|
+
x = @klazz.new
|
113
|
+
|
114
|
+
x.should be_new_record
|
115
|
+
|
116
|
+
x.send( mmap.assignment, 2 )
|
117
|
+
x.count_on_hand.should == 2
|
118
|
+
x.on_hand.should == 2 # helper method I know looks at same thing
|
119
|
+
|
120
|
+
mmap = MethodMapper.determine_calls( @klazz, 'SKU' )
|
121
|
+
mmap.assignment.should == 'sku='
|
122
|
+
x.send( mmap.assignment, 'TEST_SK 001' )
|
123
|
+
x.sku.should == 'TEST_SK 001'
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should enable correct assignment and sending of association to AR model" do
|
127
|
+
|
128
|
+
MethodMapper.find_operators( @klazz )
|
129
|
+
|
130
|
+
mmap = MethodMapper.determine_calls( @klazz, 'taxons' )
|
131
|
+
mmap.has_many.should == 'taxons'
|
132
|
+
|
133
|
+
x = @klazz.new
|
134
|
+
|
135
|
+
# NEW ASSOCIATION ASSIGNMENT v.,mn
|
136
|
+
x.send( mmap.has_many ) << Taxon.new
|
137
|
+
x.taxons.size.should == 1
|
138
|
+
|
139
|
+
x.send( mmap.has_many ) << [Taxon.new, Taxon.new]
|
140
|
+
x.taxons.size.should == 3
|
141
|
+
|
142
|
+
# EXISTING ASSOCIATIONS
|
143
|
+
x = Product.find :first
|
144
|
+
|
145
|
+
t = Taxonomy.find_or_create_by_name( 'BlahSpecTest' )
|
146
|
+
|
147
|
+
if x
|
148
|
+
sz = x.taxons.size
|
149
|
+
x.send(mmap.has_many) << t.root
|
150
|
+
x.taxons.size.should == sz + 1
|
151
|
+
else
|
152
|
+
puts "WARNING : Test not run could not find any Test Products"
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
p = Product.seed( :name ) do |s|
|
2
|
-
s.name = <%= @name %>
|
3
|
-
s.available_on = '2009-09-01 09:00:00.0'
|
4
|
-
s.meta_keywords = ['training', 'training']
|
5
|
-
s.meta_description = ""
|
6
|
-
s.description = '<%= @description %>'
|
7
|
-
s.price = 0.00
|
8
|
-
s.sku = '<%= @sku %>'
|
9
|
-
|
10
|
-
s.is_physical = false
|
11
|
-
s.is_private = false
|
12
|
-
|
13
|
-
s.append_association :taxons, Taxon.find_by_name( 'Training' )
|
14
|
-
|
15
|
-
end
|
1
|
+
p = Product.seed( :name ) do |s|
|
2
|
+
s.name = <%= @name %>
|
3
|
+
s.available_on = '2009-09-01 09:00:00.0'
|
4
|
+
s.meta_keywords = ['training', 'training']
|
5
|
+
s.meta_description = ""
|
6
|
+
s.description = '<%= @description %>'
|
7
|
+
s.price = 0.00
|
8
|
+
s.sku = '<%= @sku %>'
|
9
|
+
|
10
|
+
s.is_physical = false
|
11
|
+
s.is_private = false
|
12
|
+
|
13
|
+
s.append_association :taxons, Taxon.find_by_name( 'Training' )
|
14
|
+
|
15
|
+
end
|
@@ -1,13 +1,13 @@
|
|
1
|
-
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
-
# Author :: Tom Statter
|
3
|
-
# Date :: Aug 2010
|
4
|
-
#
|
5
|
-
# License:: Free, OpenSource... MIT ?
|
6
|
-
|
7
|
-
# This is the best effort I've found so far to reduce the amount of MS cruft
|
8
|
-
# to absolute minimum
|
9
|
-
# ... but unfortunately these tags will NOT BE REMOVED completely - manual cleanup still required
|
10
|
-
|
11
|
-
# TODO - add another ruby parse layer to strip these out completely
|
12
|
-
|
1
|
+
# Copyright:: (c) Autotelik Media Ltd 2011
|
2
|
+
# Author :: Tom Statter
|
3
|
+
# Date :: Aug 2010
|
4
|
+
#
|
5
|
+
# License:: Free, OpenSource... MIT ?
|
6
|
+
|
7
|
+
# This is the best effort I've found so far to reduce the amount of MS cruft
|
8
|
+
# to absolute minimum
|
9
|
+
# ... but unfortunately these tags will NOT BE REMOVED completely - manual cleanup still required
|
10
|
+
|
11
|
+
# TODO - add another ruby parse layer to strip these out completely
|
12
|
+
|
13
13
|
new-empty-tags: o:smarttagtype, st1:placename, st1:place, st1:placetype
|
data/tasks/db_tasks.rake
CHANGED
@@ -1,65 +1,65 @@
|
|
1
|
-
# Author :: Tom Statter
|
2
|
-
# Date :: Mar 2011
|
3
|
-
#
|
4
|
-
# License:: The MIT License (Free and OpenSource)
|
5
|
-
#
|
6
|
-
# About:: Additional Rake tasks useful when testing seeding DB via ARLoader
|
7
|
-
#
|
8
|
-
namespace :autotelik do
|
9
|
-
|
10
|
-
namespace :db do
|
11
|
-
|
12
|
-
SYSTEM_TABLE_EXCLUSION_LIST = ['schema_migrations']
|
13
|
-
|
14
|
-
desc "Purge the current database"
|
15
|
-
task :purge, :exclude_system_tables, :needs => [:environment] do |t, args|
|
16
|
-
require 'highline/import'
|
17
|
-
|
18
|
-
if(RAILS_ENV == 'production')
|
19
|
-
agree("WARNING: In Production database, REALLY PURGE ? [y]:")
|
20
|
-
end
|
21
|
-
|
22
|
-
config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
|
23
|
-
case config['adapter']
|
24
|
-
when "mysql", "jdbcmysql"
|
25
|
-
ActiveRecord::Base.establish_connection(config)
|
26
|
-
ActiveRecord::Base.connection.tables.each do |table|
|
27
|
-
next if(args[:exclude_system_tables] && SYSTEM_TABLE_EXCLUSION_LIST.include?(table) )
|
28
|
-
puts "purging table: #{table}"
|
29
|
-
ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
|
30
|
-
end
|
31
|
-
when "sqlite","sqlite3"
|
32
|
-
dbfile = config["database"] || config["dbfile"]
|
33
|
-
File.delete(dbfile) if File.exist?(dbfile)
|
34
|
-
when "sqlserver"
|
35
|
-
dropfkscript = "#{config["host"]}.#{config["database"]}.DP1".gsub(/\\/,'-')
|
36
|
-
`osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{dropfkscript}`
|
37
|
-
`osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{RAILS_ENV}_structure.sql`
|
38
|
-
when "oci", "oracle"
|
39
|
-
ActiveRecord::Base.establish_connection(config)
|
40
|
-
ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
|
41
|
-
ActiveRecord::Base.connection.execute(ddl)
|
42
|
-
end
|
43
|
-
when "firebird"
|
44
|
-
ActiveRecord::Base.establish_connection(config)
|
45
|
-
ActiveRecord::Base.connection.recreate_database!
|
46
|
-
else
|
47
|
-
raise "Task not supported by '#{config["adapter"]}'"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
desc "Clear database and optional directories such as assets, then run db:seed"
|
52
|
-
task :seed_again, :assets, :needs => [:environment] do |t, args|
|
53
|
-
|
54
|
-
Rake::Task['autotelik:db:purge'].invoke( true ) # i.e ENV['exclude_system_tables'] = true
|
55
|
-
|
56
|
-
if(args[:assets])
|
57
|
-
assets = "#{Rails.root}/public/assets"
|
58
|
-
FileUtils::rm_rf(assets) if(File.exists?(assets))
|
59
|
-
end
|
60
|
-
|
61
|
-
Rake::Task['db:seed'].invoke
|
62
|
-
end
|
63
|
-
|
64
|
-
end # db
|
1
|
+
# Author :: Tom Statter
|
2
|
+
# Date :: Mar 2011
|
3
|
+
#
|
4
|
+
# License:: The MIT License (Free and OpenSource)
|
5
|
+
#
|
6
|
+
# About:: Additional Rake tasks useful when testing seeding DB via ARLoader
|
7
|
+
#
|
8
|
+
namespace :autotelik do
|
9
|
+
|
10
|
+
namespace :db do
|
11
|
+
|
12
|
+
SYSTEM_TABLE_EXCLUSION_LIST = ['schema_migrations']
|
13
|
+
|
14
|
+
desc "Purge the current database"
|
15
|
+
task :purge, :exclude_system_tables, :needs => [:environment] do |t, args|
|
16
|
+
require 'highline/import'
|
17
|
+
|
18
|
+
if(RAILS_ENV == 'production')
|
19
|
+
agree("WARNING: In Production database, REALLY PURGE ? [y]:")
|
20
|
+
end
|
21
|
+
|
22
|
+
config = ActiveRecord::Base.configurations[RAILS_ENV || 'development']
|
23
|
+
case config['adapter']
|
24
|
+
when "mysql", "jdbcmysql"
|
25
|
+
ActiveRecord::Base.establish_connection(config)
|
26
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
27
|
+
next if(args[:exclude_system_tables] && SYSTEM_TABLE_EXCLUSION_LIST.include?(table) )
|
28
|
+
puts "purging table: #{table}"
|
29
|
+
ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
|
30
|
+
end
|
31
|
+
when "sqlite","sqlite3"
|
32
|
+
dbfile = config["database"] || config["dbfile"]
|
33
|
+
File.delete(dbfile) if File.exist?(dbfile)
|
34
|
+
when "sqlserver"
|
35
|
+
dropfkscript = "#{config["host"]}.#{config["database"]}.DP1".gsub(/\\/,'-')
|
36
|
+
`osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{dropfkscript}`
|
37
|
+
`osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{RAILS_ENV}_structure.sql`
|
38
|
+
when "oci", "oracle"
|
39
|
+
ActiveRecord::Base.establish_connection(config)
|
40
|
+
ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
|
41
|
+
ActiveRecord::Base.connection.execute(ddl)
|
42
|
+
end
|
43
|
+
when "firebird"
|
44
|
+
ActiveRecord::Base.establish_connection(config)
|
45
|
+
ActiveRecord::Base.connection.recreate_database!
|
46
|
+
else
|
47
|
+
raise "Task not supported by '#{config["adapter"]}'"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
desc "Clear database and optional directories such as assets, then run db:seed"
|
52
|
+
task :seed_again, :assets, :needs => [:environment] do |t, args|
|
53
|
+
|
54
|
+
Rake::Task['autotelik:db:purge'].invoke( true ) # i.e ENV['exclude_system_tables'] = true
|
55
|
+
|
56
|
+
if(args[:assets])
|
57
|
+
assets = "#{Rails.root}/public/assets"
|
58
|
+
FileUtils::rm_rf(assets) if(File.exists?(assets))
|
59
|
+
end
|
60
|
+
|
61
|
+
Rake::Task['db:seed'].invoke
|
62
|
+
end
|
63
|
+
|
64
|
+
end # db
|
65
65
|
end # autotelik
|