You are here

function openlayers_update_7350 in Openlayers 7.3

Update to the 3.x version.

File

./openlayers.install, line 469
Openlayers module - installation procedure.

Code

function openlayers_update_7350() {

  // Enable new dependencies.
  $dependencies = array(
    'registry_autoload',
    'service_container',
  );
  foreach ($dependencies as $module) {
    if (!module_exists($module)) {
      $enabled = module_enable(array(
        $module,
      ));
      if (!$enabled) {
        throw new DrupalUpdateException('Could not enable ' . $module . ' module. Make sure it exists and try again.');
      }
    }
  }

  // Ensure update 7201 is done.
  if (db_table_exists('openlayers_map_presets')) {

    // Change table name.
    db_rename_table('openlayers_map_presets', 'openlayers_maps');

    // Set default map variable.
    variable_set('openlayers_default_map', variable_get('openlayers_default_preset', 'default'));
    variable_del('openlayers_default_preset');
  }
  $schema = openlayers_schema();

  // Create missing tables.
  foreach ($schema as $table => $table_schema) {
    if (!db_table_exists($table)) {
      db_create_table($table, $table_schema);
    }
  }

  // Add the machine name column - and deal with the title column.
  foreach ($schema as $table => $table_schema) {
    if (isset($table_schema['fields']['machine_name']) && db_field_exists($table, 'name') && !db_field_exists($table, 'machine_name')) {

      // Add field and index.
      db_add_field($table, 'machine_name', $table_schema['fields']['machine_name'], array(
        'machine_name' => array(
          'machine_name',
        ),
      ));
      db_query('UPDATE {' . $table . '} SET machine_name = LOWER(name);');
      db_drop_primary_key($table);
      db_add_primary_key($table, array(
        'machine_name',
      ));
      db_drop_index($table, 'name');

      // If there's a title field this is the new content for name.
      if (db_field_exists($table, 'title')) {
        db_query('UPDATE {' . $table . '} SET name = title;');
        db_drop_field($table, 'title');
      }
    }

    // If there's a data field but none is defined in the schema drop it.
    // @TODO This leads to data loss! Can we do anything better?
    if (!isset($table_schema['data']) && db_field_exists($table, 'data')) {
      db_drop_field($table, 'data');
    }
  }

  // Create missing columns.
  foreach ($schema as $table => $table_schema) {
    foreach (array_keys($table_schema['fields']) as $field) {
      if (!db_field_exists($table, $field)) {
        db_add_field($table, $field, $table_schema['fields'][$field]);
      }
    }
  }

  // Drop extra projection columns.
  // @TODO This ALSO leads to data loss! Can we do anything better?
  $old_projection_columns = array(
    'identifier',
    'authority',
    'code',
    'definition',
    'projectedextentleft',
    'projectedextentbottom',
    'projectedextentright',
    'projectedextenttop',
  );
  foreach ($old_projection_columns as $column) {
    if (db_field_exists('openlayers_projections', $column)) {
      db_drop_field('openlayers_projections', $column);
    }
  }

  // Truncate the styles table to remove old OL 2.x styles.
  // @TODO Yes... more data loss...
  db_query('TRUNCATE {openlayers_styles}');
}