Project

General

Profile

Actions

Feature #12598

closed

Investigate and remediate database tables without a primary key

Added by Boone Gorges about 4 years ago. Updated almost 4 years ago.

Status:
Resolved
Priority name:
Normal
Assignee:
Category name:
WordPress Plugins
Target version:
Start date:
2020-03-31
Due date:
% Done:

0%

Estimated time:
Deployment actions:

Description

Lihua has recently asked our team to investigate why a number of our WordPress tables have no primary key. The context of the request is that he'd like to explore a cluster configuration for database servers, giving us greater redundancy and mitigating against downtime issues. (See eg #12436.)

I'm opening this ticket to explore what needs to be done to ensure that all Commons tables have primary keys.

The first step will be to analyze a list of tables without primary keys. While there are hundreds of tables without primary keys, the vast majority are generated by a small handful of plugins. See attached cac-database-tables-without-primary-keys.txt. By cataloging the distinct cases, we can then begin analyzing each individual case. The rough steps for each case will be as follows:

1. Determine how the table is created. In general, this will mean identifying the responsible plugin (I believe it's always a plugin, never WP itself), and the finding the specific place in the plugin where the table schema is defined.
2. Assess whether adding a primary key would in any way break functionality. As a rule, the answer should be no: we should always be able to add an `AUTO_INCREMENT` column, which would be ignored by the plugin code, but would satisfy the PXC requirements.
3. Assuming we can add the primary key, write and execute a script that will `ALTER TABLE` for each relevant table in question in our database.
4. Determine whether any application-level changes are necessary to ensure that tables created in the future will have the necessary key. In some cases, the responsible plugins have been deprecated - see eg firestats - so the point is moot. In other cases, the plugin is abandoned, so we can probably simply modify the table schema as defined in the plugin code. In other cases, we may need a plugin-level `ALTER TABLE` statement.

I've included Lihua's original request below, for reference:

I am exploring feasible paths to migrate production databases to a PXC cluster.

One major hurdle is that PXC is best run with strict mode set to ENFORCING or MASTER:

https://www.percona.com/doc/percona-xtradb-cluster/LATEST/features/pxc-strict-mode.html

And the ENFORCING or MASTER node will try to validate a whole bunch of items:
https://www.percona.com/doc/percona-xtradb-cluster/LATEST/features/pxc-strict-mode.html#validations

My question for you:

Why so many wordpress database tables are created (or designed) without a primary key? Do you think there is a feasible path to set or create surragate keys for wordpress tables in order to migrate to a mysql cluster?

https://stackoverflow.com/questions/2515596/can-a-database-table-be-without-a-primary-key


Files

cac-database-tables-without-primary-key.txt (51.3 KB) cac-database-tables-without-primary-key.txt Boone Gorges, 2020-03-31 04:24 PM
cas-count.php (1.48 KB) cas-count.php Script to add primary keys for peters-custom-anti-spam-image tables Boone Gorges, 2020-04-01 12:14 PM
askit-table-keys.php (539 Bytes) askit-table-keys.php Script to add primary key column to AskIt theme tables Boone Gorges, 2020-04-02 04:59 PM
tantan-flickr-table-keys.php (416 Bytes) tantan-flickr-table-keys.php Add primary key column for tantan-flickr Boone Gorges, 2020-04-08 11:12 AM
statpress-primary-key.php (301 Bytes) statpress-primary-key.php Add primary key to statpress tables Boone Gorges, 2020-04-08 11:37 AM
woo-custom-nav-primary-key.php (306 Bytes) woo-custom-nav-primary-key.php Add primary keys to woo custom nav tables Boone Gorges, 2020-04-08 11:52 AM
newsletters-lite-primary-key.php (431 Bytes) newsletters-lite-primary-key.php Add missing rel_id primary key column to newsletters-lite tables Boone Gorges, 2020-04-08 12:46 PM
sign-up-sheets-primary-key.php (350 Bytes) sign-up-sheets-primary-key.php Create primary keys on sign-up-sheets tables Boone Gorges, 2020-04-17 10:57 AM
ninja-forms-primary-key.php (349 Bytes) ninja-forms-primary-key.php Set primary key on ninja-forms tables. Boone Gorges, 2020-04-17 11:47 AM

Related issues

Related to CUNY Academic Commons - Feature #12800: Automated scan for database schema validityResolved2020-05-13

Actions
Actions

Also available in: Atom PDF