mongoid_migrations 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e4c2f765c51f0bad595b39e311cbccc425c54aec
4
+ data.tar.gz: 7f3005b08f1ab8812f5ad861d268bc7df4bc908d
5
+ SHA512:
6
+ metadata.gz: 3838008127e118580ecb48a8cc41af114a1e8fed42aa9163252aabfd40caffeb2a8d7eae16ab617d51e0431a7f87b36d612065885a8f4e6f4b2d6c8982bfe09b
7
+ data.tar.gz: 1f126fb2313d9859a1725918634972b32fdd7bf11238a92ea3c9153a0b6b9a1d6eeab21a7aeb631b537ee998fb1a9ad9ad9275df913bd3fdded6e5a05d6de16e
@@ -0,0 +1,6 @@
1
+ .bundle
2
+ .DS_Store
3
+ .rvmrc
4
+ .idea
5
+ log/
6
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+ gemspec :name => 'mongoid_migrations'
3
+
4
+ group :test do
5
+ gem 'minitest'
6
+ gem 'pry'
7
+ end
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid_migrations (1.0.1)
5
+ activesupport (>= 4.2.0)
6
+ bundler (>= 1.0.0)
7
+ mongoid (>= 5.0.0)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ activemodel (5.1.1)
13
+ activesupport (= 5.1.1)
14
+ activesupport (5.1.1)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (~> 0.7)
17
+ minitest (~> 5.1)
18
+ tzinfo (~> 1.1)
19
+ bson (4.2.1)
20
+ coderay (1.1.1)
21
+ concurrent-ruby (1.0.5)
22
+ i18n (0.8.4)
23
+ method_source (0.8.2)
24
+ minitest (5.10.2)
25
+ mongo (2.4.2)
26
+ bson (>= 4.2.1, < 5.0.0)
27
+ mongoid (6.1.1)
28
+ activemodel (~> 5.0)
29
+ mongo (>= 2.4.1, < 3.0.0)
30
+ pry (0.10.4)
31
+ coderay (~> 1.1.0)
32
+ method_source (~> 0.8.1)
33
+ slop (~> 3.4)
34
+ rake (12.0.0)
35
+ slop (3.6.0)
36
+ thread_safe (0.3.6)
37
+ tzinfo (1.2.3)
38
+ thread_safe (~> 0.1)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ minitest
45
+ mongoid_migrations!
46
+ pry
47
+ rake
48
+
49
+ BUNDLED WITH
50
+ 1.14.6
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ $:.unshift(File.dirname(__FILE__))
4
+
5
+ task :default => ['test:mongoid:migrations']
6
+
7
+ namespace :test do
8
+ namespace :mongoid do
9
+ desc "Test mongoid migrations"
10
+ task :migrations do
11
+ load 'test/migration_test.rb'
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ require 'bundler/setup'
3
+ Bundler.require(:mongoid_migrations)
4
+
5
+ # Add base to path incase not included as a gem
6
+ $:.unshift(File.dirname(__FILE__)) unless
7
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
8
+
9
+ require 'mongoid_migrations/version'
10
+ require 'mongoid_migrations/models/data_migration'
11
+ require 'mongoid_migrations/active_record_ext/migrations'
@@ -0,0 +1,422 @@
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, 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_chain, sym, "benchmarks")
107
+ singleton_class.send(:alias_method, "#{sym}_without_benchmarks", sym)
108
+ singleton_class.send(:alias_method, sym, "#{sym}_with_benchmarks")
109
+ end
110
+ ensure
111
+ @ignore_new_methods = false
112
+ end
113
+ end
114
+
115
+ def write(text="")
116
+ puts(text) if verbose
117
+ end
118
+
119
+ def announce(message)
120
+ version = defined?(@version) ? @version : nil
121
+
122
+ text = "#{version} #{name}: #{message}"
123
+ length = [0, 75 - text.length].max
124
+ write "== %s %s" % [text, "=" * length]
125
+ end
126
+
127
+ def say(message, subitem=false)
128
+ write "#{subitem ? " ->" : "--"} #{message}"
129
+ end
130
+
131
+ def say_with_time(message)
132
+ say(message)
133
+ result = nil
134
+ time = Benchmark.measure { result = yield }
135
+ say "%.4fs" % time.real, :subitem
136
+ say("#{result} rows", :subitem) if result.is_a?(Integer)
137
+ result
138
+ end
139
+
140
+ def suppress_messages
141
+ save, self.verbose = verbose, false
142
+ yield
143
+ ensure
144
+ self.verbose = save
145
+ end
146
+
147
+ def connection
148
+ # ActiveRecord::Base.connection
149
+ if ::Mongoid.respond_to?(:default_client)
150
+ ::Mongoid.default_client
151
+ else
152
+ ::Mongoid.default_session
153
+ end
154
+ end
155
+
156
+ def method_missing(method, *arguments, &block)
157
+ arg_list = arguments.map(&:inspect) * ', '
158
+
159
+ say_with_time "#{method}(#{arg_list})" do
160
+ # unless arguments.empty? || method == :execute
161
+ # arguments[0] = Migrator.proper_table_name(arguments.first)
162
+ # end
163
+ connection.send(method, *arguments, &block)
164
+ end
165
+ end
166
+ end
167
+ end
168
+
169
+ # MigrationProxy is used to defer loading of the actual migration classes
170
+ # until they are needed
171
+ class MigrationProxy
172
+
173
+ attr_accessor :name, :version, :filename
174
+
175
+ delegate :migrate, :announce, :write, :to=>:migration
176
+
177
+ private
178
+
179
+ def migration
180
+ @migration ||= load_migration
181
+ end
182
+
183
+ def load_migration
184
+ require(File.expand_path(filename))
185
+ name.constantize
186
+ end
187
+
188
+ end
189
+
190
+ class Migrator#:nodoc:
191
+ class << self
192
+ def migrate(migrations_path, target_version = nil)
193
+ case
194
+ when target_version.nil? then up(migrations_path, target_version)
195
+ when current_version > target_version then down(migrations_path, target_version)
196
+ else up(migrations_path, target_version)
197
+ end
198
+ end
199
+
200
+ def rollback(migrations_path, steps=1)
201
+ move(:down, migrations_path, steps)
202
+ end
203
+
204
+ def forward(migrations_path, steps=1)
205
+ move(:up, migrations_path, steps)
206
+ end
207
+
208
+ def up(migrations_path, target_version = nil)
209
+ self.new(:up, migrations_path, target_version).migrate
210
+ end
211
+
212
+ def down(migrations_path, target_version = nil)
213
+ self.new(:down, migrations_path, target_version).migrate
214
+ end
215
+
216
+ def run(direction, migrations_path, target_version)
217
+ self.new(direction, migrations_path, target_version).run
218
+ end
219
+
220
+ def migrations_path
221
+ 'db/migrate'
222
+ end
223
+
224
+ def generate(migration_name)
225
+ file_name = "#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_#{migration_name.underscore}.rb"
226
+
227
+ File.open("#{migrations_path}/#{file_name}", 'w') do |f|
228
+ f << "class #{migration_name.camelize} < Mongoid::Migration\n"
229
+ f << " def self.up\n end\n\n"
230
+ f << " def self.down\n end\n"
231
+ f << "end\n"
232
+ end
233
+ end
234
+
235
+ # def schema_migrations_table_name
236
+ # # Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix
237
+ # 'data_migrations'
238
+ # end
239
+
240
+ def get_all_versions
241
+ # table = Arel::Table.new(schema_migrations_table_name)
242
+ # Base.connection.select_values(table.project(table['version']).to_sql).map(&:to_i).sort
243
+ DataMigration.all.map {|datamigration| datamigration.version.to_i }.sort
244
+ end
245
+
246
+ def current_version
247
+ # sm_table = schema_migrations_table_name
248
+ # if Base.connection.table_exists?(sm_table)
249
+ # get_all_versions.max || 0
250
+ # else
251
+ # 0
252
+ # end
253
+ get_all_versions.max || 0
254
+ end
255
+
256
+ def proper_table_name(name)
257
+ # Use the Active Record objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string
258
+ # name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}"
259
+ name
260
+ end
261
+
262
+ private
263
+
264
+ def move(direction, migrations_path, steps)
265
+ migrator = self.new(direction, migrations_path)
266
+ start_index = migrator.migrations.index(migrator.current_migration)
267
+
268
+ if start_index
269
+ finish = migrator.migrations[start_index + steps]
270
+ version = finish ? finish.version : 0
271
+ send(direction, migrations_path, version)
272
+ end
273
+ end
274
+ end
275
+
276
+ def initialize(direction, migrations_path, target_version = nil)
277
+ # raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations?
278
+ # Base.connection.initialize_schema_migrations_table
279
+ @direction, @migrations_path, @target_version = direction, migrations_path, target_version
280
+ end
281
+
282
+ def current_version
283
+ migrated.last || 0
284
+ end
285
+
286
+ def current_migration
287
+ migrations.detect { |m| m.version == current_version }
288
+ end
289
+
290
+ def run
291
+ target = migrations.detect { |m| m.version == @target_version }
292
+ raise UnknownMigrationVersionError.new(@target_version) if target.nil?
293
+ unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i))
294
+ target.migrate(@direction)
295
+ record_version_state_after_migrating(target.version)
296
+ end
297
+ end
298
+
299
+ def migrate
300
+ current = migrations.detect { |m| m.version == current_version }
301
+ target = migrations.detect { |m| m.version == @target_version }
302
+
303
+ if target.nil? && !@target_version.nil? && @target_version > 0
304
+ raise UnknownMigrationVersionError.new(@target_version)
305
+ end
306
+
307
+ start = up? ? 0 : (migrations.index(current) || 0)
308
+ finish = migrations.index(target) || migrations.size - 1
309
+ runnable = migrations[start..finish]
310
+
311
+ # skip the last migration if we're headed down, but not ALL the way down
312
+ runnable.pop if down? && !target.nil?
313
+
314
+ runnable.each do |migration|
315
+ # puts "Migrating to #{migration.name} (#{migration.version})"
316
+
317
+ # On our way up, we skip migrating the ones we've already migrated
318
+ next if up? && migrated.include?(migration.version.to_i)
319
+
320
+ # On our way down, we skip reverting the ones we've never migrated
321
+ if down? && !migrated.include?(migration.version.to_i)
322
+ migration.announce 'never migrated, skipping'; migration.write
323
+ next
324
+ end
325
+
326
+ # begin
327
+ # ddl_transaction do
328
+ # migration.migrate(@direction)
329
+ # record_version_state_after_migrating(migration.version)
330
+ # end
331
+ # rescue => e
332
+ # canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : ""
333
+ # raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace
334
+ # end
335
+ begin
336
+ migration.migrate(@direction)
337
+ record_version_state_after_migrating(migration.version)
338
+ rescue => e
339
+ raise StandardError, "An error has occurred, #{migration.version} and all later migrations canceled:\n\n#{e}", e.backtrace
340
+ end
341
+ end
342
+ end
343
+
344
+ def migrations
345
+ @migrations ||= begin
346
+ files = Dir["#{@migrations_path}/[0-9]*_*.rb"]
347
+
348
+ migrations = files.inject([]) do |klasses, file|
349
+ version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first
350
+
351
+ raise IllegalMigrationNameError.new(file) unless version
352
+ version = version.to_i
353
+
354
+ if klasses.detect { |m| m.version == version }
355
+ raise DuplicateMigrationVersionError.new(version)
356
+ end
357
+
358
+ if klasses.detect { |m| m.name == name.camelize }
359
+ raise DuplicateMigrationNameError.new(name.camelize)
360
+ end
361
+
362
+ migration = MigrationProxy.new
363
+ migration.name = name.camelize
364
+ migration.version = version
365
+ migration.filename = file
366
+ klasses << migration
367
+ end
368
+
369
+ migrations = migrations.sort_by(&:version)
370
+ down? ? migrations.reverse : migrations
371
+ end
372
+ end
373
+
374
+ def pending_migrations
375
+ already_migrated = migrated
376
+ migrations.reject { |m| already_migrated.include?(m.version.to_i) }
377
+ end
378
+
379
+ def migrated
380
+ @migrated_versions ||= self.class.get_all_versions
381
+ end
382
+
383
+ private
384
+ def record_version_state_after_migrating(version)
385
+ # table = Arel::Table.new(self.class.schema_migrations_table_name)
386
+
387
+ @migrated_versions ||= []
388
+ # if down?
389
+ # @migrated_versions.delete(version)
390
+ # table.where(table["version"].eq(version.to_s)).delete
391
+ # else
392
+ # @migrated_versions.push(version).sort!
393
+ # table.insert table["version"] => version.to_s
394
+ # end
395
+ if down?
396
+ @migrated_versions.delete(version)
397
+ DataMigration.where(:version => version.to_s).first.destroy
398
+ else
399
+ @migrated_versions.push(version).sort!
400
+ DataMigration.create(:version => version.to_s)
401
+ end
402
+ end
403
+
404
+ def up?
405
+ @direction == :up
406
+ end
407
+
408
+ def down?
409
+ @direction == :down
410
+ end
411
+
412
+ # Wrap the migration in a transaction only if supported by the adapter.
413
+ def ddl_transaction(&block)
414
+ # if Base.connection.supports_ddl_transactions?
415
+ # Base.transaction { block.call }
416
+ # else
417
+ # block.call
418
+ # end
419
+ block.call
420
+ end
421
+ end
422
+ end
@@ -0,0 +1,5 @@
1
+ class DataMigration
2
+ include Mongoid::Document
3
+
4
+ field :version
5
+ end
@@ -0,0 +1,102 @@
1
+ # require 'mongoid_migrations/tasks'
2
+ # will give you the resque tasks
3
+
4
+ namespace :db do
5
+ unless Rake::Task.task_defined?("db:drop")
6
+ desc 'Drops all the collections for the database for the current env'
7
+ task :drop do
8
+ Mongoid.master.collections.each {|col| col.drop_indexes && col.drop unless ['system.indexes', 'system.users'].include?(col.name) }
9
+ end
10
+ end
11
+
12
+ unless Rake::Task.task_defined?("db:seed")
13
+ # if another ORM has defined db:seed, don't run it twice.
14
+ desc 'Load the seed data from db/seeds.rb'
15
+ task :seed do
16
+ seed_file = File.join('db', 'seeds.rb')
17
+ load(seed_file) if File.exist?(seed_file)
18
+ end
19
+ end
20
+
21
+ unless Rake::Task.task_defined?("db:setup")
22
+ desc 'Create the database, and initialize with the seed data'
23
+ task :setup => [ 'db:create', 'db:seed' ]
24
+ end
25
+
26
+ unless Rake::Task.task_defined?("db:reseed")
27
+ desc 'Delete data and seed'
28
+ task :reseed => [ 'db:drop', 'db:seed' ]
29
+ end
30
+
31
+ unless Rake::Task.task_defined?("db:create")
32
+ task :create do
33
+ # noop
34
+ end
35
+ end
36
+
37
+ desc 'Current database version'
38
+ task :version do
39
+ puts Mongoid::Migrator.current_version.to_s
40
+ end
41
+
42
+ desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
43
+ task :migrate do
44
+ Mongoid::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
45
+ Mongoid::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
46
+ end
47
+
48
+ namespace :migrate do
49
+ desc 'Rollback the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x. Target specific version with VERSION=x.'
50
+ task :redo do
51
+ if ENV["VERSION"]
52
+ Rake::Task["db:migrate:down"].invoke
53
+ Rake::Task["db:migrate:up"].invoke
54
+ else
55
+ Rake::Task["db:rollback"].invoke
56
+ Rake::Task["db:migrate"].invoke
57
+ end
58
+ end
59
+
60
+ desc 'Resets your database using your migrations for the current environment'
61
+ # should db:create be changed to db:setup? It makes more sense wanting to seed
62
+ task :reset => ["db:drop", "db:create", "db:migrate"]
63
+
64
+ desc 'Runs the "up" for a given migration VERSION.'
65
+ task :up do
66
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
67
+ raise "VERSION is required" unless version
68
+ Mongoid::Migrator.run(:up, "db/migrate/", version)
69
+ end
70
+
71
+ desc 'Runs the "down" for a given migration VERSION.'
72
+ task :down do
73
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
74
+ raise "VERSION is required" unless version
75
+ Mongoid::Migrator.run(:down, "db/migrate/", version)
76
+ end
77
+
78
+ desc 'Create a migration file for given migration NAME'
79
+ task :create do
80
+ migration_name = ENV["NAME"] || 'migration'
81
+ Mongoid::Migrator.generate(migration_name)
82
+ end
83
+ end
84
+
85
+ desc 'Rolls the database back to the previous migration. Specify the number of steps with STEP=n'
86
+ task :rollback do
87
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
88
+ Mongoid::Migrator.rollback('db/migrate/', step)
89
+ end
90
+
91
+ namespace :schema do
92
+ task :load do
93
+ # noop
94
+ end
95
+ end
96
+
97
+ namespace :test do
98
+ task :prepare do
99
+ # Stub out for MongoDB
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module MongoidMigrations #:nodoc:
2
+ VERSION = '1.0.1'
3
+ end
@@ -0,0 +1,28 @@
1
+ require File.join(File.dirname(__FILE__), 'lib', 'mongoid_migrations', 'version')
2
+
3
+ Gem::Specification.new do |s|
4
+ s.platform = Gem::Platform::RUBY
5
+ s.name = 'mongoid_migrations'
6
+ s.version = MongoidMigrations::VERSION
7
+ s.summary = 'Data migrations for Rack applications with Mongoid'
8
+ s.license = 'MIT'
9
+ s.description = 'Migrations for Mongoid'
10
+
11
+ s.required_ruby_version = '>= 1.8.6'
12
+ s.required_rubygems_version = '>= 1.3.6'
13
+
14
+ s.author = 'Nimrod Popper'
15
+ s.email = 'nimrod@yotpo.com'
16
+ s.date = %q{2017-06-06}
17
+ s.homepage = 'https://github.com/YotpoLtd/mongoid_migrations'
18
+
19
+ s.require_paths = ['lib']
20
+ s.files = Dir['.gitignore', 'Gemfile', 'Gemfile.lock', 'Rakefile', 'README.rdoc', 'mongoid_migrations.gemspec', 'lib/**/*']
21
+ s.test_files = Dir['test/**/*']
22
+ s.has_rdoc = false
23
+
24
+ s.add_runtime_dependency('bundler', '>= 1.0.0')
25
+ s.add_runtime_dependency('mongoid', '>= 5.0.0')
26
+ s.add_runtime_dependency('activesupport', '>= 4.2.0')
27
+ s.add_development_dependency 'rake'
28
+ end
@@ -0,0 +1,8 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'mongoid_migrations')
2
+
3
+ Mongoid.configure.connect_to('mongoid_test')
4
+
5
+ # require all models
6
+ Dir[File.join(File.dirname(__FILE__), 'models', '*.rb')].each { |file| require file }
7
+
8
+ MIGRATIONS_ROOT = File.join(File.dirname(__FILE__), 'migrations')
@@ -0,0 +1,11 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'bundler/setup'
4
+ Bundler.require(:test)
5
+
6
+ require 'mongoid'
7
+ require 'config'
8
+ require 'minitest/autorun'
9
+ require 'rake'
10
+ require 'rake/testtask'
11
+ require 'rdoc/task'
@@ -0,0 +1,134 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class Mongoid::Migration
4
+ class <<self
5
+ attr_accessor :message_count
6
+
7
+ def puts(text="")
8
+ self.message_count ||= 0
9
+ self.message_count += 1
10
+ end
11
+ end
12
+ end
13
+
14
+ module Mongoid
15
+ class TestCase < ActiveSupport::TestCase #:nodoc:
16
+
17
+ def setup
18
+ Mongoid::Migration.verbose = true
19
+ # same as db:drop command in lib/mongoid_migrations/mongoid_ext/railties/database.rake
20
+ if Mongoid.respond_to?(:default_client)
21
+ Mongoid.default_client.database.drop
22
+ else
23
+ Mongoid.default_session.drop
24
+ end
25
+ end
26
+
27
+ def teardown; end
28
+
29
+ def test_drop_works
30
+ assert_equal 0, Mongoid::Migrator.current_version, "db:drop should take us down to version 0"
31
+ end
32
+
33
+ def test_finds_migrations
34
+ assert Mongoid::Migrator.new(:up, MIGRATIONS_ROOT + "/valid").migrations.size == 2
35
+ assert_equal 2, Mongoid::Migrator.new(:up, MIGRATIONS_ROOT + "/valid").pending_migrations.size
36
+ end
37
+
38
+ def test_migrator_current_version
39
+ Mongoid::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 20100513054656)
40
+ assert_equal(20100513054656, Mongoid::Migrator.current_version)
41
+ end
42
+
43
+ def test_migrator
44
+ assert SurveySchema.first.nil?, "All SurveySchemas should be clear before migration run"
45
+
46
+ Mongoid::Migrator.up(MIGRATIONS_ROOT + "/valid")
47
+
48
+ assert_equal 20100513063902, Mongoid::Migrator.current_version
49
+ assert !SurveySchema.first.nil?
50
+
51
+ Mongoid::Migrator.down(MIGRATIONS_ROOT + "/valid")
52
+ assert_equal 0, Mongoid::Migrator.current_version
53
+
54
+ assert SurveySchema.create(:label => 'Questionable Survey')
55
+ assert_equal 1, SurveySchema.all.size
56
+ end
57
+
58
+ def test_migrator_two_up_and_one_down
59
+ assert SurveySchema.where(:label => 'Baseline Survey').first.nil?
60
+ assert_equal 0, SurveySchema.all.size
61
+
62
+ Mongoid::Migrator.up(MIGRATIONS_ROOT + "/valid", 20100513054656)
63
+
64
+ assert !SurveySchema.where(:label => 'Baseline Survey').first.nil?
65
+ assert_equal 1, SurveySchema.all.size
66
+
67
+ assert SurveySchema.where(:label => 'Improvement Plan Survey').first.nil?
68
+
69
+ Mongoid::Migrator.up(MIGRATIONS_ROOT + "/valid", 20100513063902)
70
+ assert_equal 20100513063902, Mongoid::Migrator.current_version
71
+
72
+ assert !SurveySchema.where(:label => 'Improvement Plan Survey').first.nil?
73
+ assert_equal 2, SurveySchema.all.size
74
+
75
+ Mongoid::Migrator.down(MIGRATIONS_ROOT + "/valid", 20100513054656)
76
+ assert_equal 20100513054656, Mongoid::Migrator.current_version
77
+
78
+ assert SurveySchema.where(:label => 'Improvement Plan Survey').first.nil?
79
+ assert !SurveySchema.where(:label => 'Baseline Survey').first.nil?
80
+ assert_equal 1, SurveySchema.all.size
81
+ end
82
+
83
+ def test_finds_pending_migrations
84
+ Mongoid::Migrator.up(MIGRATIONS_ROOT + "/valid", 20100513054656)
85
+ pending_migrations = Mongoid::Migrator.new(:up, MIGRATIONS_ROOT + "/valid").pending_migrations
86
+
87
+ assert_equal 1, pending_migrations.size
88
+ assert_equal pending_migrations[0].version, 20100513063902
89
+ assert_equal pending_migrations[0].name, 'AddImprovementPlanSurveySchema'
90
+ end
91
+
92
+ def test_migrator_rollback
93
+ Mongoid::Migrator.migrate(MIGRATIONS_ROOT + "/valid")
94
+ assert_equal(20100513063902, Mongoid::Migrator.current_version)
95
+
96
+ Mongoid::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
97
+ assert_equal(20100513054656, Mongoid::Migrator.current_version)
98
+
99
+ Mongoid::Migrator.rollback(MIGRATIONS_ROOT + "/valid")
100
+ assert_equal(0, Mongoid::Migrator.current_version)
101
+ end
102
+
103
+ def test_migrator_forward
104
+ Mongoid::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 20100513054656)
105
+ assert_equal(20100513054656, Mongoid::Migrator.current_version)
106
+
107
+ Mongoid::Migrator.forward(MIGRATIONS_ROOT + "/valid", 20100513063902)
108
+ assert_equal(20100513063902, Mongoid::Migrator.current_version)
109
+ end
110
+
111
+ def test_migrator_with_duplicate_names
112
+ assert_raise(Mongoid::DuplicateMigrationNameError) do
113
+ Mongoid::Migrator.migrate(MIGRATIONS_ROOT + "/duplicate/names", nil)
114
+ end
115
+ end
116
+
117
+ def test_migrator_with_duplicate_versions
118
+ assert_raise(Mongoid::DuplicateMigrationVersionError) do
119
+ Mongoid::Migrator.migrate(MIGRATIONS_ROOT + "/duplicate/versions", nil)
120
+ end
121
+ end
122
+
123
+ def test_migrator_with_missing_version_numbers
124
+ assert_raise(Mongoid::UnknownMigrationVersionError) do
125
+ Mongoid::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 500)
126
+ end
127
+ end
128
+
129
+ def test_migration_returns_connection_without_error
130
+ Mongoid::Migration.connection
131
+ end
132
+
133
+ end
134
+ end
@@ -0,0 +1,9 @@
1
+ class AddDuplicateSurveySchema < Mongoid::Migration
2
+ def self.up
3
+ SurveySchema.create(:label => 'Duplicate Survey')
4
+ end
5
+
6
+ def self.down
7
+ SurveySchema.where(:label => 'Duplicate Survey').first.destroy
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class AddDuplicateSurveySchema < Mongoid::Migration
2
+ def self.up
3
+ SurveySchema.create(:label => 'Duplicate Survey')
4
+ end
5
+
6
+ def self.down
7
+ SurveySchema.where(:label => 'Duplicate Survey').first.destroy
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class AddDuplicateSurveySchema < Mongoid::Migration
2
+ def self.up
3
+ SurveySchema.create(:label => 'Duplicate Survey')
4
+ end
5
+
6
+ def self.down
7
+ SurveySchema.where(:label => 'Duplicate Survey').first.destroy
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class AddDuplicateSurveySchema < Mongoid::Migration
2
+ def self.up
3
+ SurveySchema.create(:label => 'Duplicate Survey')
4
+ end
5
+
6
+ def self.down
7
+ SurveySchema.where(:label => 'Duplicate Survey').first.destroy
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ class AddBaselineSurveySchema < Mongoid::Migration
2
+ def self.up
3
+ SurveySchema.create(:id => '4c47bf87f3395c339c000001',
4
+ :label => 'Baseline Survey')
5
+ end
6
+
7
+ def self.down
8
+ SurveySchema.where(:label => 'Baseline Survey').first.destroy
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ class AddImprovementPlanSurveySchema < Mongoid::Migration
2
+ def self.up
3
+ SurveySchema.create(:label => 'Improvement Plan Survey')
4
+ end
5
+
6
+ def self.down
7
+ SurveySchema.where(:label => 'Improvement Plan Survey').first.destroy
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ class SurveySchema
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+
5
+ field :label
6
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_migrations
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nimrod Popper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: mongoid
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 5.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 5.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 4.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 4.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Migrations for Mongoid
70
+ email: nimrod@yotpo.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - Gemfile
77
+ - Gemfile.lock
78
+ - Rakefile
79
+ - lib/mongoid_migrations.rb
80
+ - lib/mongoid_migrations/active_record_ext/migrations.rb
81
+ - lib/mongoid_migrations/models/data_migration.rb
82
+ - lib/mongoid_migrations/tasks.rb
83
+ - lib/mongoid_migrations/version.rb
84
+ - mongoid_migrations.gemspec
85
+ - test/config.rb
86
+ - test/helper.rb
87
+ - test/migration_test.rb
88
+ - test/migrations/duplicate/names/20100513073457_add_duplicate_survey_schema.rb
89
+ - test/migrations/duplicate/names/20100513073724_add_duplicate_survey_schema.rb
90
+ - test/migrations/duplicate/versions/20100513073457_add_another_duplicate_survey_schema.rb
91
+ - test/migrations/duplicate/versions/20100513073457_add_duplicate_survey_schema.rb
92
+ - test/migrations/valid/20100513054656_add_baseline_survey_schema.rb
93
+ - test/migrations/valid/20100513063902_add_improvement_plan_survey_schema.rb
94
+ - test/models/survey_schema.rb
95
+ homepage: https://github.com/YotpoLtd/mongoid_migrations
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: 1.8.6
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 1.3.6
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.8
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Data migrations for Rack applications with Mongoid
119
+ test_files:
120
+ - test/config.rb
121
+ - test/helper.rb
122
+ - test/migration_test.rb
123
+ - test/migrations/duplicate/names/20100513073457_add_duplicate_survey_schema.rb
124
+ - test/migrations/duplicate/names/20100513073724_add_duplicate_survey_schema.rb
125
+ - test/migrations/duplicate/versions/20100513073457_add_another_duplicate_survey_schema.rb
126
+ - test/migrations/duplicate/versions/20100513073457_add_duplicate_survey_schema.rb
127
+ - test/migrations/valid/20100513054656_add_baseline_survey_schema.rb
128
+ - test/migrations/valid/20100513063902_add_improvement_plan_survey_schema.rb
129
+ - test/models/survey_schema.rb