mongoid_rails_migrations 1.4.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42339e4043e1ab4647e09def69fbc4d2fc674486da3113a83d461696d50d1b0c
4
- data.tar.gz: ec3472e01a1071ced5db4da967c6c6b1627a4d9219b8abda84b19871514766fa
3
+ metadata.gz: b1cdcaae2175713473f25909c60c736f37218da298585497c56f58a86274116c
4
+ data.tar.gz: 84f515ba8cae1c34cb15d28f5377c1ae1a0c11d2c16a12715094529e1825aebf
5
5
  SHA512:
6
- metadata.gz: 84b7956dcac388376845c973fd0c1744c396fa38bff504257c361bfdda9b6c00b618000f418fb2d20f0c6ffae9a3a1a6649d0ec9728c3639bef583252ad1f620
7
- data.tar.gz: 54a713f8b7d3c3280e5b83a6233ffa8eecbef7254d20a55b08c705b85342511191fad0e9bd8fcf0162dd8e8ab3ff6b1fca296e58fbe9a779a436f06b1a18b61f
6
+ metadata.gz: 6f96c8b356f100235344c478b384912600f43ab32258c2c7b8b7df481b45b1ca15a3f00fa6415b797169a0eb0999011455e5e5f37a877ed41b4493722bb18270
7
+ data.tar.gz: e7cc9cf652e98c709ce35077502793fcd1134f16629a273825612d9a7785f3bf6890a36b902349fd1bf90b58d2d6515d998618f2a698b5d2dec799f6d67ede7a
@@ -55,7 +55,7 @@ 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
  #
@@ -103,7 +103,6 @@ module Mongoid #:nodoc
103
103
  # it is safe for the call to proceed.
104
104
  def singleton_method_added(sym) #:nodoc:
105
105
  return if defined?(@ignore_new_methods) && @ignore_new_methods
106
-
107
106
  begin
108
107
  @ignore_new_methods = true
109
108
 
@@ -152,22 +151,10 @@ module Mongoid #:nodoc
152
151
  end
153
152
 
154
153
  def connection
155
- # ActiveRecord::Base.connection
156
- if ::Mongoid.respond_to?(:default_client)
157
- ::Mongoid.default_client
154
+ if ENV['MONGOID_CLIENT_NAME']
155
+ Mongoid.client(ENV['MONGOID_CLIENT_NAME'])
158
156
  else
159
- ::Mongoid.default_session
160
- end
161
- end
162
-
163
- def method_missing(method, *arguments, &block)
164
- arg_list = arguments.map(&:inspect) * ', '
165
-
166
- say_with_time "#{method}(#{arg_list})" do
167
- # unless arguments.empty? || method == :execute
168
- # arguments[0] = Migrator.proper_table_name(arguments.first)
169
- # end
170
- connection.send(method, *arguments, &block)
157
+ Mongoid.default_client
171
158
  end
172
159
  end
173
160
  end
@@ -176,25 +163,25 @@ module Mongoid #:nodoc
176
163
  # MigrationProxy is used to defer loading of the actual migration classes
177
164
  # until they are needed
178
165
  class MigrationProxy
179
-
180
- attr_accessor :name, :version, :filename
166
+ attr_accessor :name, :version, :filename, :sharded
181
167
 
182
168
  delegate :migrate, :announce, :write, :to=>:migration
183
169
 
184
170
  private
185
171
 
186
- def migration
187
- @migration ||= load_migration
188
- end
189
-
190
- def load_migration
191
- require(File.expand_path(filename))
192
- name.constantize
193
- end
172
+ def migration
173
+ @migration ||= load_migration
174
+ end
194
175
 
176
+ def load_migration
177
+ require(File.expand_path(filename))
178
+ name.constantize
179
+ end
195
180
  end
196
181
 
197
182
  class Migrator#:nodoc:
183
+ delegate :with_mongoid_client, :to => "self.class"
184
+
198
185
  class << self
199
186
  attr_writer :migrations_path
200
187
 
@@ -216,7 +203,10 @@ module Mongoid #:nodoc
216
203
 
217
204
  def rollback_to(migrations_path, target_version)
218
205
  all_versions = get_all_versions
219
- 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
220
210
  rollback_steps = all_versions.size - rollback_to
221
211
  rollback migrations_path, rollback_steps
222
212
  end
@@ -241,31 +231,22 @@ module Mongoid #:nodoc
241
231
  @migrations_path ||= ['db/migrate']
242
232
  end
243
233
 
244
- # def schema_migrations_table_name
245
- # # Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix
246
- # 'data_migrations'
247
- # end
248
-
249
234
  def get_all_versions
