Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Dec 13, 2023
1 parent b37681c commit cec68f9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/db/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ abstract class Table
public const DRAFTS = '{{%drafts}}';
/** @since 4.5.0 */
public const ELEMENTACTIVITY = '{{%elementactivity}}';
/** @since 5.0.0 */
public const ELEMENTBULKOPS = '{{%elementbulkops}}';
public const ELEMENTS = '{{%elements}}';
/** @since 5.0.0 */
public const ELEMENTS_BULKOPS = '{{%elements_bulkops}}';
/** @since 5.0.0 */
public const ELEMENTS_OWNERS = '{{%elements_owners}}';
public const ELEMENTS_SITES = '{{%elements_sites}}';
public const RESOURCEPATHS = '{{%resourcepaths}}';
Expand Down
5 changes: 3 additions & 2 deletions src/elements/db/ElementQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ class ElementQuery extends Query implements ElementQueryInterface
* @var string|null The bulk element operation key that the resulting elements were involved in.
*
* @used-by ElementQuery::inBulkOp()
* @since 5.0.0
*/
public ?string $inBulkOp = null;

Expand Down Expand Up @@ -2871,8 +2872,8 @@ private function _applyInBulkOpParam(): void
{
if ($this->inBulkOp) {
$this->subQuery
->innerJoin(['elementbulkops' => Table::ELEMENTBULKOPS], '[[elementbulkops.elementId]] = [[elements.id]]')
->andWhere(['elementbulkops.key' => $this->inBulkOp]);
->innerJoin(['elements_bulkops' => Table::ELEMENTS_BULKOPS], '[[elements_bulkops.elementId]] = [[elements.id]]')
->andWhere(['elements_bulkops.key' => $this->inBulkOp]);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/elements/db/ElementQueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ public function search(mixed $value): static;
*
* @param string|null $value The property value
* @return static self reference
* @since 5.0.0
*/
public function inBulkOp(?string $value): static;

Expand Down
16 changes: 8 additions & 8 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,6 @@ public function createTables(): void
'timestamp' => $this->dateTime(),
'PRIMARY KEY([[elementId]], [[userId]], [[type]])',
]);
$this->createTable(Table::ELEMENTBULKOPS, [
'elementId' => $this->integer(),
'key' => $this->char(10)->notNull(),
'timestamp' => $this->dateTime()->notNull(),
'PRIMARY KEY([[elementId]], [[key]])',
]);
$this->createTable(Table::ELEMENTS, [
'id' => $this->primaryKey(),
'canonicalId' => $this->integer(),
Expand All @@ -329,6 +323,12 @@ public function createTables(): void
'deletedWithOwner' => $this->boolean()->null(),
'uid' => $this->uid(),
]);
$this->createTable(Table::ELEMENTS_BULKOPS, [
'elementId' => $this->integer(),
'key' => $this->char(10)->notNull(),
'timestamp' => $this->dateTime()->notNull(),
'PRIMARY KEY([[elementId]], [[key]])',
]);
$this->createTable(Table::ELEMENTS_OWNERS, [
'elementId' => $this->integer()->notNull(),
'ownerId' => $this->integer()->notNull(),
Expand Down Expand Up @@ -809,7 +809,6 @@ public function createIndexes(): void
$this->createIndex(null, Table::DRAFTS, ['creatorId', 'provisional'], false);
$this->createIndex(null, Table::DRAFTS, ['saved'], false);
$this->createIndex(null, Table::ELEMENTACTIVITY, ['elementId', 'timestamp', 'userId'], false);
$this->createIndex(null, Table::ELEMENTBULKOPS, ['timestamp'], false);
$this->createIndex(null, Table::ELEMENTS, ['dateDeleted'], false);
$this->createIndex(null, Table::ELEMENTS, ['fieldLayoutId'], false);
$this->createIndex(null, Table::ELEMENTS, ['type'], false);
Expand All @@ -818,6 +817,7 @@ public function createIndexes(): void
$this->createIndex(null, Table::ELEMENTS, ['archived', 'dateCreated'], false);
$this->createIndex(null, Table::ELEMENTS, ['archived', 'dateDeleted', 'draftId', 'revisionId', 'canonicalId'], false);
$this->createIndex(null, Table::ELEMENTS, ['archived', 'dateDeleted', 'draftId', 'revisionId', 'canonicalId', 'enabled'], false);
$this->createIndex(null, Table::ELEMENTS_BULKOPS, ['timestamp'], false);
$this->createIndex(null, Table::ELEMENTS_SITES, ['elementId', 'siteId'], true);
$this->createIndex(null, Table::ELEMENTS_SITES, ['siteId'], false);
$this->createIndex(null, Table::ELEMENTS_SITES, ['title', 'siteId'], false);
Expand Down Expand Up @@ -989,11 +989,11 @@ public function addForeignKeys(): void
$this->addForeignKey(null, Table::ELEMENTACTIVITY, ['userId'], Table::USERS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTACTIVITY, ['siteId'], Table::SITES, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTACTIVITY, ['draftId'], Table::DRAFTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTBULKOPS, ['elementId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS, ['canonicalId'], Table::ELEMENTS, ['id'], 'SET NULL');
$this->addForeignKey(null, Table::ELEMENTS, ['draftId'], Table::DRAFTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS, ['revisionId'], Table::REVISIONS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS, ['fieldLayoutId'], Table::FIELDLAYOUTS, ['id'], 'SET NULL', null);
$this->addForeignKey(null, Table::ELEMENTS_BULKOPS, ['elementId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS_OWNERS, ['elementId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS_OWNERS, ['ownerId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::ELEMENTS_SITES, ['elementId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
Expand Down
6 changes: 3 additions & 3 deletions src/migrations/m231213_030600_element_bulk_ops.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class m231213_030600_element_bulk_ops extends Migration
*/
public function safeUp(): bool
{
$this->createTable(Table::ELEMENTBULKOPS, [
$this->createTable(Table::ELEMENTS_BULKOPS, [
'elementId' => $this->integer(),
'key' => $this->char(10)->notNull(),
'timestamp' => $this->dateTime()->notNull(),
'PRIMARY KEY([[elementId]], [[key]])',
]);
$this->addForeignKey(null, Table::ELEMENTBULKOPS, ['elementId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->createIndex(null, Table::ELEMENTBULKOPS, ['timestamp'], false);
$this->addForeignKey(null, Table::ELEMENTS_BULKOPS, ['elementId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->createIndex(null, Table::ELEMENTS_BULKOPS, ['timestamp'], false);
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ public function endBulkOp(string $key): void
]));
}

Db::delete(Table::ELEMENTBULKOPS, ['key' => $key]);
Db::delete(Table::ELEMENTS_BULKOPS, ['key' => $key]);
}

/**
Expand All @@ -1125,7 +1125,7 @@ public function trackElementInBulkOps(ElementInterface $element): void
$timestamp = Db::prepareDateForDb(DateTimeHelper::now());

foreach (array_keys($this->bulkKeys) as $key) {
Db::upsert(Table::ELEMENTBULKOPS, [
Db::upsert(Table::ELEMENTS_BULKOPS, [
'elementId' => $element->id,
'key' => $key,
'timestamp' => $timestamp,
Expand Down
2 changes: 1 addition & 1 deletion src/services/Gc.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ private function _deleteStaleElementActivity(): void
private function _deleteStaleBulkElementOps(): void
{
$this->_stdout(' > deleting stale bulk element operation records ... ');
Db::delete(Table::ELEMENTBULKOPS, ['<', 'timestamp', Db::prepareDateForDb(new DateTime('2 weeks ago'))]);
Db::delete(Table::ELEMENTS_BULKOPS, ['<', 'timestamp', Db::prepareDateForDb(new DateTime('2 weeks ago'))]);
$this->_stdout("done\n", Console::FG_GREEN);
}

Expand Down

0 comments on commit cec68f9

Please sign in to comment.