Skip to content

Commit

Permalink
Merge pull request #5 from alleyinteractive/fix/multisite
Browse files Browse the repository at this point in the history
Fix table prefix in multisite
  • Loading branch information
dlh01 authored Oct 25, 2024
2 parents 12c0031 + 85260b3 commit b718157
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a CHANGELOG](https://keepachangelog.com/en/1.0.0/).

## 0.3.0

## Changed

- When storing items with the PSR-16 adapter, the group name is now automatically prefixed to avoid unintended data loss when flushing the cache.

### Fixed

- Incorrect database table prefix in multisite installations.

## 0.2.0

### Added
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ $cache->clear();

Note that the cache adapter will store data in a custom array structure, as described [in the wp-psr16 README](https://github.com/alleyinteractive/wp-psr16/blob/5ff411661f9682b3184dab596180a7a3edcaf446/README.md#implementation-details).

## Why Not Use Options?

There's nothing wrong with using options for storing key-value data, but it comes with overhead, including:

- Managing autoloading and the `alloptions` cache.
- `pre_option_` and `option_` filters on the values.
- Settings registration and default values.

Big Pit doesn't have autoloading, filters, or registered keys, so it might work for you if you don't need these features.

Or, you might plan to store thousands of rows, and you don't want to dilute the options table with that amount of data.

## About

### License
Expand Down
25 changes: 16 additions & 9 deletions src/class-big-pit.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function instance(): self {
public function boot(): void {
global $wpdb;

$wpdb->big_pit = $wpdb->base_prefix . 'big_pit';
$wpdb->big_pit = $wpdb->get_blog_prefix() . 'big_pit';

if ( defined( 'WP_INSTALLING' ) ) {
return;
Expand Down Expand Up @@ -195,7 +195,7 @@ public function flush_group( string $group ): void {
private function upsert(): void {
global $wpdb;

$available_version = '1';
$available_version = '2';
$installed_version = get_option( 'wp_big_pit_database_version', '0' );

if ( $available_version === $installed_version ) {
Expand All @@ -206,9 +206,15 @@ private function upsert(): void {
require_once ABSPATH . '/wp-admin/includes/upgrade.php';
}

if ( ! $installed_version ) {
$delta = \dbDelta(
<<<SQL
// Ensure the database version is at least version 2.
if ( ! $installed_version || '1' === $installed_version ) {
/*
* Create the table if this is a new installation or if this is a site in
* a multisite that didn't get a site-specific table at version 1.
*/
if ( ! $installed_version || $wpdb->blogid > 1 ) {
$delta = \dbDelta(
<<<SQL
CREATE TABLE {$wpdb->big_pit} (
item_id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
item_group varchar(255) NOT NULL,
Expand All @@ -218,13 +224,14 @@ private function upsert(): void {
KEY key_value (item_key(191), item_value(100))
) {$wpdb->get_charset_collate()};
SQL
);
);

if ( ! isset( $delta[ $wpdb->big_pit ] ) ) {
throw new \Exception( 'Failed to create table.' );
if ( ! isset( $delta[ $wpdb->big_pit ] ) ) {
throw new \Exception( 'Failed to create table.' );
}
}

$installed_version = '1';
$installed_version = '2';
}

update_option( 'wp_big_pit_database_version', $installed_version );
Expand Down
3 changes: 2 additions & 1 deletion src/simplecache/class-big-pit-adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public static function create( string $group ): CacheInterface {
limit: 172,
origin: new self(
pit: Big_Pit::instance(),
group: $group,
// Prefix the group so that `flush()` doesn't wipe out other data.
group: "_psr16_{$group}",
),
),
),
Expand Down

0 comments on commit b718157

Please sign in to comment.