data_tasks 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README.md +29 -0
- data/data_tasks.gemspec +1 -1
- data/lib/data_tasks/version.rb +1 -1
- data/lib/tasks/data_tasks.rake +20 -1
- data/test/data_tasks_test.rb +32 -7
- metadata +21 -25
- data/test/dummy/db/data_tasks/20121128182853_add_farmer.rb +0 -10
- data/test/dummy/db/migrate/20121128182851_create_farmers.rb +0 -9
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -32,6 +32,35 @@ Run all pending data tasks with rake:
|
|
32
32
|
|
33
33
|
rake data:migrate
|
34
34
|
|
35
|
+
## Common Problems
|
36
|
+
|
37
|
+
If you're running your Rails processes in threadsafe mode, you'll get errors about undefined
|
38
|
+
constants when trying to access model classes. You'll need to disable while running
|
39
|
+
rake tasks. In your environment file (ex. `config/environments/production.rb`), change:
|
40
|
+
|
41
|
+
config.threadsafe!
|
42
|
+
|
43
|
+
to:
|
44
|
+
|
45
|
+
config.threadsafe! unless $rails_rake_task
|
46
|
+
|
47
|
+
If you don't want to make that change, you can explicitly load the required model classes
|
48
|
+
inside the migration class, or define stub model classes. For example, if you wanted to
|
49
|
+
add a new SideHatch record in the migration, you could define:
|
50
|
+
|
51
|
+
class SideHatch < ActiveRecord::Base; end
|
52
|
+
|
53
|
+
class CreateSideHatch < ActiveRecord::Migration
|
54
|
+
def up
|
55
|
+
SideHatch.create!(size: 49)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
## Future
|
60
|
+
|
61
|
+
* Add a capistrano hook to run data migrations after schema migrations
|
62
|
+
* Add a heroku-san deployment strategy
|
63
|
+
|
35
64
|
## Contributing
|
36
65
|
|
37
66
|
1. Fork it
|
data/data_tasks.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
22
22
|
s.require_paths = ["lib"]
|
23
23
|
|
24
|
-
s.add_dependency "rails", "~> 3.2.
|
24
|
+
s.add_dependency "rails", "~> 3.2.11"
|
25
25
|
|
26
26
|
s.add_development_dependency "sqlite3"
|
27
27
|
s.add_development_dependency "jquery-rails"
|
data/lib/data_tasks/version.rb
CHANGED
data/lib/tasks/data_tasks.rake
CHANGED
@@ -1,8 +1,27 @@
|
|
1
1
|
namespace :data do
|
2
2
|
|
3
|
+
DATA_MIGRATIONS_PATHS = ['db/data_tasks']
|
4
|
+
|
5
|
+
desc 'Prevents existing data tasks from being run'
|
6
|
+
task assume_migrated: :environment do
|
7
|
+
migrations_paths = DATA_MIGRATIONS_PATHS
|
8
|
+
paths = migrations_paths.map {|p| "#{p}/[0-9]*_*.rb" }
|
9
|
+
versions = Dir[*paths].map do |filename|
|
10
|
+
filename.split('/').last.split('_').first.to_i
|
11
|
+
end
|
12
|
+
|
13
|
+
sm_table = ActiveRecord::Schema.quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name)
|
14
|
+
migrated = ActiveRecord::Schema.select_values("SELECT version FROM #{sm_table}").map { |v| v.to_i }
|
15
|
+
versions_to_insert = (versions - migrated).uniq
|
16
|
+
|
17
|
+
versions_to_insert.each do |version|
|
18
|
+
ActiveRecord::Schema.execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
3
22
|
desc 'Runs any data tasks that have not yet been executed.'
|
4
23
|
task migrate: :environment do
|
5
24
|
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
|
6
|
-
ActiveRecord::Migrator.migrate(
|
25
|
+
ActiveRecord::Migrator.migrate(DATA_MIGRATIONS_PATHS, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
7
26
|
end
|
8
27
|
end
|
data/test/data_tasks_test.rb
CHANGED
@@ -52,6 +52,18 @@ class DataTaskTest < ActiveSupport::TestCase
|
|
52
52
|
FileUtils.mv(tmp_filename, filename)
|
53
53
|
end
|
54
54
|
|
55
|
+
def prepare_app_for_record_migration
|
56
|
+
in_dummy_app do
|
57
|
+
execute("rails generate model Record --fixture false")
|
58
|
+
execute("rake db:drop db:create db:migrate")
|
59
|
+
execute("rails generate data_task insert_record")
|
60
|
+
|
61
|
+
filename = Dir[File.join(@dummy_task_dir, '*insert_record*')].first
|
62
|
+
insert_in_file(filename, 'class Record < ActiveRecord::Base; end', after: /class InsertRecord/)
|
63
|
+
insert_in_file(filename, 'Record.create!', after: /say_with_time/)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
55
67
|
test 'runs a data task with a rake task' do
|
56
68
|
in_dummy_app do
|
57
69
|
execute("rails generate model Farmer name:string --fixture false")
|
@@ -73,19 +85,32 @@ class DataTaskTest < ActiveSupport::TestCase
|
|
73
85
|
|
74
86
|
end
|
75
87
|
|
76
|
-
test '
|
88
|
+
test 'provides ability to assume migrations have already run' do
|
89
|
+
prepare_app_for_record_migration
|
90
|
+
|
91
|
+
in_dummy_app do
|
92
|
+
execute("rake data:assume_migrated")
|
93
|
+
execute("rake data:migrate")
|
94
|
+
|
95
|
+
record_count = execute(%{rails runner "puts Record.count"}).strip
|
96
|
+
assert_equal '0', record_count
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
test 'does not interfere with schema rollback' do
|
77
101
|
pending 'Implement the test'
|
78
102
|
end
|
79
103
|
|
80
104
|
test 'does not run the same task twice' do
|
105
|
+
prepare_app_for_record_migration
|
81
106
|
in_dummy_app do
|
82
|
-
execute("rails generate model Record --fixture false")
|
83
|
-
execute("rake db:drop db:create db:migrate")
|
84
|
-
execute("rails generate data_task insert_record")
|
107
|
+
# execute("rails generate model Record --fixture false")
|
108
|
+
# execute("rake db:drop db:create db:migrate")
|
109
|
+
# execute("rails generate data_task insert_record")
|
85
110
|
|
86
|
-
filename = Dir[File.join(@dummy_task_dir, '*insert_record*')].first
|
87
|
-
insert_in_file(filename, 'class Record < ActiveRecord::Base; end', after: /class InsertRecord/)
|
88
|
-
insert_in_file(filename, 'Record.create!', after: /say_with_time/)
|
111
|
+
# filename = Dir[File.join(@dummy_task_dir, '*insert_record*')].first
|
112
|
+
# insert_in_file(filename, 'class Record < ActiveRecord::Base; end', after: /class InsertRecord/)
|
113
|
+
# insert_in_file(filename, 'Record.create!', after: /say_with_time/)
|
89
114
|
execute("rake data:migrate")
|
90
115
|
execute("rake data:migrate")
|
91
116
|
|
metadata
CHANGED
@@ -1,64 +1,64 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_tasks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0
|
5
4
|
prerelease:
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jason Wadsworth
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version: 3.2.
|
19
|
+
version: 3.2.11
|
20
|
+
none: false
|
21
|
+
name: rails
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
|
25
|
-
none: false
|
24
|
+
requirement: !ruby/object:Gem::Requirement
|
26
25
|
requirements:
|
27
26
|
- - ~>
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
version: 3.2.
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: sqlite3
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
28
|
+
version: 3.2.11
|
33
29
|
none: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
version_requirements: !ruby/object:Gem::Requirement
|
34
32
|
requirements:
|
35
33
|
- - ! '>='
|
36
34
|
- !ruby/object:Gem::Version
|
37
35
|
version: '0'
|
36
|
+
none: false
|
37
|
+
name: sqlite3
|
38
38
|
type: :development
|
39
39
|
prerelease: false
|
40
|
-
|
41
|
-
none: false
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
42
41
|
requirements:
|
43
42
|
- - ! '>='
|
44
43
|
- !ruby/object:Gem::Version
|
45
44
|
version: '0'
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: jquery-rails
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
45
|
none: false
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
48
|
requirements:
|
51
49
|
- - ! '>='
|
52
50
|
- !ruby/object:Gem::Version
|
53
51
|
version: '0'
|
52
|
+
none: false
|
53
|
+
name: jquery-rails
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
|
-
|
57
|
-
none: false
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
58
57
|
requirements:
|
59
58
|
- - ! '>='
|
60
59
|
- !ruby/object:Gem::Version
|
61
60
|
version: '0'
|
61
|
+
none: false
|
62
62
|
description: ! "\n Adds a second migration phase to a Rails project for data migrations.
|
63
63
|
Just\n like migrations, these can be run once and only once in each environment\n
|
64
64
|
\ with a single command during every deployment.\n "
|
@@ -107,8 +107,6 @@ files:
|
|
107
107
|
- test/dummy/config/initializers/wrap_parameters.rb
|
108
108
|
- test/dummy/config/locales/en.yml
|
109
109
|
- test/dummy/config/routes.rb
|
110
|
-
- test/dummy/db/data_tasks/20121128182853_add_farmer.rb
|
111
|
-
- test/dummy/db/migrate/20121128182851_create_farmers.rb
|
112
110
|
- test/dummy/db/schema.rb
|
113
111
|
- test/dummy/lib/assets/.gitkeep
|
114
112
|
- test/dummy/log/.gitkeep
|
@@ -125,17 +123,17 @@ rdoc_options: []
|
|
125
123
|
require_paths:
|
126
124
|
- lib
|
127
125
|
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
-
none: false
|
129
126
|
requirements:
|
130
127
|
- - ! '>='
|
131
128
|
- !ruby/object:Gem::Version
|
132
129
|
version: '0'
|
133
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
130
|
none: false
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
132
|
requirements:
|
136
133
|
- - ! '>='
|
137
134
|
- !ruby/object:Gem::Version
|
138
135
|
version: '0'
|
136
|
+
none: false
|
139
137
|
requirements: []
|
140
138
|
rubyforge_project:
|
141
139
|
rubygems_version: 1.8.23
|
@@ -169,8 +167,6 @@ test_files:
|
|
169
167
|
- test/dummy/config/initializers/wrap_parameters.rb
|
170
168
|
- test/dummy/config/locales/en.yml
|
171
169
|
- test/dummy/config/routes.rb
|
172
|
-
- test/dummy/db/data_tasks/20121128182853_add_farmer.rb
|
173
|
-
- test/dummy/db/migrate/20121128182851_create_farmers.rb
|
174
170
|
- test/dummy/db/schema.rb
|
175
171
|
- test/dummy/lib/assets/.gitkeep
|
176
172
|
- test/dummy/log/.gitkeep
|