mongoid_rails_migrations 1.3.0 → 1.6.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8775f1eec59ab5b9b48253d924c225ef8f4a381e25e70b8666f1f5439a8db3d7
4
- data.tar.gz: 896306d933de2c622e4ff3465801b300c16287a027edbb70c6881277c4643c41
3
+ metadata.gz: b1cdcaae2175713473f25909c60c736f37218da298585497c56f58a86274116c
4
+ data.tar.gz: 84f515ba8cae1c34cb15d28f5377c1ae1a0c11d2c16a12715094529e1825aebf
5
5
  SHA512:
6
- metadata.gz: bf0e7d1b3827d1c2af5c1c5f564a84a038dc750f7d1834198e7bf9b7e9eba9c6e5d60924c56e799eb23a71c3c972c19bd6f3796a2196d38b6c6f35646990dc51
7
- data.tar.gz: e4bad977cedcba39cc704c2ee23a765827a02834a6f45bbc65b52421a2b968f7fb174db82aeccd4b15e778c671a0232dfd65d21d185cb1348387470a04659eb4
6
+ metadata.gz: 6f96c8b356f100235344c478b384912600f43ab32258c2c7b8b7df481b45b1ca15a3f00fa6415b797169a0eb0999011455e5e5f37a877ed41b4493722bb18270
7
+ data.tar.gz: e7cc9cf652e98c709ce35077502793fcd1134f16629a273825612d9a7785f3bf6890a36b902349fd1bf90b58d2d6515d998618f2a698b5d2dec799f6d67ede7a
@@ -55,13 +55,13 @@ module Mongoid #:nodoc
55
55
  # If you'd prefer to use numeric prefixes, you can turn timestamped migrations
56
56
  # off by setting:
57
57
  #
58
- # Mongoid.config.timestamped_migrations = false
58
+ # Mongoid.configure.timestamped_migrations = false
59
59
  #
60
60
  # In environment.rb.
61
61
  #
62
62
  class Migration
63
63
  @@verbose = true
64
- cattr_accessor :verbose, :after_migrate
64
+ cattr_accessor :verbose, :after_migrate, :buffer_output
65
65
 
66
66
  class << self
67
67
  def up_with_benchmarks #:nodoc:
@@ -90,7 +90,8 @@ module Mongoid #:nodoc
90
90
  end
91
91
 
92
92
  begin
93
- @@after_migrate.call(@buffer_output, name, direction) if @@after_migrate
93
+ @@after_migrate.call(@@buffer_output, name, direction, false) if @@after_migrate
94
+ @@buffer_output = nil
94
95
  rescue => e
95
96
  say("Error in after_migrate hook: #{e}")
96
97
  end
@@ -102,7 +103,6 @@ module Mongoid #:nodoc
102
103
  # it is safe for the call to proceed.
103
104
  def singleton_method_added(sym) #:nodoc:
104
105
  return if defined?(@ignore_new_methods) && @ignore_new_methods
105
-
106
106
  begin
107
107
  @ignore_new_methods = true
108
108
 
@@ -117,8 +117,8 @@ module Mongoid #:nodoc
117
117
  end
118
118
 
119
119
  def write(text="")
120
- @buffer_output ||= ""
121
- @buffer_output += text + "\n"
120
+ @@buffer_output ||= ""
121
+ @@buffer_output += text + "\n"
122
122
  puts(text) if verbose
123
123
  end
124
124
 
@@ -151,22 +151,10 @@ module Mongoid #:nodoc
151
151
  end
152
152
 
153
153
  def connection
154
- # ActiveRecord::Base.connection
155
- if ::Mongoid.respond_to?(:default_client)
156
- ::Mongoid.default_client
154
+ if ENV['MONGOID_CLIENT_NAME']
155
+ Mongoid.client(ENV['MONGOID_CLIENT_NAME'])
157
156
  else
158
- ::Mongoid.default_session
159
- end
160
- end
161
-
162
- def method_missing(method, *arguments, &block)
163
- arg_list = arguments.map(&:inspect) * ', '
164
-
165
- say_with_time "#{method}(#{arg_list})" do
166
- # unless arguments.empty? || method == :execute
167
- # arguments[0] = Migrator.proper_table_name(arguments.first)
168
- # end
169
- connection.send(method, *arguments, &block)
157
+ Mongoid.default_client
170
158
  end
