Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from blasvicco/master
Browse files Browse the repository at this point in the history
Fix #14 - SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'url' - thanks @blasvicco
  • Loading branch information
Oliver Stark authored Jun 18, 2018
2 parents 7d01348 + d63c751 commit e394f4d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
44 changes: 26 additions & 18 deletions src/EventRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,32 @@ public static function registerFallback()
? implode(" ", $event->tags)
: str_replace(['[', ']'], ['{', '}'], json_encode($event->tags));

// Insert item
\Craft::$app->getDb()->createCommand()
->upsert(
// Table
Plugin::CACHE_TABLE,

// Identifier
['url' => $event->requestUrl],

// Data
[
'url' => $event->requestUrl,
'tags' => $tags,
'headers' => json_encode($event->headers),
'siteId' => \Craft::$app->getSites()->currentSite->id
]
)
->execute();
// in order to have a unique (collitions are possible) identifier by url with a fixed length
$urlHash = md5($event->requestUrl);

try {
// Insert item
\Craft::$app->getDb()->createCommand()
->upsert(
// Table
Plugin::CACHE_TABLE,

// Identifier
['urlHash' => $urlHash],

// Data
[
'urlHash' => $urlHash,
'url' => $event->requestUrl,
'tags' => $tags,
'headers' => json_encode($event->headers),
'siteId' => \Craft::$app->getSites()->currentSite->id
]
)
->execute();
} catch (\Exception $e) {
\Craft::warning("Failed to register fallback.", "upper");
}

});

Expand Down
8 changes: 5 additions & 3 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ public function safeUp()

$this->createTable(Plugin::CACHE_TABLE, [
'uid' => $this->string(40)->notNull()->unique(),
'url' => $this->string(255)->notNull(),
'urlHash' => $this->string(32)->notNull(),
'url' => $this->text()->notNull(),
'headers' => $this->text()->defaultValue(null),
'tags' => $this->text()->notNull(),
'siteId' => $this->integer(),
'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->null()
]);

$this->createIndex('url_idx', Plugin::CACHE_TABLE, 'url', true);
$this->createIndex('url_idx', Plugin::CACHE_TABLE, 'urlHash', true);
$this->execute("ALTER TABLE " . Plugin::CACHE_TABLE . " ADD FULLTEXT INDEX tags_fulltext (tags ASC)");

}
Expand All @@ -38,6 +39,7 @@ public function safeUp()

$this->createTable(Plugin::CACHE_TABLE, [
'uid' => $this->string(40)->notNull()->unique(),
'urlHash' => $this->string(32)->notNull(),
'url' => $this->string(255)->notNull(),
'headers' => $this->text()->defaultValue(null),
'tags' => 'varchar[]',
Expand All @@ -47,7 +49,7 @@ public function safeUp()
'PRIMARY KEY(uid)',
]);

$this->createIndex('url_idx', Plugin::CACHE_TABLE, 'url', true);
$this->createIndex('url_idx', Plugin::CACHE_TABLE, 'urlHash', true);
$this->execute("CREATE INDEX tags_array ON " . Plugin::CACHE_TABLE . " USING GIN(tags)");

}
Expand Down

0 comments on commit e394f4d

Please sign in to comment.