Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

column rename #1

Merged
merged 3 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion embargoes.install
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Drupal\Core\Database\Database;

/**
* @file
* Install, update and uninstall functions for the embargoes module.
Expand Down Expand Up @@ -30,7 +32,7 @@ function embargoes_schema() {
'type' => 'int',
'not null' => TRUE,
],
'user' => [
'uid' => [
'type' => 'int',
'not null' => TRUE,
],
Expand All @@ -44,3 +46,57 @@ function embargoes_schema() {
];
return $schema;
}

/**
* Update the 'user' column to be called 'uid'.
*/
function embargoes_update_8001(&$sandbox) {
$conn = Database::getConnection();

// Add the new field. Initially nullable; this will be changed once all fields
// have content.
$conn->schema()->addField('embargoes_log', 'uid', [
qadan marked this conversation as resolved.
Show resolved Hide resolved
'type' => 'int',
]);

// Migrate current data.
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current'] = 0;
$sandbox['total'] = $conn
->select('embargoes_log', 'el')
->countQuery()
->execute()
->fetchField();
if (empty($sandbox['total'])) {
$sandbox['#finished'] = 1;
$conn->schema()->changeField('embargoes_log', 'uid', 'uid', ['not null' => TRUE]);
$conn->schema()->dropField('embargoes_log', 'user');
return t('No logs to update');
}
}
$logs = $conn
->select('embargoes_log', 'el')
->fields('el', ['id', 'user'])
->condition('id', $sandbox['current'], '>')
->range(0, 20)
->orderBy('id', 'ASC')
->execute();
foreach ($logs as $log) {
$conn
->update('embargoes_log')
->fields(['uid' => $log->user])
->condition('id', $log->id)
->execute();
$sandbox['#message'] = t('Updated log ID: %id', ['%id' => $log->id]);
$sandbox['progress']++;
}

// Finish check.
$sandbox['#finished'] = empty($sandbox['total']) ? 1 : floor($sandbox['progress'] / $sandbox['max']);
if ($sandbox['#finished']) {
$conn->schema()->changeField('embargoes_log', 'uid', 'uid', ['not null' => TRUE]);
$conn->schema()->dropField('embargoes_log', 'user');
return t('Updated %count logs', ['%count' => $sandbox['progress']]);
}
}
4 changes: 2 additions & 2 deletions src/Controller/EmbargoesLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function showRenderedLog() {
foreach ($result as $record) {
$formatted_time = date('c', $record->time);
$node_title = \Drupal::entityTypeManager()->getStorage('node')->load($record->node)->get('title')->value;
$username = \Drupal\user\Entity\User::load($record->user)->getUsername();
$username = \Drupal\user\Entity\User::load($record->uid)->getUsername();
if ($record->action == "deleted") {
$embargo_formatted = Markup::create("<span style='text-decoration:line-through;'>{$record->embargo}</span>");
}
Expand All @@ -32,7 +32,7 @@ public function showRenderedLog() {
'time' => $formatted_time,
'action' => ucfirst($record->action),
'node' => Markup::create("<a href='/node/{$record->node}'>$node_title</a>"),
'user' => Markup::create("<a href='/user/{$record->user}'>$username</a>"),
'user' => Markup::create("<a href='/user/{$record->uid}'>$username</a>"),
];
array_push($formatted_log, $row);
}
Expand Down
2 changes: 1 addition & 1 deletion src/EmbargoesLogService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct() {
public function logEmbargoEvent($values) {
$time = time();
$database = \Drupal::database();
$result = $database->query("INSERT INTO {embargoes_log} (time, action, node, user, embargo) VALUES ('{$time}', '{$values['action']}', '{$values['node']}', '{$values['user']}','{$values['embargo_id']}');");
$result = $database->query("INSERT INTO {embargoes_log} (time, action, node, uid, embargo) VALUES ('{$time}', '{$values['action']}', '{$values['node']}', '{$values['user']}','{$values['embargo_id']}');");
adam-vessey marked this conversation as resolved.
Show resolved Hide resolved
return $result;
}

Expand Down