171
159
  end
172
160
  end
@@ -175,25 +163,25 @@ module Mongoid #:nodoc
175
163
  # MigrationProxy is used to defer loading of the actual migration classes
176
164
  # until they are needed
177
165
  class MigrationProxy
178
-
179
- attr_accessor :name, :version, :filename
166
+ attr_accessor :name, :version, :filename, :sharded
180
167
 
181
168
  delegate :migrate, :announce, :write, :to=>:migration
182
169
 
183
170
  private
184
171
 
185
- def migration
186
- @migration ||= load_migration
187
- end
188
-
189
- def load_migration
190
- require(File.expand_path(filename))
191
- name.constantize
192
- end
172
+ def migration
173
+ @migration ||= load_migration
174
+ end
193
175
 
176
+ def load_migration
177
+ require(File.expand_path(filename))
178
+ name.constantize
179
+ end
194
180
  end
195
181
 
196
182
  class Migrator#:nodoc:
183
+ delegate :with_mongoid_client, :to => "self.class"
184
+
197
185
  class << self
198
186
  attr_writer :migrations_path
199
187
 
@@ -215,7 +203,10 @@ module Mongoid #:nodoc
215
203
 
216
204
  def rollback_to(migrations_path, target_version)
217
205
  all_versions = get_all_versions
218
- rollback_to = all_versions.index(target_version.to_i) + 1
206
+ target_version_index = all_versions.index(target_version.to_i)
207
+ raise UnknownMigrationVersionError.new(target_version) if target_version_index.nil?
208
+
209
+ rollback_to = target_version_index + 1
219
210
  rollback_steps = all_versions.size - rollback_to
220
211
  rollback migrations_path, rollback_steps
221
212
  end
@@ -240,31 +231,22 @@ module Mongoid #:nodoc
240
231
  @migrations_path ||= ['db/migrate']
241
232
  end
242
233
 
243
- # def schema_migrations_table_name
244
- # # Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix
245
- # 'data_migrations'
246
- # end
247
-
248
234
  def get_all_versions
249
- # table = Arel::Table.new(schema_migrations_table_name)
250
- # Base.connection.select_values(table.project(table['version']).to_sql).map(&:to_i).sort
251
- DataMigration.all.map { |datamigration| datamigration.version.to_i }.sort
235
+ with_mongoid_client(ENV['MONGOID_CLIENT_NAME']) do
236
+ DataMigration.all.map { |datamigration| datamigration.version.to_i }.sort
237
+ end
252
238
  end
253
239
 
254
240
  def current_version
255
- # sm_table = schema_migrations_table_name
256
- # if Base.connection.table_exists?(sm_table)
257
- # get_all_versions.max || 0
258
- # else
259
- # 0
260
- # end
261
241
  get_all_versions.max || 0
262
242
  end
263
243
 
264
- def proper_table_name(name)
265
- # Use the Active Record objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string
266
- # name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}"
267
- name
244
+ def with_mongoid_client(mongoid_client_name, &block)
245
+ previous_mongoid_client_name = Mongoid::Threaded.client_override
246
+ Mongoid.override_client(mongoid_client_name)
247
+ block.call
248
+ ensure
249
+ Mongoid.override_client(previous_mongoid_client_name)
268
250
  end
269
251
 
270
252
  private
@@ -282,9 +264,12 @@ module Mongoid #:nodoc
282
264
  end
283
265
 
284
266
  def initialize(direction, migrations_path, target_version = nil)
285
- # raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations?
286
- # Base.connection.initialize_schema_migrations_table
287
267
  @direction, @migrations_path, @target_version = direction, migrations_path, target_version
268
+
269
+ @mongoid_client_name = ENV["MONGOID_CLIENT_NAME"]
270
+ if @mongoid_client_name && !Mongoid.clients.has_key?(@mongoid_client_name)
271
+ raise Mongoid::Errors::NoClientConfig.new(@mongoid_client_name)
272
+ end
288
273
  end
289
274
 
290
275
  def current_version
@@ -298,9 +283,12 @@ module Mongoid #:nodoc
298
283
  def run
299
284
  target = migrations.detect { |m| m.version == @target_version }
300
285
  raise UnknownMigrationVersionError.new(@target_version) if target.nil?
286
+
301
287
  unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i))
