Class: Mongoid::Migrator
- Inherits:
-
Object
- Object
- Mongoid::Migrator
- Defined in:
- lib/mongoid_rails_migrations/active_record_ext/migrations.rb
Overview
:nodoc:
Class Method Summary collapse
- .current_version(mongoid_session = :default) ⇒ Object
- .down(migrations_path, target_version = nil, mongoid_session = :default) ⇒ Object
- .forward(migrations_path, steps = 1) ⇒ Object
-
.get_all_versions(mongoid_session = :default) ⇒ Object
def schema_migrations_table_name # Base.table_name_prefix + ‘schema_migrations’ + Base.table_name_suffix ‘data_migrations’ end.
- .migrate(migrations_path, target_version = nil, mongoid_session = :default) ⇒ Object
- .migrations_path ⇒ Object
- .proper_table_name(name) ⇒ Object
- .rollback(migrations_path, steps = 1) ⇒ Object
- .run(direction, migrations_path, target_version) ⇒ Object
- .up(migrations_path, target_version = nil, mongoid_session = :default) ⇒ Object
Instance Method Summary collapse
- #current_migration ⇒ Object
- #current_version ⇒ Object
- #get_all_versions ⇒ Object
-
#initialize(direction, migrations_path, target_version = nil, mongoid_session = :default) ⇒ Migrator
constructor
A new instance of Migrator.
- #migrate ⇒ Object
- #migrated ⇒ Object
- #migrations ⇒ Object
- #pending_migrations ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(direction, migrations_path, target_version = nil, mongoid_session = :default) ⇒ Migrator
Returns a new instance of Migrator.
251 252 253 254 255 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 251 def initialize(direction, migrations_path, target_version = nil, mongoid_session=:default) # raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations? # Base.connection.initialize_schema_migrations_table @direction, @migrations_path, @target_version, @mongoid_session = direction, migrations_path, target_version, mongoid_session end |
Class Method Details
.current_version(mongoid_session = :default) ⇒ Object
227 228 229 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 227 def current_version(mongoid_session=:default) get_all_versions(mongoid_session).max || 0 end |
.down(migrations_path, target_version = nil, mongoid_session = :default) ⇒ Object
206 207 208 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 206 def down(migrations_path, target_version = nil, mongoid_session=:default) self.new(:down, migrations_path, target_version, mongoid_session).migrate end |
.forward(migrations_path, steps = 1) ⇒ Object
198 199 200 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 198 def forward(migrations_path, steps=1) move(:up, migrations_path, steps) end |
.get_all_versions(mongoid_session = :default) ⇒ Object
def schema_migrations_table_name
# Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix
'data_migrations'
end
223 224 225 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 223 def get_all_versions(mongoid_session=:default) new(nil, nil, nil, mongoid_session).get_all_versions end |
.migrate(migrations_path, target_version = nil, mongoid_session = :default) ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 186 def migrate(migrations_path, target_version = nil, mongoid_session = :default) case when target_version.nil? then up(migrations_path, target_version, mongoid_session) when current_version > target_version then down(migrations_path, target_version, mongoid_session) else up(migrations_path, target_version, mongoid_session) end end |
.migrations_path ⇒ Object
214 215 216 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 214 def migrations_path 'db/migrate' end |
.proper_table_name(name) ⇒ Object
231 232 233 234 235 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 231 def proper_table_name(name) # Use the Active Record objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string # name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}" name end |
.rollback(migrations_path, steps = 1) ⇒ Object
194 195 196 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 194 def rollback(migrations_path, steps=1) move(:down, migrations_path, steps) end |
.run(direction, migrations_path, target_version) ⇒ Object
210 211 212 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 210 def run(direction, migrations_path, target_version) self.new(direction, migrations_path, target_version).run end |
.up(migrations_path, target_version = nil, mongoid_session = :default) ⇒ Object
202 203 204 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 202 def up(migrations_path, target_version = nil, mongoid_session=:default) self.new(:up, migrations_path, target_version, mongoid_session).migrate end |
Instance Method Details
#current_migration ⇒ Object
268 269 270 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 268 def current_migration migrations.detect { |m| m.version == current_version } end |
#current_version ⇒ Object
264 265 266 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 264 def current_version migrated.last || 0 end |
#get_all_versions ⇒ Object
257 258 259 260 261 262 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 257 def get_all_versions # table = Arel::Table.new(schema_migrations_table_name) # Base.connection.select_values(table.project(table['version']).to_sql).map(&:to_i).sort # migration_model.all.map {|datamigration| datamigration.version.to_i }.sort end |
#migrate ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 281 def migrate current = migrations.detect { |m| m.version == current_version } target = migrations.detect { |m| m.version == @target_version } if target.nil? && !@target_version.nil? && @target_version > 0 raise UnknownMigrationVersionError.new(@target_version) end start = up? ? 0 : (migrations.index(current) || 0) finish = migrations.index(target) || migrations.size - 1 runnable = migrations[start..finish] # skip the last migration if we're headed down, but not ALL the way down runnable.pop if down? && !target.nil? runnable.each do |migration| Rails.logger.info "Migrating to #{migration.name} (#{migration.version})" if Rails.logger # On our way up, we skip migrating the ones we've already migrated next if up? && migrated.include?(migration.version.to_i) # On our way down, we skip reverting the ones we've never migrated if down? && !migrated.include?(migration.version.to_i) migration.announce 'never migrated, skipping'; migration.write next end # begin # ddl_transaction do # migration.migrate(@direction) # record_version_state_after_migrating(migration.version) # end # rescue => e # canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : "" # raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace # end begin migration.migrate(@direction) (migration.version) rescue => e raise StandardError, "An error has occurred, #{migration.version} and all later migrations canceled:\n\n#{e}", e.backtrace end end end |
#migrated ⇒ Object
361 362 363 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 361 def migrated @migrated_versions ||= self.get_all_versions end |
#migrations ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 326 def migrations @migrations ||= begin files = Dir["#{@migrations_path}/[0-9]*_*.rb"] migrations = files.inject([]) do |klasses, file| version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first raise IllegalMigrationNameError.new(file) unless version version = version.to_i if klasses.detect { |m| m.version == version } raise DuplicateMigrationVersionError.new(version) end if klasses.detect { |m| m.name == name.camelize } raise DuplicateMigrationNameError.new(name.camelize) end migration = MigrationProxy.new migration.name = name.camelize migration.version = version migration.filename = file klasses << migration end migrations = migrations.sort_by(&:version) down? ? migrations.reverse : migrations end end |
#pending_migrations ⇒ Object
356 357 358 359 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 356 def pending_migrations already_migrated = migrated migrations.reject { |m| already_migrated.include?(m.version.to_i) } end |
#run ⇒ Object
272 273 274 275 276 277 278 279 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 272 def run target = migrations.detect { |m| m.version == @target_version } raise UnknownMigrationVersionError.new(@target_version) if target.nil? unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i)) target.migrate(@direction) (target.version) end end |