Newer
Older

Angie Byron
committed
/**
* @file
* Install, update and uninstall functions for the user module.
*/
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;

Dries Buytaert
committed
* Implements hook_schema().
*/
function user_schema() {
$schema['users_data'] = [
'description' => 'Stores module data as key/value pairs per user.',
'fields' => [
'uid' => [
'description' => 'The {users}.uid this record affects.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'module' => [
'description' => 'The name of the module declaring the variable.',
'type' => 'varchar_ascii',
'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
'not null' => TRUE,
'default' => '',
],
'name' => [
'description' => 'The identifier of the data.',
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
],
'value' => [
'description' => 'The value.',
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
],
'serialized' => [
'description' => 'Whether value is serialized.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'default' => 0,
],
],
'primary key' => ['uid', 'module', 'name'],
'indexes' => [
'module' => ['module'],
'name' => ['name'],
],
'foreign keys' => [

catch
committed
'data_user' => [
'table' => 'users',
'columns' => [
'uid' => 'uid',
],
],
],
];
return $schema;
}

Dries Buytaert
committed
/**
* Implements hook_install().
*/
function user_install() {
$storage = \Drupal::entityTypeManager()->getStorage('user');

Dries Buytaert
committed
// Insert a row for the anonymous user.
$storage
->create([

Dries Buytaert
committed
'uid' => 0,
'status' => 0,
'name' => '',
])
->save();

Dries Buytaert
committed

Alex Pott
committed
// We need some placeholders here as name and mail are unique.

Angie Byron
committed
// This will be changed by the settings form in the installer.
$storage
->create([

Dries Buytaert
committed
'uid' => 1,
'name' => 'placeholder-for-uid-1',
'mail' => 'placeholder-for-uid-1',
'status' => TRUE,
])
->save();

Dries Buytaert
committed
}
/**
* Implements hook_update_last_removed().
*/
function user_update_last_removed() {
return 8100;
}
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Change the users table to use an serial uid field.
*/
function user_update_9301(&$sandbox) {
if (!\Drupal::entityTypeManager()->getStorage('user') instanceof SqlContentEntityStorage) {
return t('The user entity storage is not using an SQL storage, update skipped.');
}
$connection = \Drupal::database();
$connection->schema()->dropPrimaryKey('users');
if ($connection->databaseType() === 'mysql') {
$sql_mode = $connection->query("SELECT @@sql_mode;")->fetchField();
$connection->query("SET sql_mode = '$sql_mode,NO_AUTO_VALUE_ON_ZERO'");
}
$connection->schema()->changeField('users', 'uid', 'uid', ['type' => 'serial', 'not null' => TRUE], ['primary key' => ['uid']]);
if (isset($sql_mode)) {
$connection->query("SET sql_mode = '$sql_mode'");
}
// Update the last installed schema to reflect the change of field type.
$installed_storage_schema = \Drupal::keyValue('entity.storage_schema.sql');
$field_schema_data = $installed_storage_schema->get('user.field_schema_data.uid');
$field_schema_data['users']['fields']['uid']['type'] = 'serial';
$installed_storage_schema->set('user.field_schema_data.uid', $field_schema_data);
// The new PostgreSQL sequence for the uid field needs to start with the last
// used user ID + 1 and the sequence must be owned by uid field.
// @todo https://drupal.org/i/3028706 implement a generic fix.
if ($connection->driver() == 'pgsql') {
$maximum_uid = $connection->query('SELECT MAX([uid]) FROM {users}')->fetchField();
$seq = $connection->makeSequenceName('users', 'uid');
$connection->query("ALTER SEQUENCE " . $seq . " RESTART WITH " . ($maximum_uid + 1) . " OWNED BY {users}.uid");
}
}