302
288
  target.migrate(@direction)
303
- record_version_state_after_migrating(target.version)
289
+ with_mongoid_client(@mongoid_client_name) do
290
+ record_version_state_after_migrating(target.version)
291
+ end
304
292
  end
305
293
  end
306
294
 
@@ -319,19 +307,19 @@ module Mongoid #:nodoc
319
307
  next
320
308
  end
321
309
 
322
- # begin
323
- # ddl_transaction do
324
- # migration.migrate(@direction)
325
- # record_version_state_after_migrating(migration.version)
326
- # end
327
- # rescue => e
328
- # canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : ""
329
- # raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace
330
- # end
331
310
  begin
332
311
  migration.migrate(@direction)
333
- record_version_state_after_migrating(migration.version)
312
+ with_mongoid_client(@mongoid_client_name) do
313
+ record_version_state_after_migrating(migration.version)
314
+ end
334
315
  rescue => e
316
+ output = Migration.buffer_output + "An error has occurred, #{migration.version} and all later migrations canceled:\n\n#{e}\n#{e.backtrace.join("\n")}"
317
+ begin
318
+ Migration.after_migrate.call(output, migration.name, @direction, true) if Migration.after_migrate
319
+ Migration.buffer_output = nil
320
+ rescue => error
321
+ puts("Error in after_migrate hook: #{error}")
322
+ end
335
323
  raise StandardError, "An error has occurred, #{migration.version} and all later migrations canceled:\n\n#{e}", e.backtrace
336
324
  end
337
325
  end
@@ -352,7 +340,7 @@ module Mongoid #:nodoc
352
340
  def migrations
353
341
  @migrations ||= begin
354
342
  files = Array(@migrations_path).inject([]) do |files, path|
355
- files += Dir["#{path}/[0-9]*_*.rb"]
343
+ files += Dir["#{path}/**/[0-9]*_*.rb"]
356
344
  end
357
345
 
358
346
  migrations = files.inject([]) do |klasses, file|
@@ -370,10 +358,15 @@ module Mongoid #:nodoc
370
358
  end
371
359
 
372
360
  migration = MigrationProxy.new
361
+ migration.sharded = file.match?(/\/shards\/#{version}_#{name}\.rb/)
373
362
  migration.name = name.camelize
374
363
  migration.version = version
375
364
  migration.filename = file
376
- klasses << migration
365
+
366
+ if (@mongoid_client_name && migration.sharded) || (!@mongoid_client_name && !migration.sharded)
367
+ klasses << migration
368
+ end
369
+ klasses
377
370
  end
378
371
 
379
372
  migrations = migrations.sort_by(&:version)
@@ -409,42 +402,24 @@ module Mongoid #:nodoc
409
402
  end
410
403
 
411
404
  private
412
- def record_version_state_after_migrating(version)
413
- # table = Arel::Table.new(self.class.schema_migrations_table_name)
414
-
415
- @migrated_versions ||= []
416
- # if down?
417
- # @migrated_versions.delete(version)
418
- # table.where(table["version"].eq(version.to_s)).delete
419
- # else
420
- # @migrated_versions.push(version).sort!
421
- # table.insert table["version"] => version.to_s
422
- # end
423
- if down?
424
- @migrated_versions.delete(version)
425
- DataMigration.where(:version => version.to_s).first.destroy
426
- else
427
- @migrated_versions.push(version).sort!
428
- DataMigration.create(:version => version.to_s)
429
- end
430
- end
431
405
 
432
- def up?
433
- @direction == :up
406
+ def record_version_state_after_migrating(version)
407
+ @migrated_versions ||= []
408
+ if down?
409
+ @migrated_versions.delete(version)
410
+ DataMigration.where(:version => version.to_s).destroy_all
411
+ else
412
+ @migrated_versions.push(version).sort!
413
+ DataMigration.find_or_create_by(:version => version.to_s)
434
414
  end
415
+ end
435
416
 
436
- def down?
437
- @direction == :down
438
- end
417
+ def up?
418
+ @direction == :up
419
+ end
439
420
 
440
- # Wrap the migration in a transaction only if supported by the adapter.
441
- def ddl_transaction(&block)
442
- # if Base.connection.supports_ddl_transactions?
443
- # Base.transaction { block.call }
444
- # else
445
- # block.call
446
- # end
447
- block.call
448
- end
421
+ def down?
422
+ @direction == :down
423
+ end
449
424
  end
450
425
  end
@@ -2,4 +2,4 @@ class DataMigration
2
2
  include Mongoid::Document
3
3
 
4
4
  field :version
5
- end
5
+ end
@@ -1,21 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
3
  # Specify whether or not to use timestamps for migration versions
4
- # NOTE: newer style is a module
5
- # Config.module_eval would work for both but still need to determine the type
6
- # that's why we do it the ug way.
7
- if Config.is_a? Class
8
- # older mongoid style; pre 2.0.0.rc.1
9
- Config.module_eval do
10
- cattr_accessor :timestamped_migrations
11
- class_variable_set(:@@timestamped_migrations, true) unless class_variable_get(:@@timestamped_migrations)
12
-
13
- def self.reset
14
- @@timestamped_migrations = true
15
- end
16
- end
17
- else # module
18
- # newer mongoid style; >= 2.0.0.rc.1
19
- Config.option :timestamped_migrations, :default => true
20
- end
21
- end
4
+ Config.option :timestamped_migrations, default: true
5
+ # Specify whether or not to use shards migrations as default type
6
+ Config.option :shards_migration_as_default, default: false
7
+ end
@@ -1,34 +1,12 @@
1
1
  namespace :db do
2
- unless Rake::Task.task_defined?("db:drop")
3
- desc 'Drops all the collections for the database for the current Rails.env'
4
- task :drop => :environment do
5
- Mongoid.master.collections.each {|col| col.drop_indexes && col.drop unless ['system.indexes', 'system.users'].include?(col.name) }
6
- end
7
- end
8
-
9
- unless Rake::Task.task_defined?("db:seed")
10
- # if another ORM has defined db:seed, don't run it twice.
11
- desc 'Load the seed data from db/seeds.rb'
12
- task :seed => :environment do
13
- seed_file = File.join(Rails.application.root, 'db', 'seeds.rb')
14
- load(seed_file) if File.exist?(seed_file)
15
- end
2
+ if Rake::Task.task_defined?("db:drop")
3
+ Rake::Task["db:drop"].clear
16
4
  end
17
5
 
18
- unless Rake::Task.task_defined?("db:setup")
19
- desc 'Create the database, and initialize with the seed data'
20
- task :setup => [ 'db:create', 'db:seed' ]
21
- end
22
-
23
- unless Rake::Task.task_defined?("db:reseed")
24
- desc 'Delete data and seed'
25
- task :reseed => [ 'db:drop', 'db:seed' ]
26
- end
27
-
28
- unless Rake::Task.task_defined?("db:create")
29
- task :create => :environment do
30
- # noop
31
- end
6
+ desc 'Drops the database for the current Mongoid client'
7
+ task :drop => :environment do
8
+ # Unlike Mongoid's default, this implementation supports the MONGOID_CLIENT_NAME override
9
+ Mongoid::Migration.connection.database.drop
32
10
  end
33
11
 
34
12
  desc 'Current database version'
@@ -43,7 +21,7 @@ namespace :db do
43
21
  end
44
22
 
45
23
  namespace :migrate do
46
- 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.'
24
+ 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.'
47
25
  task :redo => :environment do
48
26
  if ENV["VERSION"]
49
27
  Rake::Task["db:migrate:down"].invoke
@@ -90,16 +68,4 @@ namespace :db do
90
68
  raise "VERSION is required" unless version
91
69
  Mongoid::Migrator.rollback_to(Mongoid::Migrator.migrations_path, version)
92
70
  end
93
-
94
- namespace :schema do
95
- task :load do
96
- # noop
97
- end
98
- end
99
-
100
- namespace :test do
101
- task :prepare do
102
- # Stub out for MongoDB
103
- end
104
- end
105
71
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidRailsMigrations #:nodoc:
2
- VERSION = '1.3.0'
2
+ VERSION = '1.6.0'
3
3
  end
@@ -3,9 +3,16 @@ require 'rails/generators/mongoid/mongoid_generator'
3
3
  module Mongoid
4
4
  module Generators
5
5
  class MigrationGenerator < Base
6
+ class_option :shards, type: :boolean, optional: true, desc: "Create migration in shards subfolder"
6
7
 
7
8
  def create_migration_file
8
- migration_template "migration.rb", "db/migrate/#{file_name}.rb"
9
+ destination_folder = "db/migrate"
10
+ if options.fetch(:shards, Config.shards_migration_as_default)
11
+ destination_folder = "#{destination_folder}/shards"
12
+ FileUtils.mkdir_p("#{Rails.root}/#{destination_folder}")
13
+ end
14
+
15
+ migration_template "migration.rb", "#{destination_folder}/#{file_name}.rb"
9
16
  end
10
17
 
11
18
  protected
@@ -13,4 +20,4 @@ module Mongoid
13
20
 
14
21
  end
15
22
  end
16
- end
23
+ end
@@ -4,4 +4,4 @@ class <%= migration_class_name %> < Mongoid::Migration
4
4
 
5
5
  def self.down
6
6
  end
7
- end
7
+ end
@@ -5,8 +5,8 @@ require 'mongoid_rails_migrations/version'
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'mongoid_rails_migrations'
7
7
  spec.version = MongoidRailsMigrations::VERSION
8
- spec.authors = ['Alan Da Costa']
9
- spec.email = ['alandacosta@gmail.com']
8
+ spec.authors = ['Alan Da Costa', 'Adrien Rey-Jarthon']
9
+ spec.email = ['alandacosta@gmail.com', 'jobs@adrienjarthon.com']
10
10
 
11
11
  spec.summary = 'Data migrations for Mongoid.'
12
12
  spec.description = 'Data migrations for Mongoid in Active Record style, minus column input.'
@@ -19,7 +19,8 @@ Gem::Specification.new do |spec|
19
19
  rails_version = '>= 4.2.0'
20
20
 
21
21
  spec.add_runtime_dependency('bundler', '>= 1.0.0')
22
- spec.add_runtime_dependency('mongoid', '>= 4.0.0')
22
+ # 9.0.0 broke client override isolation: https://jira.mongodb.org/browse/MONGOID-5815
23
+ spec.add_runtime_dependency('mongoid', '>= 5.0.0', '< 9.0.0')
23
24
  spec.add_runtime_dependency('rails', rails_version)
24
25
  spec.add_runtime_dependency('railties', rails_version)
25
26
  spec.add_runtime_dependency('activesupport', rails_version)
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_rails_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Da Costa
8
- autorequire:
8
+ - Adrien Rey-Jarthon
9
+ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2020-12-17 00:00:00.000000000 Z
12
+ date: 2024-09-12 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
@@ -30,14 +31,20 @@ dependencies:
30
31
  requirements:
31
32
  - - ">="
32
33
  - !ruby/object:Gem::Version
33
- version: 4.0.0
34
+ version: 5.0.0
35
+ - - "<"
36
+ - !ruby/object:Gem::Version
37
+ version: 9.0.0
34
38
  type: :runtime
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
37
41
  requirements:
38
42
  - - ">="
39
43
  - !ruby/object:Gem::Version
40
- version: 4.0.0
44
+ version: 5.0.0
45
+ - - "<"
46
+ - !ruby/object:Gem::Version
47
+ version: 9.0.0
41
48
  - !ruby/object:Gem::Dependency
42
49
  name: rails
43
50
  requirement: !ruby/object:Gem::Requirement
@@ -111,6 +118,7 @@ dependencies:
111
118
  description: Data migrations for Mongoid in Active Record style, minus column input.
112
119
  email:
113
120
  - alandacosta@gmail.com
121
+ - jobs@adrienjarthon.com
114
122
  executables: []
115
123
  extensions: []
116
124
  extra_rdoc_files: []
@@ -130,7 +138,7 @@ homepage: http://github.com/adacosta/mongoid_rails_migrations
130
138
  licenses:
131
139
  - MIT
132
140
  metadata: {}
133
- post_install_message:
141
+ post_install_message:
134
142
  rdoc_options: []
135
143
  require_paths:
136
144
  - lib
@@ -145,8 +153,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
153
  - !ruby/object:Gem::Version
146
154
  version: '0'
147
155
  requirements: []
148
- rubygems_version: 3.1.2
149
- signing_key:
156
+ rubygems_version: 3.4.22
157
+ signing_key:
150
158
  specification_version: 4
151
159
  summary: Data migrations for Mongoid.
152
160
  test_files: []