Skip to content

Commit

Permalink
BUGS-6563: Add primary key if necessary (#265)
Browse files Browse the repository at this point in the history
BUGS-6563: Provides command to add a Primary Key column to Sessions table and notice to users if needed.
  • Loading branch information
rwagner00 authored Oct 4, 2023
1 parent bb5dd6c commit 60030f5
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 26 deletions.
44 changes: 24 additions & 20 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2
version: 2.1
workflows:
version: 2
version: 2.1
main:
jobs:
- test-behat
Expand All @@ -16,6 +16,25 @@ workflows:
- master
jobs:
- test-behat
commands:
run_test:
steps:
- run:
name: "Run Tests"
command: |
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
composer phpunit
WP_MULTISITE=1 composer phpunit
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 nightly true
composer phpunit
bash bin/full-teardown-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
vendor/bin/phpunit --group=cli_command
WP_MULTISITE=1 vendor/bin/phpunit --group=cli_command
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 nightly true
vendor/bin/phpunit --group=cli_command
jobs:
test-behat:
resource_class: small
Expand Down Expand Up @@ -94,15 +113,8 @@ jobs:
sudo docker-php-ext-enable imagick
sudo docker-php-ext-install mysqli
sudo apt-get install mariadb-client-10.5
- run:
name: "Run Tests"
command: |
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
composer phpunit
WP_MULTISITE=1 composer phpunit
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 nightly true
composer phpunit
- run_test

test-phpunit-82:
resource_class: small
working_directory: ~/pantheon-systems/wp-native-php-sessions
Expand Down Expand Up @@ -133,12 +145,4 @@ jobs:
sudo docker-php-ext-enable imagick
sudo docker-php-ext-install mysqli
sudo apt-get install mariadb-client-10.6
- run:
name: "Run Tests"
command: |
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
composer phpunit
WP_MULTISITE=1 composer phpunit
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 nightly true
composer phpunit
- run_test
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
**Tags:** comments, sessions
**Requires at least:** 4.7
**Tested up to:** 6.3
**Stable tag:** 1.3.7-dev
**Stable tag:** 1.4.0-dev
**Requires PHP:** 5.4
**License:** GPLv2 or later
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -42,6 +42,20 @@ To override this use the `pantheon_session_expiration` filter before the WordPre
}
add_filter( 'pantheon_session_expiration', 'my_session_expiration_override' );

## CLI Commands ##

### `wp pantheon session add-index` ###

Added in 1.4.0-dev. This command should be run if your installation of the plugin occurred before the addition of the primary ID key to the session table in version 1.2.2. You will be automatically notified when you visit any admin page if this is the case. If there's no message, your version is good to go. Note that this command is non-destructive, a new table will be created and the existing one preserved in a backup state until you have verified that the upgrade is functioning as expected.

### `wp pantheon session primary-key-finalize` ###

Added in 1.4.0-dev. If you have run the `add-index` command and have verified that the new table is functioning correctly, running the `primary-key-finalize` command will perform a database cleanup and remove the backup table.

### `wp pantheon session primary-key-revert` ###

Added in 1.4.0-dev. If you have run the `add-index` command and something unexpected has occurred, just run the `primary-key-revert` command and the backup table will immediately be returned to being the active table.

## Contributing ##

See [CONTRIBUTING.md](https://github.com/pantheon-systems/wp-native-php-sessions/blob/main/CONTRIBUTING.md) for information on contributing.
Expand Down Expand Up @@ -73,9 +87,16 @@ To fix, create a new file at `wp-content/mu-plugins/000-loader.php` and include

This mu-plugin will load WP Native PHP Sessions before all other plugins, while letting you still use the WordPress plugin updater to keep the plugin up-to-date.

## Upgrade Notice ##

### 1.4.0-dev ###
Adds a WP-CLI command to add an index to the sessions table if one does not exist already. If you installed this plugin before version 1.2.2, you likely need to run this command. However, regardless of version at installation a notice will appear in your admin dashboard if your database table is missing the index. If no notice appears, no action is necessary.

## Changelog ##

### 1.3.7-dev ###
### 1.4.0-dev ###
* Adds new CLI command to add a Primary Column (id) to the `pantheon_sessions` table for users who do not have one. [[#265](https://github.com/pantheon-systems/wp-native-php-sessions/pull/265)]
* Adds alert to dashboard for users who need to run the new command.
* Updates Pantheon WP Coding Standards to 2.0 [[#264](https://github.com/pantheon-systems/wp-native-php-sessions/pull/264)]

### 1.3.6 (June 1, 2023) ###
Expand Down
44 changes: 44 additions & 0 deletions bin/full-teardown-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host]"
exit 1
fi

DB_NAME=$1
DB_USER=$2
DB_PASS=$3
DB_HOST=${4-localhost}

TMPDIR=${TMPDIR-/tmp}
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/}

file_delete() {
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
}

drop_db() {

# parse DB_HOST for port or socket references
local PARTS=(${DB_HOST//\:/ })
local DB_HOSTNAME=${PARTS[0]};
local DB_SOCK_OR_PORT=${PARTS[1]};
local EXTRA=""

if ! [ -z $DB_HOSTNAME ] ; then
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
EXTRA=" --socket=$DB_SOCK_OR_PORT"
elif ! [ -z $DB_HOSTNAME ] ; then
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
fi
fi

# create database
echo Y | mysqladmin drop $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
}

file_delete
drop_db
30 changes: 30 additions & 0 deletions inc/class-cli-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ public function delete( $args, $assoc_args ) {
}
}
}

/**
* Set id as primary key in the Native PHP Sessions plugin table.
*
* @subcommand add-index
*/
public function add_index( $args, $assoc_arc ) {
$pantheon_session = new \Pantheon_Sessions();
$pantheon_session->add_index();
}

/**
* Finalizes the creation of a primary key by deleting the old data.
*
* @subcommand primary-key-finalize
*/
public function primary_key_finalize() {
$pan_session = new \Pantheon_Sessions();
$pan_session->primary_key_finalize();
}

/**
* Reverts addition of primary key.
*
* @subcommand primary-key-revert
*/
public function primary_key_revert() {
$pan_session = new \Pantheon_Sessions();
$pan_session->primary_key_revert();
}
}

\WP_CLI::add_command( 'pantheon session', '\Pantheon_Sessions\CLI_Command' );
Loading

0 comments on commit 60030f5

Please sign in to comment.