Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stats Schema #2719

Merged
merged 11 commits into from
Jan 30, 2024
Merged

Add Stats Schema #2719

merged 11 commits into from
Jan 30, 2024

Conversation

fjorgemota
Copy link
Member

@fjorgemota fjorgemota commented Jan 30, 2024

Fixes #2715

Changes Proposed in this Pull Request

  • Add logic to initialize alias for stats table on wpdb
  • Add method to migrate the table for the latest version (and run it on plugin activation!)

Testing Instructions

Check if the plugin can be activated on a new WP install just fine, without any errors on the error log. Also check if the wp_job_manager_stats is created as appropriate

Release Notes

New or Updated Hooks and Templates

Deprecated Code

Screenshot / Video


Plugin build for dd028a7
📦 Download plugin zip
▶️ Open in playground

@fjorgemota fjorgemota self-assigned this Jan 30, 2024
[
"CREATE TABLE {$wpdb->job_manager_stats} (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to remove the CURRENT_DATE here because apparently it is not supported by MySQL older than the 8.0.20

private function initialize_wpdb() {
global $wpdb;
$wpdb->job_manager_stats = $wpdb->prefix . 'job_manager_stats';
$wpdb->tables[] = 'job_manager_stats';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for including the table name on tables is so WP automatically updates the attribute with the proper name with the prefix when switching to another blog on a multisite installation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

global $wpdb;
$collate = $wpdb->get_charset_collate();
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
\dbDelta(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function will run automatically a diff for us on the SQL and do any changes on the database side.

@fjorgemota fjorgemota marked this pull request as ready for review January 30, 2024 01:34
@fjorgemota fjorgemota requested review from a team and removed request for a team January 30, 2024 01:34
Base automatically changed from feature/stats-add-stats-class to feature/stats January 30, 2024 10:18
@pgk pgk changed the base branch from feature/stats to trunk January 30, 2024 10:35
@pgk pgk changed the base branch from trunk to feature/stats January 30, 2024 10:35
Copy link
Contributor

@pgk pgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It worked well! I left some comments, mainly around compat and for moving some things around a bit.

Edit: Forgot to mention something important: The table name we use is the same table name used by Astoundify stats. I think that this might be a problem, specially since enabling the plugin might dbDelta an existing astoundify table and possibly corrupt existing stats.

includes/class-stats.php Outdated Show resolved Hide resolved
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
\dbDelta(
[
"CREATE TABLE {$wpdb->job_manager_stats} (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"CREATE TABLE {$wpdb->job_manager_stats} (
"CREATE TABLE IF NOT EXISTS {$wpdb->job_manager_stats} (

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't clear at first, but dbDelta already handles that for us, and I think it doesn't support having IF NOT EXISTS on the table name (it will consider the IF NOT EXISTS part of the table name, which I don't think we want to have). See how it handles that here: https://github.com/WordPress/WordPress/blob/27348531f3fdf048809da977fced3f40b90259de/wp-admin/includes/upgrade.php#L2836-L2840

You can see here that no WP tables use IF NOT EXISTS when creating the schema (those are passed to dbDelta, too). So I'm inclined to not add them. WDYT?

*
* @var $custom_tables
*/
private static $custom_tables = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be a const?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other properties in the class are private static, hence why I put this as private static too.

I agree that changing everything to private const might make more sense, but I'd change all the other properties too. Do you think it is worth it, @pgk?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if this property is always constant it makes sense for it to be const.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed that specific property in dd028a7. As mentioned on Slack, I'll submit another PR to change the other properties too. :)

private function initialize_wpdb() {
global $wpdb;
$wpdb->job_manager_stats = $wpdb->prefix . 'job_manager_stats';
$wpdb->tables[] = 'job_manager_stats';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

includes/class-wp-job-manager-install.php Show resolved Hide resolved
@fjorgemota fjorgemota requested a review from pgk January 30, 2024 13:51
To avoid conflict with third-party plugins
@fjorgemota fjorgemota requested a review from a team January 30, 2024 13:56
@fjorgemota
Copy link
Member Author

Edit: Forgot to mention something important: The table name we use is the same table name used by Astoundify stats. I think that this might be a problem, specially since enabling the plugin might dbDelta an existing astoundify table and possibly corrupt existing stats.

Great catch!

I renamed it in 5f63042 . Thanks!

@fjorgemota fjorgemota linked an issue Jan 30, 2024 that may be closed by this pull request
Copy link
Contributor

@pgk pgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Table name change looks good, and activating/deactivating logic worked well!

@fjorgemota fjorgemota merged commit d95908f into feature/stats Jan 30, 2024
9 checks passed
@fjorgemota fjorgemota deleted the add/stats-schema branch January 30, 2024 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create schema for stats data on upgrade
3 participants