seed_migration 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +41 -10
- data/lib/seed_migration.rb +1 -1
- data/lib/seed_migration/engine.rb +17 -17
- data/lib/seed_migration/migrator.rb +44 -8
- data/lib/seed_migration/version.rb +1 -1
- data/lib/tasks/seed_migration_tasks.rake +9 -0
- metadata +16 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d535ca4398f774e12467c56e174923aca1c20d5
|
4
|
+
data.tar.gz: 7de4bf9f1f32a8c199a83e43e204b20f578e4f19
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca7c584a9a4b42c8d6f38d145a4d1b320ac311779c3dac98223415be2ff4439d5350b5db3a8a20e44b1885e18eb4ca201984c9abe4544d7e79493f3888d6b1ce
|
7
|
+
data.tar.gz: 900252bf1b87a11f1530e5976f95bd906c8a398277e86f7b6661a52857187603cae1eafc41b34ed18a200bea614c727d017cf393842e6ca0de4fd3938b4f5777
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[![Build Status](https://travis-ci.org/harrystech/seed_migration.svg?branch=master)](https://travis-ci.org/harrystech/seed_migration) [![Gem Version](https://badge.fury.io/rb/seed_migration.svg)](http://badge.fury.io/rb/seed_migration)
|
1
|
+
[![Build Status](https://travis-ci.org/harrystech/seed_migration.svg?branch=master)](https://travis-ci.org/harrystech/seed_migration) [![Gem Version](https://badge.fury.io/rb/seed_migration.svg)](http://badge.fury.io/rb/seed_migration) [![Code Climate](https://codeclimate.com/repos/565d2ab865f101004800136a/badges/22e9c1da0befd44cac82/gpa.svg)](https://codeclimate.com/repos/565d2ab865f101004800136a/feed) [![Test Coverage](https://codeclimate.com/repos/565d2ab865f101004800136a/badges/22e9c1da0befd44cac82/coverage.svg)](https://codeclimate.com/repos/565d2ab865f101004800136a/coverage)
|
2
2
|
|
3
3
|
# SeedMigration
|
4
4
|
|
@@ -21,7 +21,7 @@ gem 'seed_migration'
|
|
21
21
|
|
22
22
|
### Install and run the internal migrations
|
23
23
|
|
24
|
-
```
|
24
|
+
```
|
25
25
|
rake seed_migration:install:migrations
|
26
26
|
rake db:migrate
|
27
27
|
```
|
@@ -32,7 +32,7 @@ That will create the table to keep track of data migrations.
|
|
32
32
|
|
33
33
|
You can use the generator :
|
34
34
|
|
35
|
-
```
|
35
|
+
```
|
36
36
|
rails g seed_migration AddFoo
|
37
37
|
```
|
38
38
|
A new file will be created under `db/data/` using rails migration convention:
|
@@ -47,13 +47,13 @@ You'll need to implement the `#up` method and if you need to be able to rollback
|
|
47
47
|
|
48
48
|
To run all pending migrations, simply use
|
49
49
|
|
50
|
-
```
|
50
|
+
```
|
51
51
|
rake seed:migrate
|
52
52
|
```
|
53
53
|
|
54
54
|
If needed, you can run a specific migration:
|
55
55
|
|
56
|
-
```
|
56
|
+
```
|
57
57
|
rake seed:migrate MIGRATION=20140407162007_add_foo.rb
|
58
58
|
```
|
59
59
|
|
@@ -61,22 +61,42 @@ rake seed:migrate MIGRATION=20140407162007_add_foo.rb
|
|
61
61
|
|
62
62
|
Rolling back the last migration is as simple as:
|
63
63
|
|
64
|
-
```
|
64
|
+
```
|
65
65
|
rake seed:rollback
|
66
66
|
```
|
67
67
|
|
68
68
|
You can rollback more than one migration at the same time:
|
69
69
|
|
70
|
-
```
|
70
|
+
```
|
71
71
|
rake seed:rollback STEP=3 # rollback last 3 migrations
|
72
72
|
```
|
73
73
|
|
74
74
|
Or rollback a specific migration:
|
75
75
|
|
76
|
-
```
|
76
|
+
```
|
77
77
|
rake seed:rollback MIGRATION=20140407162007_add_foo.rb
|
78
78
|
```
|
79
79
|
|
80
|
+
### Status
|
81
|
+
|
82
|
+
See the status of your migrations:
|
83
|
+
|
84
|
+
```
|
85
|
+
rake seed:migrate:status
|
86
|
+
```
|
87
|
+
|
88
|
+
Example output:
|
89
|
+
|
90
|
+
```
|
91
|
+
database: seed-migrationdevelopment
|
92
|
+
|
93
|
+
Status Migration ID Migration Name
|
94
|
+
--------------------------------------------------
|
95
|
+
up 20160114153832 Add users
|
96
|
+
down 20160114153843 Add more users
|
97
|
+
down 20160114153851 Add even more users
|
98
|
+
```
|
99
|
+
|
80
100
|
### Registering models
|
81
101
|
|
82
102
|
By default no models are registered, so running seed migrations won't update the seeds file.
|
@@ -124,6 +144,17 @@ SeedMigration::Migrator.bootstrap(20140404193326)
|
|
124
144
|
|
125
145
|
Note that `seeds.rb` is only generated in development mode. Production data will not be dumped in this process.
|
126
146
|
|
147
|
+
### Checking for pending migrations
|
148
|
+
|
149
|
+
Check for pending data migrations:
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
SeedMigration::Migrator.check_pending!
|
153
|
+
```
|
154
|
+
|
155
|
+
If there are pending migrations, this will raise
|
156
|
+
`SeedMigration::Migrator::PendingMigrationError`.
|
157
|
+
|
127
158
|
### Adding seed_migrations to an existing app
|
128
159
|
|
129
160
|
If your app already contains seeds, using this gem could cause some issues.
|
@@ -207,7 +238,7 @@ SeedMigration.register Product
|
|
207
238
|
|
208
239
|
At the moment, we rely by default on
|
209
240
|
|
210
|
-
```
|
241
|
+
```ruby
|
211
242
|
ActiveRecord::Base.connection.reset_pk_sequence!
|
212
243
|
```
|
213
244
|
which is `pg` only.
|
@@ -218,7 +249,7 @@ If you need to use this gem with another database, use the `ignore_ids` configur
|
|
218
249
|
## Runnings tests
|
219
250
|
|
220
251
|
|
221
|
-
```
|
252
|
+
```
|
222
253
|
RAILS_ENV=test bundle exec rake app:db:reset
|
223
254
|
bundle exec rspec spec
|
224
255
|
```
|
data/lib/seed_migration.rb
CHANGED
@@ -2,23 +2,23 @@ require 'rails'
|
|
2
2
|
|
3
3
|
module SeedMigration
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
def self.config
|
5
|
+
DEFAULT_TABLE_NAME = 'seed_migration_data_migrations'
|
6
|
+
|
7
|
+
mattr_accessor :extend_native_migration_task
|
8
|
+
mattr_accessor :migration_table_name
|
9
|
+
mattr_accessor :ignore_ids
|
10
|
+
mattr_accessor :update_seeds_file
|
11
|
+
mattr_accessor :migrations_path
|
12
|
+
mattr_accessor :use_strict_create
|
13
|
+
|
14
|
+
self.migration_table_name = DEFAULT_TABLE_NAME
|
15
|
+
self.extend_native_migration_task = false
|
16
|
+
self.ignore_ids = false
|
17
|
+
self.update_seeds_file = true
|
18
|
+
self.migrations_path = 'data'
|
19
|
+
self.use_strict_create = false
|
20
|
+
|
21
|
+
def self.config
|
22
22
|
yield self
|
23
23
|
after_config
|
24
24
|
end
|
@@ -14,7 +14,7 @@ module SeedMigration
|
|
14
14
|
|
15
15
|
def initialize(migration_path)
|
16
16
|
@path = Pathname.new(migration_path)
|
17
|
-
raise "Can't find migration at #{@path
|
17
|
+
raise "Can't find migration at #{@path}." if !@path.exist?
|
18
18
|
end
|
19
19
|
|
20
20
|
def up
|
@@ -37,7 +37,7 @@ module SeedMigration
|
|
37
37
|
migration.migrated_on = DateTime.now
|
38
38
|
begin
|
39
39
|
migration.save!
|
40
|
-
rescue
|
40
|
+
rescue StandardError => e
|
41
41
|
puts e
|
42
42
|
end
|
43
43
|
announce("#{klass}: migrated (#{runtime}s)")
|
@@ -68,6 +68,12 @@ module SeedMigration
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
+
def self.check_pending!
|
72
|
+
if get_new_migrations.any?
|
73
|
+
raise SeedMigration::Migrator::PendingMigrationError
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
71
77
|
# Rake methods
|
72
78
|
def self.run_new_migrations
|
73
79
|
# TODO : Add warning about empty registered_models
|
@@ -105,13 +111,25 @@ module SeedMigration
|
|
105
111
|
create_seed_file
|
106
112
|
end
|
107
113
|
|
114
|
+
def self.display_migrations_status
|
115
|
+
puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
|
116
|
+
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
|
117
|
+
puts "-" * 50
|
118
|
+
|
119
|
+
up_versions = get_all_migration_versions
|
120
|
+
get_migration_files.each do |file|
|
121
|
+
version, name = parse_migration_filename(file)
|
122
|
+
status = up_versions.include?(version) ? "up" : "down"
|
123
|
+
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
108
127
|
def self.bootstrap(last_timestamp = nil)
|
109
128
|
# replace with logger ?
|
110
|
-
p "Assume migrated up to #{last_timestamp}"
|
129
|
+
p "Assume seed data migrated up to #{last_timestamp}"
|
111
130
|
files = get_migration_files(last_timestamp.to_s)
|
112
131
|
files.each do |file|
|
113
|
-
|
114
|
-
version = name.split('_').first
|
132
|
+
_, version = parse_migration_filename(file)
|
115
133
|
migration = SeedMigration::DataMigration.new
|
116
134
|
migration.version = version
|
117
135
|
migration.runtime = 0
|
@@ -145,7 +163,7 @@ module SeedMigration
|
|
145
163
|
return files
|
146
164
|
end
|
147
165
|
|
148
|
-
all_migration_versions =
|
166
|
+
all_migration_versions = get_all_migration_versions
|
149
167
|
|
150
168
|
files.each do |file|
|
151
169
|
filename = file.split('/').last
|
@@ -197,6 +215,17 @@ module SeedMigration
|
|
197
215
|
files.sort!
|
198
216
|
end
|
199
217
|
|
218
|
+
def self.get_all_migration_versions
|
219
|
+
SeedMigration::DataMigration.all.map(&:version)
|
220
|
+
end
|
221
|
+
|
222
|
+
def self.parse_migration_filename(filename)
|
223
|
+
basename = File.basename(filename, '.rb')
|
224
|
+
_, version, underscored_name = basename.match(/(\d+)_(.*)/).to_a
|
225
|
+
name = underscored_name.gsub("_", " ").capitalize
|
226
|
+
[version, name]
|
227
|
+
end
|
228
|
+
|
200
229
|
def self.create_seed_file
|
201
230
|
if !SeedMigration.update_seeds_file || !Rails.env.development?
|
202
231
|
return
|
@@ -247,9 +276,9 @@ SeedMigration::Migrator.bootstrap(#{last_migration})
|
|
247
276
|
end
|
248
277
|
|
249
278
|
if Rails::VERSION::MAJOR == 3 || defined?(ActiveModel::MassAssignmentSecurity)
|
250
|
-
model_creation_string = "#{instance.class}.#{create_method}(#{JSON.parse(sorted_attributes.to_json)
|
279
|
+
model_creation_string = "#{instance.class}.#{create_method}(#{JSON.parse(sorted_attributes.to_json)}, :without_protection => true)"
|
251
280
|
elsif Rails::VERSION::MAJOR == 4
|
252
|
-
model_creation_string = "#{instance.class}.#{create_method}(#{JSON.parse(sorted_attributes.to_json)
|
281
|
+
model_creation_string = "#{instance.class}.#{create_method}(#{JSON.parse(sorted_attributes.to_json)})"
|
253
282
|
end
|
254
283
|
|
255
284
|
# With pretty indents, please.
|
@@ -262,5 +291,12 @@ SeedMigration::Migrator.bootstrap(#{last_migration})
|
|
262
291
|
def self.create_method
|
263
292
|
SeedMigration.use_strict_create? ? 'create!' : 'create'
|
264
293
|
end
|
294
|
+
|
295
|
+
class PendingMigrationError < StandardError
|
296
|
+
def initialize
|
297
|
+
super("Data migrations are pending. To resolve this issue, "\
|
298
|
+
"run the following:\n\n\trake seed:migrate\n")
|
299
|
+
end
|
300
|
+
end
|
265
301
|
end
|
266
302
|
end
|
@@ -12,4 +12,13 @@ namespace :seed do
|
|
12
12
|
SeedMigration::Migrator.rollback_migrations(ENV["MIGRATION"], ENV["STEP"] || ENV["STEPS"] || 1)
|
13
13
|
end
|
14
14
|
end
|
15
|
+
|
16
|
+
namespace :migrate do
|
17
|
+
if !Rake::Task.task_defined?("seed:migrate:status")
|
18
|
+
desc "Display status of data migrations."
|
19
|
+
task :status => :environment do
|
20
|
+
SeedMigration::Migrator.display_migrations_status
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
15
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seed_migration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy O'Neill
|
@@ -11,10 +11,10 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2016-07-11 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
|
-
name:
|
17
|
+
name: pry
|
18
18
|
requirement: !ruby/object:Gem::Requirement
|
19
19
|
requirements:
|
20
20
|
- - ">="
|
@@ -28,21 +28,21 @@ dependencies:
|
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
|
-
name:
|
31
|
+
name: rspec-rails
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
33
33
|
requirements:
|
34
|
-
- -
|
34
|
+
- - '='
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version:
|
36
|
+
version: 2.14.2
|
37
37
|
type: :development
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
|
-
- -
|
41
|
+
- - '='
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version:
|
43
|
+
version: 2.14.2
|
44
44
|
- !ruby/object:Gem::Dependency
|
45
|
-
name:
|
45
|
+
name: rspec-mocks
|
46
46
|
requirement: !ruby/object:Gem::Requirement
|
47
47
|
requirements:
|
48
48
|
- - ">="
|
@@ -56,21 +56,21 @@ dependencies:
|
|
56
56
|
- !ruby/object:Gem::Version
|
57
57
|
version: '0'
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
|
-
name:
|
59
|
+
name: test-unit
|
60
60
|
requirement: !ruby/object:Gem::Requirement
|
61
61
|
requirements:
|
62
|
-
- -
|
62
|
+
- - "~>"
|
63
63
|
- !ruby/object:Gem::Version
|
64
|
-
version:
|
64
|
+
version: '3.0'
|
65
65
|
type: :development
|
66
66
|
prerelease: false
|
67
67
|
version_requirements: !ruby/object:Gem::Requirement
|
68
68
|
requirements:
|
69
|
-
- -
|
69
|
+
- - "~>"
|
70
70
|
- !ruby/object:Gem::Version
|
71
|
-
version:
|
71
|
+
version: '3.0'
|
72
72
|
- !ruby/object:Gem::Dependency
|
73
|
-
name:
|
73
|
+
name: codeclimate-test-reporter
|
74
74
|
requirement: !ruby/object:Gem::Requirement
|
75
75
|
requirements:
|
76
76
|
- - ">="
|
@@ -83,20 +83,6 @@ dependencies:
|
|
83
83
|
- - ">="
|
84
84
|
- !ruby/object:Gem::Version
|
85
85
|
version: '0'
|
86
|
-
- !ruby/object:Gem::Dependency
|
87
|
-
name: test-unit
|
88
|
-
requirement: !ruby/object:Gem::Requirement
|
89
|
-
requirements:
|
90
|
-
- - "~>"
|
91
|
-
- !ruby/object:Gem::Version
|
92
|
-
version: '3.0'
|
93
|
-
type: :development
|
94
|
-
prerelease: false
|
95
|
-
version_requirements: !ruby/object:Gem::Requirement
|
96
|
-
requirements:
|
97
|
-
- - "~>"
|
98
|
-
- !ruby/object:Gem::Version
|
99
|
-
version: '3.0'
|
100
86
|
description: Rails gem for Data Migrations
|
101
87
|
email:
|
102
88
|
- aoneill@harrys.com
|
@@ -142,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
128
|
version: '0'
|
143
129
|
requirements: []
|
144
130
|
rubyforge_project:
|
145
|
-
rubygems_version: 2.
|
131
|
+
rubygems_version: 2.5.1
|
146
132
|
signing_key:
|
147
133
|
specification_version: 4
|
148
134
|
summary: Rails Data Migration
|