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 CHANGED
@@ -4,6 +4,8 @@ log/*.log
4
4
  pkg/
5
5
  *.sublime-*
6
6
  test/dummy/db/*.sqlite3
7
+ test/dummy/db/migrate/*
8
+ test/dummy/db/data_tasks/*
7
9
  test/dummy/log/*.log
8
10
  test/dummy/tmp/
9
11
  test/dummy/.sass-cache
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
@@ -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.8"
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"
@@ -1,3 +1,3 @@
1
1
  module DataTasks
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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(['db/data_tasks'], ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
25
+ ActiveRecord::Migrator.migrate(DATA_MIGRATIONS_PATHS, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
7
26
  end
8
27
  end
@@ -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 'does not interfere with database rollback' do
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: 2012-11-28 00:00:00.000000000 Z
12
+ date: 2013-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rails
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.8
19
+ version: 3.2.11
20
+ none: false
21
+ name: rails
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
24
+ requirement: !ruby/object:Gem::Requirement
26
25
  requirements:
27
26
  - - ~>
28
27
  - !ruby/object:Gem::Version
29
- version: 3.2.8
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
- version_requirements: !ruby/object:Gem::Requirement
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
- version_requirements: !ruby/object:Gem::Requirement
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
@@ -1,10 +0,0 @@
1
- class AddFarmer < ActiveRecord::Migration
2
- class Farmer < ActiveRecord::Base; end
3
- def up
4
- say_with_time "migrating data" do
5
- farmer = Farmer.new
6
- farmer.name = "Al"
7
- farmer.save!
8
- end
9
- end
10
- end
@@ -1,9 +0,0 @@
1
- class CreateFarmers < ActiveRecord::Migration
2
- def change
3
- create_table :farmers do |t|
4
- t.string :name
5
-
6
- t.timestamps
7
- end
8
- end
9
- end