-
Notifications
You must be signed in to change notification settings - Fork 1
2.x Rename the entity base table #18
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
base: 8.x-2.x
Are you sure you want to change the base?
Changes from 13 commits
28a74df
0254dce
0b2d73e
dc0297a
e407308
313cb86
5e70978
f06514c
c084713
9af8366
e0e36cc
18f156e
62b166a
63a883a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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() | ||
| */ | ||
| 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')) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.