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

Adjust some query and table functions #2774

Merged
merged 2 commits into from
Jan 21, 2025
Merged

Conversation

mikkamp
Copy link
Contributor

@mikkamp mikkamp commented Jan 17, 2025

Changes proposed in this Pull Request:

As a result of the discussion in issue #500 there were three things to address:

  1. Remove the GROUP BY (we already have a DB constraint handling this, if we didn't it would alter the count query results)
    This was completed in this PR

  2. Use esc_like when checking for an existing table
    This was completed in this PR

  3. Remove get_sql_safe_name and make sure we are placing the table name in backquotes in all locations
    I decided to leave this in place for now as it isn't causing any harm. Ideally in the future we'd use wpdb->prepare for escaping the table names: https://make.wordpress.org/core/2022/10/08/escaping-table-and-field-names-with-wpdbprepare-in-wordpress-6-1/
    However that's only available in WordPress 6.2, we would have to bump the minimum version for that.

Closes #500

Detailed test instructions:

  1. Add a snippet to log DB queries:
// Log queries from gla.
add_filter(
	'query',
	function ( string $query ) {
		global $wpdb;

		if ( str_contains( strtolower( $query ), "{$wpdb->prefix}gla_" ) ) {
			wc_get_logger()->log(
				WC_Log_Levels::DEBUG,
				$query,
				[
					'source' => 'gla-db-queries',
				]
			);
		}

		return $query;
	}
);
  1. Go to the Product Feed page to load issues from the table
  2. Install this PR and reload the Product Feed page
  3. Go to WooCommerce > Status > Logs and find a log file for gla-db-queries
  4. Confirm we initially have the DB query with a GROUP BY:
SELECT * FROM `wp_gla_merchant_issues`
WHERE type = 'account' AND
severity IN ('error','critical')
GROUP BY `wp_gla_merchant_issues`.`id`
ORDER BY `type` ASC, `product` ASC, `issue` ASC
  1. Confirm the DB query no longer has the GROUP BY after switching to this PR:
SELECT * FROM `wp_gla_merchant_issues`
WHERE type = 'account' AND
severity IN ('error','critical')
GROUP BY `wp_gla_merchant_issues`.`id`

The second change is harder to test because we don't use invalid characters in table names. But we can confirm tables are recreated after checking if they exist:

  1. Drop one of the tables like wp_gla_budget_recommendations
  2. Delete options:
    • wp option delete gla_db_version
    • wp option delete gla_file_version
  3. Refresh one of the WP Admin pages
  4. Confirm the database table has been recreated and populated

Changelog entry

  • Tweak - Adjust some query and table functions.

@mikkamp mikkamp self-assigned this Jan 17, 2025
@github-actions github-actions bot added the changelog: tweak Small change, that isn't actually very important. label Jan 17, 2025
@mikkamp mikkamp requested a review from a team January 17, 2025 17:08
Copy link

codecov bot commented Jan 17, 2025

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 67.2%. Comparing base (9949c75) to head (2f2c112).
Report is 14 commits behind head on develop.

Files with missing lines Patch % Lines
src/DB/Table.php 0.0% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             develop   #2774     +/-   ##
===========================================
- Coverage       67.2%   67.2%   -0.0%     
+ Complexity      4677    4676      -1     
===========================================
  Files            480     480             
  Lines          19571   19569      -2     
===========================================
- Hits           13159   13155      -4     
- Misses          6412    6414      +2     
Flag Coverage Δ
php-unit-tests 67.2% <0.0%> (-<0.1%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/DB/Query.php 43.6% <ø> (-0.7%) ⬇️
src/DB/Table.php 34.4% <0.0%> (-6.2%) ⬇️

Copy link
Member

@ianlin ianlin left a comment

Choose a reason for hiding this comment

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

Thanks for the update. Confirmed the query did not contain GROUP BY and the deleted table was recreated successfully.

@mikkamp mikkamp merged commit 6e15a75 into develop Jan 21, 2025
14 checks passed
@mikkamp mikkamp deleted the tweak/500-query-and-table branch January 21, 2025 09:01
@puntope puntope mentioned this pull request Jan 28, 2025
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: tweak Small change, that isn't actually very important.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Revisit some Query and Table issues
2 participants