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

2.x Rename the entity base table #18

Open
wants to merge 14 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 31 additions & 0 deletions backerymails.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* Contains backerymails.install.
*/

use Symfony\Component\Yaml\Yaml;
use Drupal\Core\Url;
use Drupal\Core\Database\Database;

/**
* Implements hook_install().
Expand All @@ -20,3 +22,32 @@ function backerymails_install() {
$messenger = \Drupal::messenger();
$messenger->addMessage(t('Backery Mails settings are available under <a href="@administer-page">Administer > Site configuration > Backery Mails</a>', ['@administer-page' => $url->toString()]));
}

/**
* Implementions of hook_update_N().
*/

/**
* Update Backerymails entity base table in order to fix typo in table name.
*/
function backerymails_update_8001() {
$database = Database::getConnection();

// Delete all old backerymails entity definition.
$database->delete('key_value')
->condition('name', 'backerymails_entity.%', 'LIKE')
->execute();

// Re-install the new entity that will generate a new table.
$type_manager = \Drupal::entityTypeManager();
$type_manager->clearCachedDefinitions();
$entity_type = $type_manager->getDefinition('backerymails_entity');
\Drupal::entityDefinitionUpdateManager()->installEntityType($entity_type);

// Update the existing backerymails views with the new base-table.
$config_path = drupal_get_path('module', 'backerymails') . '/config/install/views.view.backerymails.yml';
$data = Yaml::parse(file_get_contents($config_path));
\Drupal::configFactory()->getEditable('views.view.backerymails')->setData($data)->save(TRUE);

return t('Backerymails base-table entity has been updated.');
}
41 changes: 41 additions & 0 deletions backerymails.post_update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @file
* Post update functions for Backerymails module.
*/

use Drupal\Core\Database\Database;

