mongoid_rails_5_migrations 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +132 -0
- data/README.rdoc +56 -0
- data/Rakefile +12 -0
- data/lib/mongoid_rails_migrations.rb +9 -0
- data/lib/mongoid_rails_migrations/active_record_ext/migrations.rb +414 -0
- data/lib/mongoid_rails_migrations/models/data_migration.rb +5 -0
- data/lib/mongoid_rails_migrations/mongoid_ext/mongoid.rb +21 -0
- data/lib/mongoid_rails_migrations/mongoid_ext/railtie.rb +18 -0
- data/lib/mongoid_rails_migrations/mongoid_ext/railties/database.rake +93 -0
- data/lib/mongoid_rails_migrations/version.rb +3 -0
- data/lib/rails/generators/mongoid/migration/migration_generator.rb +16 -0
- data/lib/rails/generators/mongoid/migration/templates/migration.rb +7 -0
- data/lib/rails/generators/mongoid/mongoid_generator.rb +26 -0
- data/mongoid_rails_migrations.gemspec +32 -0
- data/test/config.rb +9 -0
- data/test/helper.rb +34 -0
- data/test/migration_test.rb +164 -0
- data/test/migrations/duplicate/names/20100513073457_add_duplicate_survey_schema.rb +9 -0
- data/test/migrations/duplicate/names/20100513073724_add_duplicate_survey_schema.rb +9 -0
- data/test/migrations/duplicate/versions/20100513073457_add_another_duplicate_survey_schema.rb +9 -0
- data/test/migrations/duplicate/versions/20100513073457_add_duplicate_survey_schema.rb +9 -0
- data/test/migrations/other_valid/20160509073459_add_other_plan_survey_schema.rb +9 -0
- data/test/migrations/valid/20100513054656_add_baseline_survey_schema.rb +10 -0
- data/test/migrations/valid/20100513063902_add_improvement_plan_survey_schema.rb +9 -0
- data/test/models/survey_schema.rb +6 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b340ce184447bd9109f4da7f29712499df5305d5
|
4
|
+
data.tar.gz: 1adc0417a8cba633ae84be4b0029726ec65f4fd7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 31f565040f832b82ea058861d6557d1028c685de3dc726e0438df10a1d2f999ee76d8dfb29bd9fb5b55176f07cfa3dd460fefb9a8e2451f4528fc927d6b0ac55
|
7
|
+
data.tar.gz: d084d79b1b4ef535f2eaf463af076b9409b3c78593b67b1cd95af394ff57040c7fd812540bde6b8aa01604f1bf7c1ca6a40b0340215332dbe7c8a95b657268d0
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mongoid_rails_migrations (1.1.0)
|
5
|
+
activesupport (>= 4.2.0)
|
6
|
+
bundler (>= 1.0.0)
|
7
|
+
mongoid (>= 4.0.0)
|
8
|
+
rails (>= 4.2.0)
|
9
|
+
railties (>= 4.2.0)
|
10
|
+
|
11
|
+
GEM
|
12
|
+
remote: http://rubygems.org/
|
13
|
+
specs:
|
14
|
+
actionmailer (4.2.5.1)
|
15
|
+
actionpack (= 4.2.5.1)
|
16
|
+
actionview (= 4.2.5.1)
|
17
|
+
activejob (= 4.2.5.1)
|
18
|
+
mail (~> 2.5, >= 2.5.4)
|
19
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
20
|
+
actionpack (4.2.5.1)
|
21
|
+
actionview (= 4.2.5.1)
|
22
|
+
activesupport (= 4.2.5.1)
|
23
|
+
rack (~> 1.6)
|
24
|
+
rack-test (~> 0.6.2)
|
25
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
26
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
27
|
+
actionview (4.2.5.1)
|
28
|
+
activesupport (= 4.2.5.1)
|
29
|
+
builder (~> 3.1)
|
30
|
+
erubis (~> 2.7.0)
|
31
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
32
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
33
|
+
activejob (4.2.5.1)
|
34
|
+
activesupport (= 4.2.5.1)
|
35
|
+
globalid (>= 0.3.0)
|
36
|
+
activemodel (4.2.5.1)
|
37
|
+
activesupport (= 4.2.5.1)
|
38
|
+
builder (~> 3.1)
|
39
|
+
activerecord (4.2.5.1)
|
40
|
+
activemodel (= 4.2.5.1)
|
41
|
+
activesupport (= 4.2.5.1)
|
42
|
+
arel (~> 6.0)
|
43
|
+
activesupport (4.2.5.1)
|
44
|
+
i18n (~> 0.7)
|
45
|
+
json (~> 1.7, >= 1.7.7)
|
46
|
+
minitest (~> 5.1)
|
47
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
48
|
+
tzinfo (~> 1.1)
|
49
|
+
arel (6.0.3)
|
50
|
+
bson (4.0.3)
|
51
|
+
builder (3.2.2)
|
52
|
+
coderay (1.1.1)
|
53
|
+
concurrent-ruby (1.0.1)
|
54
|
+
erubis (2.7.0)
|
55
|
+
globalid (0.3.6)
|
56
|
+
activesupport (>= 4.1.0)
|
57
|
+
i18n (0.7.0)
|
58
|
+
json (1.8.3)
|
59
|
+
loofah (2.0.3)
|
60
|
+
nokogiri (>= 1.5.9)
|
61
|
+
mail (2.6.3)
|
62
|
+
mime-types (>= 1.16, < 3)
|
63
|
+
method_source (0.8.2)
|
64
|
+
mime-types (2.99.1)
|
65
|
+
mini_portile2 (2.0.0)
|
66
|
+
minitest (5.8.4)
|
67
|
+
mongo (2.2.3)
|
68
|
+
bson (~> 4.0)
|
69
|
+
mongoid (5.1.1)
|
70
|
+
activemodel (~> 4.0)
|
71
|
+
mongo (~> 2.1)
|
72
|
+
origin (~> 2.2)
|
73
|
+
tzinfo (>= 0.3.37)
|
74
|
+
nokogiri (1.6.7.2)
|
75
|
+
mini_portile2 (~> 2.0.0.rc2)
|
76
|
+
origin (2.2.0)
|
77
|
+
pry (0.10.3)
|
78
|
+
coderay (~> 1.1.0)
|
79
|
+
method_source (~> 0.8.1)
|
80
|
+
slop (~> 3.4)
|
81
|
+
rack (1.6.4)
|
82
|
+
rack-test (0.6.3)
|
83
|
+
rack (>= 1.0)
|
84
|
+
rails (4.2.5.1)
|
85
|
+
actionmailer (= 4.2.5.1)
|
86
|
+
actionpack (= 4.2.5.1)
|
87
|
+
actionview (= 4.2.5.1)
|
88
|
+
activejob (= 4.2.5.1)
|
89
|
+
activemodel (= 4.2.5.1)
|
90
|
+
activerecord (= 4.2.5.1)
|
91
|
+
activesupport (= 4.2.5.1)
|
92
|
+
bundler (>= 1.3.0, < 2.0)
|
93
|
+
railties (= 4.2.5.1)
|
94
|
+
sprockets-rails
|
95
|
+
rails-deprecated_sanitizer (1.0.3)
|
96
|
+
activesupport (>= 4.2.0.alpha)
|
97
|
+
rails-dom-testing (1.0.7)
|
98
|
+
activesupport (>= 4.2.0.beta, < 5.0)
|
99
|
+
nokogiri (~> 1.6.0)
|
100
|
+
rails-deprecated_sanitizer (>= 1.0.1)
|
101
|
+
rails-html-sanitizer (1.0.3)
|
102
|
+
loofah (~> 2.0)
|
103
|
+
railties (4.2.5.1)
|
104
|
+
actionpack (= 4.2.5.1)
|
105
|
+
activesupport (= 4.2.5.1)
|
106
|
+
rake (>= 0.8.7)
|
107
|
+
thor (>= 0.18.1, < 2.0)
|
108
|
+
rake (10.5.0)
|
109
|
+
slop (3.6.0)
|
110
|
+
sprockets (3.5.2)
|
111
|
+
concurrent-ruby (~> 1.0)
|
112
|
+
rack (> 1, < 3)
|
113
|
+
sprockets-rails (3.0.3)
|
114
|
+
actionpack (>= 4.0)
|
115
|
+
activesupport (>= 4.0)
|
116
|
+
sprockets (>= 3.0.0)
|
117
|
+
thor (0.19.1)
|
118
|
+
thread_safe (0.3.5)
|
119
|
+
tzinfo (1.2.2)
|
120
|
+
thread_safe (~> 0.1)
|
121
|
+
|
122
|
+
PLATFORMS
|
123
|
+
ruby
|
124
|
+
|
125
|
+
DEPENDENCIES
|
126
|
+
minitest
|
127
|
+
mongoid_rails_migrations!
|
128
|
+
pry
|
129
|
+
rake
|
130
|
+
|
131
|
+
BUNDLED WITH
|
132
|
+
1.10.6
|
data/README.rdoc
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
== RELEASE NOTES
|
2
|
+
* The most current release, 1.1.x, targets Mongoid >= 4.0.0 and Rails >= 4.2.0 .
|
3
|
+
* For Rails >= 3.2.0 and Mongoid >= 3.0.0, use version 1.0.0 .
|
4
|
+
* For Rails >= 3.0.0 (but < 3.2.0) and Mongoid >= 2.0.0, use version 0.0.14 .
|
5
|
+
|
6
|
+
== SYNOPSIS
|
7
|
+
* Data migrations for Mongoid.
|
8
|
+
|
9
|
+
== MIGRATE WHEN ...
|
10
|
+
* The migrating is good
|
11
|
+
|
12
|
+
== INSTALL
|
13
|
+
* gem install mongoid_rails_migrations
|
14
|
+
* In your Gemfile, include (after including mongoid):
|
15
|
+
gem "mongoid_rails_migrations", <version>
|
16
|
+
|
17
|
+
== FEATURES AND HOW TO USE
|
18
|
+
* generator:
|
19
|
+
* rails generate mongoid:migration your_migration_name_here
|
20
|
+
|
21
|
+
* migrations:
|
22
|
+
* db:migrate
|
23
|
+
* db:migrate:down
|
24
|
+
* db:migrate:up
|
25
|
+
* db:rollback
|
26
|
+
* db:migrate:redo
|
27
|
+
* db:migrate:reset
|
28
|
+
* db:reseed (handled by mongoid)
|
29
|
+
* db:version
|
30
|
+
|
31
|
+
== TESTING
|
32
|
+
```rake test:mongoid:migrations```
|
33
|
+
|
34
|
+
== CREDITS TO
|
35
|
+
* rails
|
36
|
+
* mongoid
|
37
|
+
* contributions from the community (git log)
|
38
|
+
|
39
|
+
Much of this gem simply modifies existing code from both projects.
|
40
|
+
With that out of the way, on to the license.
|
41
|
+
|
42
|
+
== LICENSE (MIT)
|
43
|
+
|
44
|
+
Copyright © 2013: Alan Da Costa
|
45
|
+
|
46
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'),
|
47
|
+
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish,
|
48
|
+
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
|
49
|
+
the following conditions:
|
50
|
+
|
51
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
52
|
+
|
53
|
+
The software is provided 'as is', without warranty of any kind, express or implied, including but not limited to the warranties of
|
54
|
+
merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any
|
55
|
+
claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the
|
56
|
+
software or the use or other dealings in the software.
|
data/Rakefile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'mongoid'
|
4
|
+
|
5
|
+
require 'mongoid_rails_migrations/version'
|
6
|
+
require 'mongoid_rails_migrations/models/data_migration'
|
7
|
+
require 'mongoid_rails_migrations/mongoid_ext/mongoid'
|
8
|
+
require 'mongoid_rails_migrations/mongoid_ext/railtie'
|
9
|
+
require 'mongoid_rails_migrations/active_record_ext/migrations'
|
@@ -0,0 +1,414 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mongoid #:nodoc
|
4
|
+
# Exception that can be raised to stop migrations from going backwards.
|
5
|
+
class IrreversibleMigration < RuntimeError
|
6
|
+
end
|
7
|
+
|
8
|
+
class DuplicateMigrationVersionError < RuntimeError#:nodoc:
|
9
|
+
def initialize(version)
|
10
|
+
super("Multiple migrations have the version number #{version}")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class DuplicateMigrationNameError < RuntimeError#:nodoc:
|
15
|
+
def initialize(name)
|
16
|
+
super("Multiple migrations have the name #{name}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class UnknownMigrationVersionError < RuntimeError#:nodoc:
|
21
|
+
def initialize(version)
|
22
|
+
super("No migration with version number #{version}")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class IllegalMigrationNameError < RuntimeError#:nodoc:
|
27
|
+
def initialize(name)
|
28
|
+
super("Illegal name for migration file: #{name}\n\t(only lower case letters, numbers, and '_' allowed)")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Data migrations can manage the modification of data. It's a solution to the common problem of modifying
|
33
|
+
# data between code revisions within a document oriented database.
|
34
|
+
#
|
35
|
+
# Example of simple migration for a system dependency:
|
36
|
+
#
|
37
|
+
# class AddBaselineSurveySchema < Mongoid::Migration
|
38
|
+
# def self.up
|
39
|
+
# SurveySchema.create(:label => 'Baseline Survey')
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# def self.down
|
43
|
+
# SurveySchema.where(:label => 'Baseline Survey').first.destroy
|
44
|
+
# end
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# == Timestamped Migrations
|
48
|
+
#
|
49
|
+
# By default, Rails generates migrations that look like:
|
50
|
+
#
|
51
|
+
# 20080717013526_your_migration_name.rb
|
52
|
+
#
|
53
|
+
# The prefix is a generation timestamp (in UTC).
|
54
|
+
#
|
55
|
+
# If you'd prefer to use numeric prefixes, you can turn timestamped migrations
|
56
|
+
# off by setting:
|
57
|
+
#
|
58
|
+
# Mongoid.config.timestamped_migrations = false
|
59
|
+
#
|
60
|
+
# In environment.rb.
|
61
|
+
#
|
62
|
+
class Migration
|
63
|
+
@@verbose = true
|
64
|
+
cattr_accessor :verbose
|
65
|
+
|
66
|
+
class << self
|
67
|
+
def up_with_benchmarks #:nodoc:
|
68
|
+
migrate(:up)
|
69
|
+
end
|
70
|
+
|
71
|
+
def down_with_benchmarks #:nodoc:
|
72
|
+
migrate(:down)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Execute this migration in the named direction
|
76
|
+
def migrate(direction)
|
77
|
+
return unless respond_to?(direction)
|
78
|
+
|
79
|
+
case direction
|
80
|
+
when :up then announce "migrating"
|
81
|
+
when :down then announce "reverting"
|
82
|
+
end
|
83
|
+
|
84
|
+
result = nil
|
85
|
+
time = Benchmark.measure { result = send("#{direction}_without_benchmarks") }
|
86
|
+
|
87
|
+
case direction
|
88
|
+
when :up then announce "migrated (%.4fs)" % time.real; write
|
89
|
+
when :down then announce "reverted (%.4fs)" % time.real; write
|
90
|
+
end
|
91
|
+
|
92
|
+
result
|
93
|
+
end
|
94
|
+
|
95
|
+
# Because the method added may do an alias_method, it can be invoked
|
96
|
+
# recursively. We use @ignore_new_methods as a guard to indicate whether
|
97
|
+
# it is safe for the call to proceed.
|
98
|
+
def singleton_method_added(sym) #:nodoc:
|
99
|
+
return if defined?(@ignore_new_methods) && @ignore_new_methods
|
100
|
+
|
101
|
+
begin
|
102
|
+
@ignore_new_methods = true
|
103
|
+
|
104
|
+
case sym
|
105
|
+
when :up, :down
|
106
|
+
singleton_class.send(:alias_method, "#{sym}_without_benchmarks".to_sym, sym)
|
107
|
+
singleton_class.send(:alias_method, sym, "#{sym}_with_benchmarks".to_sym)
|
108
|
+
end
|
109
|
+
ensure
|
110
|
+
@ignore_new_methods = false
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def write(text="")
|
115
|
+
puts(text) if verbose
|
116
|
+
end
|
117
|
+
|
118
|
+
def announce(message)
|
119
|
+
version = defined?(@version) ? @version : nil
|
120
|
+
|
121
|
+
text = "#{version} #{name}: #{message}"
|
122
|
+
length = [0, 75 - text.length].max
|
123
|
+
write "== %s %s" % [text, "=" * length]
|
124
|
+
end
|
125
|
+
|
126
|
+
def say(message, subitem=false)
|
127
|
+
write "#{subitem ? " ->" : "--"} #{message}"
|
128
|
+
end
|
129
|
+
|
130
|
+
def say_with_time(message)
|
131
|
+
say(message)
|
132
|
+
result = nil
|
133
|
+
time = Benchmark.measure { result = yield }
|
134
|
+
say "%.4fs" % time.real, :subitem
|
135
|
+
say("#{result} rows", :subitem) if result.is_a?(Integer)
|
136
|
+
result
|
137
|
+
end
|
138
|
+
|
139
|
+
def suppress_messages
|
140
|
+
save, self.verbose = verbose, false
|
141
|
+
yield
|
142
|
+
ensure
|
143
|
+
self.verbose = save
|
144
|
+
end
|
145
|
+
|
146
|
+
def connection
|
147
|
+
# ActiveRecord::Base.connection
|
148
|
+
if ::Mongoid.respond_to?(:default_client)
|
149
|
+
::Mongoid.default_client
|
150
|
+
else
|
151
|
+
::Mongoid.default_session
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def method_missing(method, *arguments, &block)
|
156
|
+
arg_list = arguments.map(&:inspect) * ', '
|
157
|
+
|
158
|
+
say_with_time "#{method}(#{arg_list})" do
|
159
|
+
# unless arguments.empty? || method == :execute
|
160
|
+
# arguments[0] = Migrator.proper_table_name(arguments.first)
|
161
|
+
# end
|
162
|
+
connection.send(method, *arguments, &block)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
# MigrationProxy is used to defer loading of the actual migration classes
|
169
|
+
# until they are needed
|
170
|
+
class MigrationProxy
|
171
|
+
|
172
|
+
attr_accessor :name, :version, :filename
|
173
|
+
|
174
|
+
delegate :migrate, :announce, :write, :to=>:migration
|
175
|
+
|
176
|
+
private
|
177
|
+
|
178
|
+
def migration
|
179
|
+
@migration ||= load_migration
|
180
|
+
end
|
181
|
+
|
182
|
+
def load_migration
|
183
|
+
require(File.expand_path(filename))
|
184
|
+
name.constantize
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
class Migrator#:nodoc:
|
190
|
+
class << self
|
191
|
+
attr_writer :migrations_path
|
192
|
+
|
193
|
+
def migrate(migrations_path, target_version = nil)
|
194
|
+
case
|
195
|
+
when target_version.nil? then up(migrations_path, target_version)
|
196
|
+
when current_version > target_version then down(migrations_path, target_version)
|
197
|
+
else up(migrations_path, target_version)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def rollback(migrations_path, steps=1)
|
202
|
+
move(:down, migrations_path, steps)
|
203
|
+
end
|
204
|
+
|
205
|
+
def forward(migrations_path, steps=1)
|
206
|
+
move(:up, migrations_path, steps)
|
207
|
+
end
|
208
|
+
|
209
|
+
def up(migrations_path, target_version = nil)
|
210
|
+
self.new(:up, migrations_path, target_version).migrate
|
211
|
+
end
|
212
|
+
|
213
|
+
def down(migrations_path, target_version = nil)
|
214
|
+
self.new(:down, migrations_path, target_version).migrate
|
215
|
+
end
|
216
|
+
|
217
|
+
def run(direction, migrations_path, target_version)
|
218
|
+
self.new(direction, migrations_path, target_version).run
|
219
|
+
end
|
220
|
+
|
221
|
+
def migrations_path
|
222
|
+
@migrations_path ||= ['db/migrate']
|
223
|
+
end
|
224
|
+
|
225
|
+
# def schema_migrations_table_name
|
226
|
+
# # Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix
|
227
|
+
# 'data_migrations'
|
228
|
+
# end
|
229
|
+
|
230
|
+
def get_all_versions
|
231
|
+
# table = Arel::Table.new(schema_migrations_table_name)
|
232
|
+
# Base.connection.select_values(table.project(table['version']).to_sql).map(&:to_i).sort
|
233
|
+
DataMigration.all.map { |datamigration| datamigration.version.to_i }.sort
|
234
|
+
end
|
235
|
+
|
236
|
+
def current_version
|
237
|
+
# sm_table = schema_migrations_table_name
|
238
|
+
# if Base.connection.table_exists?(sm_table)
|
239
|
+
# get_all_versions.max || 0
|
240
|
+
# else
|
241
|
+
# 0
|
242
|
+
# end
|
243
|
+
get_all_versions.max || 0
|
244
|
+
end
|
245
|
+
|
246
|
+
def proper_table_name(name)
|
247
|
+
# Use the Active Record objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string
|
248
|
+
# name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}"
|
249
|
+
name
|
250
|
+
end
|
251
|
+
|
252
|
+
private
|
253
|
+
|
254
|
+
def move(direction, migrations_path, steps)
|
255
|
+
migrator = self.new(direction, migrations_path)
|
256
|
+
start_index = migrator.migrations.index(migrator.current_migration)
|
257
|
+
|
258
|
+
if start_index
|
259
|
+
finish = migrator.migrations[start_index + steps]
|
260
|
+
version = finish ? finish.version : 0
|
261
|
+
send(direction, migrations_path, version)
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
def initialize(direction, migrations_path, target_version = nil)
|
267
|
+
# raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations?
|
268
|
+
# Base.connection.initialize_schema_migrations_table
|
269
|
+
@direction, @migrations_path, @target_version = direction, migrations_path, target_version
|
270
|
+
end
|
271
|
+
|
272
|
+
def current_version
|
273
|
+
migrated.last || 0
|
274
|
+
end
|
275
|
+
|
276
|
+
def current_migration
|
277
|
+
migrations.detect { |m| m.version == current_version }
|
278
|
+
end
|
279
|
+
|
280
|
+
def run
|
281
|
+
target = migrations.detect { |m| m.version == @target_version }
|
282
|
+
raise UnknownMigrationVersionError.new(@target_version) if target.nil?
|
283
|
+
unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i))
|
284
|
+
target.migrate(@direction)
|
285
|
+
record_version_state_after_migrating(target.version)
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
def migrate
|
290
|
+
current = migrations.detect { |m| m.version == current_version }
|
291
|
+
target = migrations.detect { |m| m.version == @target_version }
|
292
|
+
|
293
|
+
if target.nil? && !@target_version.nil? && @target_version > 0
|
294
|
+
raise UnknownMigrationVersionError.new(@target_version)
|
295
|
+
end
|
296
|
+
|
297
|
+
start = up? ? 0 : (migrations.index(current) || 0)
|
298
|
+
finish = migrations.index(target) || migrations.size - 1
|
299
|
+
runnable = migrations[start..finish]
|
300
|
+
|
301
|
+
# skip the last migration if we're headed down, but not ALL the way down
|
302
|
+
runnable.pop if down? && !target.nil?
|
303
|
+
|
304
|
+
runnable.each do |migration|
|
305
|
+
Rails.logger.info "Migrating to #{migration.name} (#{migration.version})" if Rails.logger
|
306
|
+
|
307
|
+
# On our way up, we skip migrating the ones we've already migrated
|
308
|
+
next if up? && migrated.include?(migration.version.to_i)
|
309
|
+
|
310
|
+
# On our way down, we skip reverting the ones we've never migrated
|
311
|
+
if down? && !migrated.include?(migration.version.to_i)
|
312
|
+
migration.announce 'never migrated, skipping'; migration.write
|
313
|
+
next
|
314
|
+
end
|
315
|
+
|
316
|
+
# begin
|
317
|
+
# ddl_transaction do
|
318
|
+
# migration.migrate(@direction)
|
319
|
+
# record_version_state_after_migrating(migration.version)
|
320
|
+
# end
|
321
|
+
# rescue => e
|
322
|
+
# canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : ""
|
323
|
+
# raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace
|
324
|
+
# end
|
325
|
+
begin
|
326
|
+
migration.migrate(@direction)
|
327
|
+
record_version_state_after_migrating(migration.version)
|
328
|
+
rescue => e
|
329
|
+
raise StandardError, "An error has occurred, #{migration.version} and all later migrations canceled:\n\n#{e}", e.backtrace
|
330
|
+
end
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
def migrations
|
335
|
+
@migrations ||= begin
|
336
|
+
files = Array(@migrations_path).inject([]) do |files, path|
|
337
|
+
files += Dir["#{path}/[0-9]*_*.rb"]
|
338
|
+
end
|
339
|
+
|
340
|
+
migrations = files.inject([]) do |klasses, file|
|
341
|
+
version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first
|
342
|
+
|
343
|
+
raise IllegalMigrationNameError.new(file) unless version
|
344
|
+
version = version.to_i
|
345
|
+
|
346
|
+
if klasses.detect { |m| m.version == version }
|
347
|
+
raise DuplicateMigrationVersionError.new(version)
|
348
|
+
end
|
349
|
+
|
350
|
+
if klasses.detect { |m| m.name == name.camelize }
|
351
|
+
raise DuplicateMigrationNameError.new(name.camelize)
|
352
|
+
end
|
353
|
+
|
354
|
+
migration = MigrationProxy.new
|
355
|
+
migration.name = name.camelize
|
356
|
+
migration.version = version
|
357
|
+
migration.filename = file
|
358
|
+
klasses << migration
|
359
|
+
end
|
360
|
+
|
361
|
+
migrations = migrations.sort_by(&:version)
|
362
|
+
down? ? migrations.reverse : migrations
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
def pending_migrations
|
367
|
+
already_migrated = migrated
|
368
|
+
migrations.reject { |m| already_migrated.include?(m.version.to_i) }
|
369
|
+
end
|
370
|
+
|
371
|
+
def migrated
|
372
|
+
@migrated_versions ||= self.class.get_all_versions
|
373
|
+
end
|
374
|
+
|
375
|
+
private
|
376
|
+
def record_version_state_after_migrating(version)
|
377
|
+
# table = Arel::Table.new(self.class.schema_migrations_table_name)
|
378
|
+
|
379
|
+
@migrated_versions ||= []
|
380
|
+
# if down?
|
381
|
+
# @migrated_versions.delete(version)
|
382
|
+
# table.where(table["version"].eq(version.to_s)).delete
|
383
|
+
# else
|
384
|
+
# @migrated_versions.push(version).sort!
|
385
|
+
# table.insert table["version"] => version.to_s
|
386
|
+
# end
|
387
|
+
if down?
|
388
|
+
@migrated_versions.delete(version)
|
389
|
+
DataMigration.where(:version => version.to_s).first.destroy
|
390
|
+
else
|
391
|
+
@migrated_versions.push(version).sort!
|
392
|
+
DataMigration.create(:version => version.to_s)
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
def up?
|
397
|
+
@direction == :up
|
398
|
+
end
|
399
|
+
|
400
|
+
def down?
|
401
|
+
@direction == :down
|
402
|
+
end
|
403
|
+
|
404
|
+
# Wrap the migration in a transaction only if supported by the adapter.
|
405
|
+
def ddl_transaction(&block)
|
406
|
+
# if Base.connection.supports_ddl_transactions?
|
407
|
+
# Base.transaction { block.call }
|
408
|
+
# else
|
409
|
+
# block.call
|
410
|
+
# end
|
411
|
+
block.call
|
412
|
+
end
|
413
|
+
end
|
414
|
+
end
|