Class: DBMigrator

Inherits:
Object
  • Object
show all
Defined in:
common/db/db_migrator.rb

Defined Under Namespace

Classes: ContainerMigrationError

Constant Summary collapse

MIGRATIONS_DIR =
File.join(File.dirname(__FILE__), "migrations")
PLUGIN_MIGRATIONS =
[]
PLUGIN_MIGRATION_DIRS =
{}
CONTAINER_MIGRATION_NUMBER =
60

Class Method Summary collapse

Class Method Details

.fail_if_managed_container_migration_needed!(db) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'common/db/db_migrator.rb', line 251

def self.fail_if_managed_container_migration_needed!(db)
    # If brand new install with empty database, need to check
    # for tables existence before determining the current version number
    if db.tables.empty?
      current_version = 0
    else
      current_version = db[:schema_info].first[:version]
    end

    if current_version && current_version > 0 && current_version < CONTAINER_MIGRATION_NUMBER
      $stderr.puts <<~EOM

        =======================================================================
        Important migration issue
        =======================================================================

        Hello!

        It appears that you are upgrading ArchivesSpace from version 1.4.2 or prior.  To
        complete this upgrade, there are some additional steps to follow.

        The 1.5 series of ArchivesSpace introduced a new data model for containers,
        along with a compatibility layer to provide a seamless transition between the
        old and new container models.  In ArchivesSpace version 2.1, this compatibility
        layer was removed in the interest of long-term maintainability and system
        performance.

        To upgrade your ArchivesSpace installation, you will first need to upgrade to
        version 2.0.1.  This will upgrade your containers to the new model and clear the
        path for future upgrades.  Once you have done this, you can upgrade to the
        latest ArchivesSpace version as normal.

        For more information on upgrading to ArchivesSpace 2.0.1, please see the upgrade
        guide:

          https://archivesspace.github.io/tech-docs/administration/upgrading.html

        The upgrade guide for version 1.5.0 also contains specific instructions for
        the container upgrade that you will be performing, and the steps in this guide
        apply equally to version 2.0.1.  You can find that guide here:

          https://github.com/archivesspace/archivesspace/blob/master/UPGRADING_1.5.0.md

        =======================================================================

      EOM

      raise ContainerMigrationError.new
    end
  end


  def self.nuke_database(db)
    $db_type = db.database_type

    if $db_type == :mysql
      db.run('SET foreign_key_checks = 0;')
      db.drop_table?(*db.tables)
      db.run('SET foreign_key_checks = 1;')
    else
      PLUGIN_MIGRATIONS.reverse.each { |plugin| Sequel::Migrator.run(db, PLUGIN_MIGRATION_DIRS[plugin],
        :table => "#{plugin}_schema_info", :target => 0) }
      Sequel::Migrator.run(db, MIGRATIONS_DIR, :target => 0)
    end
  end

  def self.needs_updating?(db)
    $db_type = db.database_type
    return true unless Sequel::Migrator.is_current?(db, MIGRATIONS_DIR)
    PLUGIN_MIGRATIONS.each { |plugin| return true unless Sequel::Migrator.is_current?(db, PLUGIN_MIGRATIONS_DIR[plugin],
                                                                                      :table => "#{plugin}_schema_info") }
    return false
  end

  def self.latest_migration_number(db)
    migration_numbers = Dir.entries(MIGRATIONS_DIR).map {|e|
      if e =~ Sequel::Migrator::MIGRATION_FILE_PATTERN
        # $1 is the migration number (e.g. '075')
        Integer($1, 10)
      end
    }.compact

    if migration_numbers.empty?
      0
    else
      migration_numbers.max
    end
  end
end

.order_plugins(plugins) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'common/db/db_migrator.rb', line 176

def self.order_plugins(plugins)
  ordered_plugin_dirs = ASUtils.order_plugins(plugins.map {|p| File.join(ASUtils.plugin_base_directory, p)})

  result = plugins.sort_by {|p|
    ordered_plugin_dirs.index(File.absolute_path(File.join(ASUtils.plugin_base_directory, p)))
  }

  result
end

.setup_database(db) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'common/db/db_migrator.rb', line 198

def self.setup_database(db)
  begin
    $db_type = db.database_type
    unless $db_type == :derby
      db.default_engine = 'InnoDB'
      db.default_charset = 'utf8'
    end

    fail_if_managed_container_migration_needed!(db)

    Sequel::Migrator.run(db, MIGRATIONS_DIR)
    PLUGIN_MIGRATIONS.each { |plugin| Sequel::Migrator.run(db, PLUGIN_MIGRATION_DIRS[plugin],
                                                           :table => "#{plugin}_schema_info") }
  rescue ContainerMigrationError
    raise $!
  rescue Exception => e
    $stderr.puts "\n    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n    !!                                                                                                !!\n    !!                                      Database migration error.                                 !!\n    !!                                  Your upgrade has encountered a problem.                       !!\n    !!                  You must resolve these issues before the database migration can complete.     !!\n    !!                                                                                                !!\n    !!                                                                                                !!\n    !!                                                Error:                                          !!\n"
    e.message.split("\n").each do |line|
      $stderr.puts "    !! #{line}"
    end
    $stderr.puts "    !!                                                                                                !!\n    !!                                                                                                !!\n    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n"

    raise e
  end
end