/**
* Migrate data from the old existing Backerymails table to the new one.
*
* @see backerymails_update_8001()
*/
WengerK marked this conversation as resolved.
Show resolved Hide resolved
function backerymails_post_update_8001_migrate_data(&$sandbox = NULL) {
$database = Database::getConnection();

if ($database->schema()->tableExists('backerymails_sended_mail') && $database->schema()->tableExists('backerymails_sent_mails')) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use early return that explain:

as post_update will always run independent of hook_update, but only once. Ensure we don't run migrate if the module was already installed postschema 8001 (we may tests that instead of tableexists)

$query = $database->select('backerymails_sended_mail', 'sent_mails')
->fields('sent_mails');

$backerymails_storage = \Drupal::service('entity_type.manager')->getStorage('backerymails_entity');
$tuples = $query->execute()->fetchAll();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard to tell if we need to Batch API here (and the use of $sandbox argument). Did you test it with production data?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try but on small project, your're right I will try on a huge database

foreach ($tuples as $tulpe) {
$backerymails_storage->create([
'module' => $tulpe->module,
'module_key' => $tulpe->module_key,
'mail_from' => $tulpe->mail_from,
'mail_to' => $tulpe->mail_to,
'mail_reply_to' => $tulpe->mail_reply_to,
'langcode' => $tulpe->langcode,
'subject' => $tulpe->subject,
'body__value' => $tulpe->body__value,
'body__format' => $tulpe->body__format,
'created' => $tulpe->created,
])->save();
}

$database->schema()->dropTable('backerymails_sended_mail');
}
}
21 changes: 10 additions & 11 deletions config/install/views.view.backerymails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ label: Backerymails
module: views
description: Backerymails
tag: backerymails
base_table: backerymails_sended_mail
base_table: backerymails_sent_mails
base_field: id
core: 8.x
display:
default:
display_plugin: default
Expand Down Expand Up @@ -158,7 +157,7 @@ display:
fields:
id:
id: id
table: backerymails_sended_mail
table: backerymails_sent_mails
field: id
relationship: none
group_type: group
Expand Down Expand Up @@ -224,7 +223,7 @@ display:
plugin_id: field
created:
id: created
table: backerymails_sended_mail
table: backerymails_sent_mails
field: created
relationship: none
group_type: group
Expand Down Expand Up @@ -291,7 +290,7 @@ display:
plugin_id: field
module:
id: module
table: backerymails_sended_mail
table: backerymails_sent_mails
field: module
relationship: none
group_type: group
Expand Down Expand Up @@ -356,7 +355,7 @@ display:
plugin_id: field
module_key:
id: module_key
table: backerymails_sended_mail
table: backerymails_sent_mails
field: module_key
relationship: none
group_type: group
Expand Down Expand Up @@ -421,7 +420,7 @@ display:
plugin_id: field
mail_from:
id: mail_from
table: backerymails_sended_mail
table: backerymails_sent_mails
field: mail_from
relationship: none
group_type: group
Expand Down Expand Up @@ -486,14 +485,14 @@ display:
plugin_id: field
mail_to:
id: mail_to
table: backerymails_sended_mail
table: backerymails_sent_mails
field: mail_to
entity_type: backerymails_entity
entity_field: mail_to
plugin_id: field
mail_reply_to:
id: mail_reply_to
table: backerymails_sended_mail
table: backerymails_sent_mails
field: mail_reply_to
relationship: none
group_type: group
Expand Down Expand Up @@ -558,7 +557,7 @@ display:
plugin_id: field
langcode:
id: langcode
table: backerymails_sended_mail
table: backerymails_sent_mails
field: langcode
relationship: none
group_type: group
Expand Down Expand Up @@ -623,7 +622,7 @@ display:
plugin_id: field
subject:
id: subject
table: backerymails_sended_mail
table: backerymails_sent_mails
field: subject
relationship: none
group_type: group
Expand Down
15 changes: 3 additions & 12 deletions src/Entity/BackerymailsEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
* },
* "access" = "Drupal\backerymails\BackerymailsEntityAccessControlHandler",
* },
* base_table = "backerymails_sended_mail",
* base_table = "backerymails_sent_mails",
* admin_permission = "administer backerymails",
* entity_keys = {
* "id" = "id",
* "label" = "subject",
* "langcode" = "langcode",
* },
* links = {
* "canonical" = "/admin/config/backerymails/mails/{backerymails_entity}",
Expand Down Expand Up @@ -134,12 +136,6 @@ public function getBody() {
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);

$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Entity ID'))
->setDescription(t('The entity ID for this menu link content entity.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);

$fields['module'] = BaseFieldDefinition::create('string')
->setLabel(t('Module'))
->setDescription(t('The module that send the mail.'));
Expand All @@ -160,11 +156,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Reply-to'))
->setDescription(t('The reply-to(s) of the mail.'));

$fields['langcode'] = BaseFieldDefinition::create('string')
->setLabel(t('Langcode'))
->setSetting('max_length', 12)
->setDescription(t('The langcode of the mail.'));

$fields['subject'] = BaseFieldDefinition::create('string')
->setLabel(t('Subject'))
->setDescription(t('The subject of the mail.'));
Expand Down
Binary file not shown.
Binary file not shown.
44 changes: 44 additions & 0 deletions tests/fixtures/update/drupal-8.backerymails-installed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

// @codingStandardsIgnoreFile

/**
* @file
* Contains database additions to drupal-8.8.0.bare.standard.php.gz for testing
* the upgrade paths of Backerymails module.
*/

use Drupal\Core\Database\Database;

$connection = Database::getConnection();

// Set the schema version.
$connection->merge('key_value')
->fields([
'value' => 'i:8000;',
'name' => 'backerymails',
'collection' => 'system.schema',
])
->condition('collection', 'system.schema')
->condition('name', 'backerymails')
->execute();

// Update core.extension.
$extensions = $connection->select('config')
->fields('config', ['data'])
->condition('collection', '')
->condition('name', 'core.extension')
->execute()
->fetchField();
$extensions = unserialize($extensions);
$extensions['module']['backerymails'] = 8000;
$connection->update('config')
->fields([
'data' => serialize($extensions),
'collection' => '',
'name' => 'core.extension',
])
->condition('collection', '')
->condition('name', 'core.extension')
->execute();

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Drupal\Tests\backerymails\Functional\Update;

use Drupal\backerymails\Entity\BackerymailsEntity;
use Drupal\Core\Database\Database;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;

/**
* Tests Backerymails entity base-table renamed and migrated.
*
* @group backerymails
* @group legacy
*
* @see backerymails_update_8001()
* @see backerymails_post_update_migrate_data()
*/
class RenameAndMigrateEntityTableUpdate8001Test extends UpdatePathTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = ['backerymails'];

/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
// This conditional allows tests to pass both before and after 8.8.x. The
// 8.4.0 fixtures were removed in 8.8.x.
// https://www.drupal.org/project/consumers/issues/3115996
// @todo: Remove this conditional after 8.7.x is no longer supported.

if (file_exists(DRUPAL_ROOT . '/core/modules/system/tests/fixtures/update/drupal-8.8.0.bare.standard.php.gz')) {
$this->databaseDumpFiles = [
DRUPAL_ROOT . '/core/modules/system/tests/fixtures/update/drupal-8.8.0.bare.standard.php.gz',
__DIR__ . '/../../../fixtures/update/drupal-8.backerymails-installed.php',
__DIR__ . '/../../../fixtures/update/drupal-8.backerymails-entity-typos-8001.php',
];
}
else {
$this->databaseDumpFiles = [
// @todo: Remove this fixture after 8.7 is no longer supported.
DRUPAL_ROOT . '/core/modules/system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
__DIR__ . '/../../../fixtures/update/drupal-8.backerymails-installed.php',
__DIR__ . '/../../../fixtures/update/drupal-8.backerymails-entity-typos-8001.php',
];
}
}

/**
* {@inheritdoc}
*/
protected function doSelectionTest() {
parent::doSelectionTest();
$this->assertSession()->responseContains('Update Backerymails entity base table in order to fix typo in table name.');
}

/**
* Tests backerymails_update_8001().
*
* Ensure every existing entries in the old table are migrated.
*
* @see backerymails_update_8001()
*/
public function testUpdate8001() {
$database = Database::getConnection();

$this->assertFalse($database->schema()->tableExists('backerymails_sent_mails'));
$this->assertTrue($database->schema()->tableExists('backerymails_sended_mail'));
$this->assertEquals(0, $database->query('SELECT count(*) FROM {backerymails_sended_mail}')->fetchField());

$database->insert('backerymails_sended_mail')
->fields([
'module',
'module_key',
'mail_from',
'mail_to',
'mail_reply_to',
'langcode',
'subject',
'body__value',
'body__format',
])
->values([
'backerymails.module',
'backerymails.module_key',
'backerymails.mail_from',
'backerymails.mail_to',
'backerymails.mail_reply_to',
'en',
'backerymails.subject',
'backerymails.body',
null,
])
->execute();
$this->assertEquals(1, $database->query('SELECT count(*) FROM {backerymails_sended_mail}')->fetchField());

$this->runUpdates();

$this->assertTrue($database->schema()->tableExists('backerymails_sent_mails'));
$this->assertFalse($database->schema()->tableExists('backerymails_sended_mail'));

$this->assertEquals(1, $database->query('SELECT count(*) FROM {backerymails_sent_mails}')->fetchField());
$backerymails = BackerymailsEntity::loadMultiple();
$this->assertCount(1, $backerymails);
}

}