Skip to content

Commit 60030f5

Browse files
authored
BUGS-6563: Add primary key if necessary (#265)
BUGS-6563: Provides command to add a Primary Key column to Sessions table and notice to users if needed.
1 parent bb5dd6c commit 60030f5

8 files changed

Lines changed: 386 additions & 26 deletions

File tree

.circleci/config.yml

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
version: 2
1+
version: 2.1
22
workflows:
3-
version: 2
3+
version: 2.1
44
main:
55
jobs:
66
- test-behat
@@ -16,6 +16,25 @@ workflows:
1616
- master
1717
jobs:
1818
- test-behat
19+
commands:
20+
run_test:
21+
steps:
22+
- run:
23+
name: "Run Tests"
24+
command: |
25+
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
26+
composer phpunit
27+
WP_MULTISITE=1 composer phpunit
28+
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
29+
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 nightly true
30+
composer phpunit
31+
bash bin/full-teardown-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
32+
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
33+
vendor/bin/phpunit --group=cli_command
34+
WP_MULTISITE=1 vendor/bin/phpunit --group=cli_command
35+
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
36+
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 nightly true
37+
vendor/bin/phpunit --group=cli_command
1938
jobs:
2039
test-behat:
2140
resource_class: small
@@ -94,15 +113,8 @@ jobs:
94113
sudo docker-php-ext-enable imagick
95114
sudo docker-php-ext-install mysqli
96115
sudo apt-get install mariadb-client-10.5
97-
- run:
98-
name: "Run Tests"
99-
command: |
100-
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
101-
composer phpunit
102-
WP_MULTISITE=1 composer phpunit
103-
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
104-
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 nightly true
105-
composer phpunit
116+
- run_test
117+
106118
test-phpunit-82:
107119
resource_class: small
108120
working_directory: ~/pantheon-systems/wp-native-php-sessions
@@ -133,12 +145,4 @@ jobs:
133145
sudo docker-php-ext-enable imagick
134146
sudo docker-php-ext-install mysqli
135147
sudo apt-get install mariadb-client-10.6
136-
- run:
137-
name: "Run Tests"
138-
command: |
139-
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
140-
composer phpunit
141-
WP_MULTISITE=1 composer phpunit
142-
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
143-
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 nightly true
144-
composer phpunit
148+
- run_test

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
**Tags:** comments, sessions
44
**Requires at least:** 4.7
55
**Tested up to:** 6.3
6-
**Stable tag:** 1.3.7-dev
6+
**Stable tag:** 1.4.0-dev
77
**Requires PHP:** 5.4
88
**License:** GPLv2 or later
99
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html
@@ -42,6 +42,20 @@ To override this use the `pantheon_session_expiration` filter before the WordPre
4242
}
4343
add_filter( 'pantheon_session_expiration', 'my_session_expiration_override' );
4444

45+
## CLI Commands ##
46+
47+
### `wp pantheon session add-index` ###
48+
49+
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.
50+
51+
### `wp pantheon session primary-key-finalize` ###
52+
53+
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.
54+
55+
### `wp pantheon session primary-key-revert` ###
56+
57+
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.
58+
4559
## Contributing ##
4660

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

7488
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.
7589

90+
## Upgrade Notice ##
91+
92+
### 1.4.0-dev ###
93+
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.
94+
7695
## Changelog ##
7796

78-
### 1.3.7-dev ###
97+
### 1.4.0-dev ###
98+
* 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)]
99+
* Adds alert to dashboard for users who need to run the new command.
79100
* Updates Pantheon WP Coding Standards to 2.0 [[#264](https://github.com/pantheon-systems/wp-native-php-sessions/pull/264)]
80101

81102
### 1.3.6 (June 1, 2023) ###

bin/full-teardown-wp-tests.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
3+
if [ $# -lt 3 ]; then
4+
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host]"
5+
exit 1
6+
fi
7+
8+
DB_NAME=$1
9+
DB_USER=$2
10+
DB_PASS=$3
11+
DB_HOST=${4-localhost}
12+
13+
TMPDIR=${TMPDIR-/tmp}
14+
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
15+
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/}
16+
17+
file_delete() {
18+
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
19+
}
20+
21+
drop_db() {
22+
23+
# parse DB_HOST for port or socket references
24+
local PARTS=(${DB_HOST//\:/ })
25+
local DB_HOSTNAME=${PARTS[0]};
26+
local DB_SOCK_OR_PORT=${PARTS[1]};
27+
local EXTRA=""
28+
29+
if ! [ -z $DB_HOSTNAME ] ; then
30+
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
31+
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
32+
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
33+
EXTRA=" --socket=$DB_SOCK_OR_PORT"
34+
elif ! [ -z $DB_HOSTNAME ] ; then
35+
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
36+
fi
37+
fi
38+
39+
# create database
40+
echo Y | mysqladmin drop $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
41+
}
42+
43+
file_delete
44+
drop_db

inc/class-cli-command.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,36 @@ public function delete( $args, $assoc_args ) {
7878
}
7979
}
8080
}
81+
82+
/**
83+
* Set id as primary key in the Native PHP Sessions plugin table.
84+
*
85+
* @subcommand add-index
86+
*/
87+
public function add_index( $args, $assoc_arc ) {
88+
$pantheon_session = new \Pantheon_Sessions();
89+
$pantheon_session->add_index();
90+
}
91+
92+
/**
93+
* Finalizes the creation of a primary key by deleting the old data.
94+
*
95+
* @subcommand primary-key-finalize
96+
*/
97+
public function primary_key_finalize() {
98+
$pan_session = new \Pantheon_Sessions();
99+
$pan_session->primary_key_finalize();
100+
}
101+
102+
/**
103+
* Reverts addition of primary key.
104+
*
105+
* @subcommand primary-key-revert
106+
*/
107+
public function primary_key_revert() {
108+
$pan_session = new \Pantheon_Sessions();
109+
$pan_session->primary_key_revert();
110+
}
81111
}
82112

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

0 commit comments

Comments
 (0)