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
# 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 "\n        =======================================================================\n        Important migration issue\n        =======================================================================\n\n        Hello!\n\n        It appears that you are upgrading ArchivesSpace from version 1.4.2 or prior.  To\n        complete this upgrade, there are some additional steps to follow.\n\n        The 1.5 series of ArchivesSpace introduced a new data model for containers,\n        along with a compatibility layer to provide a seamless transition between the\n        old and new container models.  In ArchivesSpace version 2.1, this compatibility\n        layer was removed in the interest of long-term maintainability and system\n        performance.\n\n        To upgrade your ArchivesSpace installation, you will first need to upgrade to\n        version 2.0.1.  This will upgrade your containers to the new model and clear the\n        path for future upgrades.  Once you have done this, you can upgrade to the\n        latest ArchivesSpace version as normal.\n\n        For more information on upgrading to ArchivesSpace 2.0.1, please see the upgrade\n        guide:\n\n          https://archivesspace.github.io/tech-docs/administration/upgrading.html\n\n        The upgrade guide for version 1.5.0 also contains specific instructions for\n        the container upgrade that you will be performing, and the steps in this guide\n        apply equally to version 2.0.1.  You can find that guide here:\n\n          https://github.com/archivesspace/archivesspace/blob/master/UPGRADING_1.5.0.md\n\n        =======================================================================\n\n      EOM\n\n      raise ContainerMigrationError.new\n    end\n  end\n\n\n  def self.nuke_database(db)\n    $db_type = db.database_type\n\n    if $db_type == :mysql\n      db.run('SET foreign_key_checks = 0;')\n      db.drop_table?(*db.tables)\n      db.run('SET foreign_key_checks = 1;')\n    else\n      PLUGIN_MIGRATIONS.reverse.each { |plugin| Sequel::Migrator.run(db, PLUGIN_MIGRATION_DIRS[plugin],\n        :table => \"\#{plugin}_schema_info\", :target => 0) }\n      Sequel::Migrator.run(db, MIGRATIONS_DIR, :target => 0)\n    end\n  end\n\n  def self.needs_updating?(db)\n    $db_type = db.database_type\n    return true unless Sequel::Migrator.is_current?(db, MIGRATIONS_DIR)\n    PLUGIN_MIGRATIONS.each { |plugin| return true unless Sequel::Migrator.is_current?(db, PLUGIN_MIGRATIONS_DIR[plugin],\n                                                                                      :table => \"\#{plugin}_schema_info\") }\n    return false\n  end\n\n  def self.latest_migration_number(db)\n    migration_numbers = Dir.entries(MIGRATIONS_DIR).map {|e|\n      if e =~ Sequel::Migrator::MIGRATION_FILE_PATTERN\n        # $1 is the migration number (e.g. '075')\n        Integer($1, 10)\n      end\n    }.compact\n\n    if migration_numbers.empty?\n      0\n    else\n      migration_numbers.max\n    end\n  end\nend\n"

.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