250
- # table = Arel::Table.new(schema_migrations_table_name)
251
- # Base.connection.select_values(table.project(table['version']).to_sql).map(&:to_i).sort
252
- 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
253
238
  end
254
239
 
255
240
  def current_version
256
- # sm_table = schema_migrations_table_name
257
- # if Base.connection.table_exists?(sm_table)
258
- # get_all_versions.max || 0
259
- # else
260
- # 0
261
- # end
262
241
  get_all_versions.max || 0
263
242
  end
264
243
 
265
- def proper_table_name(name)
266
- # Use the Active Record objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string
267
- # name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}"
268
- 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)
269
250
  end
270
251
 
271
252
  private
@@ -283,9 +264,12 @@ module Mongoid #:nodoc
283
264
  end
284
265
 
285
266
  def initialize(direction, migrations_path, target_version = nil)
286
- # raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations?
287
- # Base.connection.initialize_schema_migrations_table
288
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
289
273
  end
290
274
 
291
275
  def current_version
@@ -299,9 +283,12 @@ module Mongoid #:nodoc
299
283
  def run
300
284
  target = migrations.detect { |m| m.version == @target_version }
301
285
  raise UnknownMigrationVersionError.new(@target_version) if target.nil?
286
+
302
287
  unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i))
303
288
  target.migrate(@direction)
304
- 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
305
292
  end
306
293
  end
307
294
 
@@ -320,18 +307,11 @@ module Mongoid #:nodoc
320
307
  next
321
308
  end
322
309
 
323
- # begin
324
- # ddl_transaction do
325
- # migration.migrate(@direction)
326
- # record_version_state_after_migrating(migration.version)
327
- # end
328
- # rescue => e
329
- # canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : ""
330
- # raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace
331
- # end
332
310
  begin
333
311
  migration.migrate(@direction)
334
- 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
335
315
  rescue => e
336
316
  output = Migration.buffer_output + "An error has occurred, #{migration.version} and all later migrations canceled:\n\n#{e}\n#{e.backtrace.join("\n")}"
337
317
  begin
@@ -360,7 +340,7 @@ module Mongoid #:nodoc
360
340
  def migrations
361
341
  @migrations ||= begin
362
342
  files = Array(@migrations_path).inject([]) do |files, path|
363
- files += Dir["#{path}/[0-9]*_*.rb"]
343
+ files += Dir["#{path}/**/[0-9]*_*.rb"]
364
344
  end
365
345
 
366
346
  migrations = files.inject([]) do |klasses, file|
@@ -378,10 +358,15 @@ module Mongoid #:nodoc
378
358
  end
379
359
 
380
360
  migration = MigrationProxy.new
361
+ migration.sharded = file.match?(/\/shards\/#{version}_#{name}\.rb/)
381
362
  migration.name = name.camelize
382
363
  migration.version = version
383
364
  migration.filename = file
384
- klasses << migration
365
+
366
+ if (@mongoid_client_name && migration.sharded) || (!@mongoid_client_name && !migration.sharded)
367
+ klasses << migration
368
+ end
369
+ klasses
385
370
  end
386
371
 
387
372
  migrations = migrations.sort_by(&:version)
@@ -417,42 +402,24 @@ module Mongoid #:nodoc
417
402
  end
418
403
 
419
404
  private
420
- def record_version_state_after_migrating(version)
421
- # table = Arel::Table.new(self.class.schema_migrations_table_name)
422
-
423
- @migrated_versions ||= []
424
- # if down?
425
- # @migrated_versions.delete(version)
426
- # table.where(table["version"].eq(version.to_s)).delete
427
- # else
428
- # @migrated_versions.push(version).sort!
429
- # table.insert table["version"] => version.to_s
430
- # end
431
- if down?
432
- @migrated_versions.delete(version)
433
- DataMigration.where(:version => version.to_s).first.destroy
434
- else
435
- @migrated_versions.push(version).sort!
436
- DataMigration.create(:version => version.to_s)
437
- end
438
- end
439
405
 
440
- def up?
441
- @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)
442
414
  end
415
+ end
443
416
 
444
- def down?
445
- @direction == :down
446
- end
417
+ def up?
418
+ @direction == :up
419
+ end
447
420
 
448
- # Wrap the migration in a transaction only if supported by the adapter.
449
- def ddl_transaction(&block)
450
- # if Base.connection.supports_ddl_transactions?
451
- # Base.transaction { block.call }
452
- # else
453
- # block.call
454
- # end
455
- block.call
456
- end
421
+ def down?
422
+ @direction == :down
423
+ end
457
424
  end
458
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.4.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.4.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: 2021-01-08 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: []