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 4 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
15 changes: 8 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ language: php
sudo: false

php:
- 7.1
- 7.3

services:
- mysql

matrix:
include:
- name: PHP 7.1 & MySQL 5.7, D8.7
php: 7.1
- name: D8.7
env: DRUPAL_TI_RUNNERS="phpunit-core" DRUPAL_TI_CORE_BRANCH="8.7.x"
- name: PHP 7.1 & MySQL 5.7, D8.8
php: 7.1
- name: D8.8
env: DRUPAL_TI_RUNNERS="phpunit-core" DRUPAL_TI_CORE_BRANCH="8.8.x"

env:
Expand All @@ -42,7 +40,7 @@ env:
- DRUPAL_TI_ENVIRONMENT="drupal-8"

# The drush version to use, by default: drush/drush:8.0.*
# - DRUPAL_TI_DRUSH_VERSION="drush/drush:~9.0"
- DRUPAL_TI_DRUSH_VERSION="drush/drush:^10.1.1"

# Switch to 8.7.x versions instead of 8.1.x by default.
- DRUPAL_TI_CORE_BRANCH="8.7.x"
Expand Down Expand Up @@ -78,6 +76,7 @@ env:
- DRUPAL_TI_BEHAT_BROWSER="firefox"

# PHPUnit specific commandline arguments.
- DRUPAL_TI_PHPUNIT_VERSION="^7.0"
- DRUPAL_TI_PHPUNIT_ARGS=""
# Specifying the phpunit-core src/ directory is useful when e.g. a vendor/
# directory is present in the module directory, which phpunit would then
Expand Down Expand Up @@ -113,7 +112,9 @@ mysql:
before_install:
- composer self-update
- cd ./tests
- composer global require "lionsad/drupal_ti:dev-master#9f26af8"
- composer global config repositories.repo-name git https://github.com/wengerk/drupal_ti
- composer global require "lionsad/drupal_ti:dev-add-drupal-9-support"
# - composer global require "lionsad/drupal_ti:dev-master"
- drupal-ti before_install

install:
Expand Down
22 changes: 22 additions & 0 deletions backerymails.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Contains backerymails.install.
*/

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

/**
Expand All @@ -20,3 +21,24 @@ 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() {
$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.');
}
39 changes: 39 additions & 0 deletions backerymails.post_update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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.
*/
WengerK marked this conversation as resolved.
Show resolved Hide resolved
function backerymails_post_update_migrate_data(&$sandbox = NULL) {
WengerK marked this conversation as resolved.
Show resolved Hide resolved
$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');
$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) {
$database->insert('backerymails_sent_mails')
WengerK marked this conversation as resolved.
Show resolved Hide resolved
->fields([
'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,
])
->execute();
}
}

$database->schema()->dropTable('backerymails_sended_mail');
}
20 changes: 10 additions & 10 deletions config/install/views.view.backerymails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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:
Expand Down Expand Up @@ -158,7 +158,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 +224,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 +291,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 +356,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 +421,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 +486,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 +558,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 +623,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
2 changes: 1 addition & 1 deletion src/Entity/BackerymailsEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* },
* "access" = "Drupal\backerymails\BackerymailsEntityAccessControlHandler",
* },
* base_table = "backerymails_sended_mail",
* base_table = "backerymails_sent_mails",
* admin_permission = "administer backerymails",
* entity_keys = {
* "id" = "id",
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.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,97 @@
<?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->databaseDumpFiles = [
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'));

$backerymails = BackerymailsEntity::loadMultiple();
$this->assertCount(0, $backerymails);

$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);
}

}
Loading