Skip to content

Instantly share code, notes, and snippets.

@Berdir
Created April 30, 2015 09:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Berdir/3bfdb91e664e325004ba to your computer and use it in GitHub Desktop.
Save Berdir/3bfdb91e664e325004ba to your computer and use it in GitHub Desktop.
Update redirect source fields
/**
* Update the {redirect} table.
*/
function np8_update_update_8004() {
// Drop the title column.
if (db_field_exists('redirect', 'redirect_source__title')) {
db_drop_field('redirect', 'redirect_source__title');
}
// We assume there are no options stored and just rename it to query.
if (db_field_exists('redirect', 'redirect_source__uri')) {
db_change_field(
'redirect', 'redirect_source__uri', 'redirect_source__path', array(
'description' => 'The source path',
'type' => 'varchar',
'length' => 2048,
), [
'indexes' => array(
'redirect_source__path' => array(array('redirect_source__path', 50)),
)
]
);
}
// We assume there are no options stored and just rename it to query.
if (db_field_exists('redirect', 'redirect_source__options')) {
db_change_field(
'redirect', 'redirect_source__options', 'redirect_source__query', array(
'description' => 'Serialized array of path queries',
'type' => 'blob',
'size' => 'big',
'serialize' => TRUE,
)
);
}
// Prefix all redirect URI's.
db_update('redirect')
->condition('redirect_redirect__uri', 'internal:/%', 'NOT LIKE')
->expression('redirect_redirect__uri', "CONCAT('internal:/', redirect_redirect__uri)")
->execute();
// Update the last installed field definition and field schema.
/** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value_store */
\Drupal::entityManager()->clearCachedFieldDefinitions();
$key_value_store_definition = \Drupal::service('entity.definitions.installed');
$storage_definitions = $key_value_store_definition->get('redirect.field_storage_definitions');
$storage_definitions['redirect_source'] = $storage_definition = \Drupal::entityManager()->getFieldStorageDefinitions('redirect')['redirect_source'];
$key_value_store_definition->set('redirect.field_storage_definitions', $storage_definitions);
$key_value_store_schema = \Drupal::keyValue('entity.storage_schema.sql');
$schema = array(
'redirect' => array(
'fields' => array(
'redirect_source__path' => array(
'description' => 'The source path',
'type' => 'varchar',
'length' => 2048,
'not null' => FALSE,
),
'redirect_source__query' => array(
'description' => 'Serialized array of path queries',
'type' => 'blob',
'size' => 'big',
'serialize' => TRUE,
'not null' => FALSE,
),
),
'indexes' => array(
'redirect_field__redirect_source__path' => array(
0 => array('redirect_source__path', 50),
),
),
),
);
$key_value_store_schema->set('redirect.field_schema_data.redirect_source', $schema);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment