Skip to content

Commit

Permalink
Use the primary connection when fetching records
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Jul 12, 2023
1 parent 8975362 commit 8f25718
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Blitz

## 4.4.7 - Unreleased
### Fixed
- Fixed a race condition that could result in an SQL error if the database used read/write splitting ([#531](https://github.com/putyourlightson/craft-blitz/issues/531)).

## 4.4.6 - 2023-06-28
> {warning} To ensure the fix is applied, the cache should be cleared or refreshed after this update completes.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "putyourlightson/craft-blitz",
"description": "Intelligent static page caching for creating lightning-fast sites.",
"version": "4.4.6",
"version": "4.4.7",
"type": "craft-plugin",
"homepage": "https://putyourlightson.com/plugins/blitz",
"license": "proprietary",
Expand Down
22 changes: 12 additions & 10 deletions src/services/GenerateCacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,18 @@ public function saveElementQuery(ElementQuery $elementQuery): void
return;
}

// Get element query record from index or create one if it does not exist
$queryId = ElementQueryRecord::find()
$db = Craft::$app->getDb();

// Use the primary connection when fetching the record
$queryId = $db->usePrimary(fn() => ElementQueryRecord::find()
->select('id')
->where(['index' => $index])
->scalar();
->scalar()
);

if (!$queryId) {
try {
// Use DB connection, so we can exclude audit columns when inserting
$db = Craft::$app->getDb();

$db->createCommand()
->insert(
ElementQueryRecord::tableName(),
Expand Down Expand Up @@ -378,17 +379,18 @@ public function saveInclude(int $siteId, string $template, array $params): ?arra
return null;
}

// Get record or create one if it does not exist
$includeId = IncludeRecord::find()
$db = Craft::$app->getDb();

// Use the primary connection when fetching the record
$includeId = $db->usePrimary(fn() => IncludeRecord::find()
->select('id')
->where(['index' => $index])
->scalar();
->scalar()
);

if (!$includeId) {
try {
// Use DB connection, so we can exclude audit columns when inserting
$db = Craft::$app->getDb();

$db->createCommand()
->insert(
IncludeRecord::tableName(),
Expand Down

0 comments on commit 8f25718

Please sign in to comment.