refactor: extract upgrade logic to PSA_Upgrade class (fixes CI)
CI on 61cf00c failed PHPUnit with "Call to undefined function
plugin_dir_path()" — the test required peptide-search-ai.php, which calls
plugin_dir_path() at top level (line 19) before any hook. Stubbing every
top-level WP function the plugin file touches was the wrong shape of fix.
Proper fix: move the upgrade logic into includes/class-psa-upgrade.php so
the test can require just the helper class, matching the pattern every
other includes/ module follows.
- New: includes/class-psa-upgrade.php with PSA_Upgrade::is_needed()
(pure) and PSA_Upgrade::maybe_run() (driver). OPTION_NAME and
DEFAULT_STORED_VERSION exposed as constants.
- peptide-search-ai.php: psa_admin_init() now calls PSA_Upgrade::maybe_run();
the inline helper functions and docblocks are removed.
- tests/bootstrap.php: loads class-psa-upgrade.php alongside the other
tested classes.
- tests/unit/UpgradeTest.php: rewritten against PSA_Upgrade; adds two
extra assertions on the constants for stability.
- ARCHITECTURE.md: refreshed to reference the class and its constants.
No behavioral change. The cost-tracker schema upgrade still runs once per
admin_init when PSA_VERSION advances past psa_db_version.
Agent-Session: cowork-qa-p1-fixes-20260414
This commit is contained in:
@@ -33,6 +33,7 @@ require_once PSA_PLUGIN_DIR . 'includes/class-psa-admin.php';
|
||||
require_once PSA_PLUGIN_DIR . 'includes/class-psa-batch-enrichment.php';
|
||||
require_once PSA_PLUGIN_DIR . 'includes/class-psa-template.php';
|
||||
require_once PSA_PLUGIN_DIR . 'includes/class-psa-directory.php';
|
||||
require_once PSA_PLUGIN_DIR . 'includes/class-psa-upgrade.php';
|
||||
|
||||
function psa_init() {
|
||||
PSA_Post_Type::register_peptide_post_type();
|
||||
@@ -48,49 +49,10 @@ add_action( 'init', 'psa_init' );
|
||||
|
||||
function psa_admin_init() {
|
||||
PSA_Post_Type::init_admin();
|
||||
psa_maybe_run_upgrades();
|
||||
PSA_Upgrade::maybe_run();
|
||||
}
|
||||
add_action( 'admin_init', 'psa_admin_init' );
|
||||
|
||||
/**
|
||||
* Decide whether a schema upgrade is needed given a stored version and the
|
||||
* currently-installed code version. Pure function — no side effects, testable
|
||||
* in isolation.
|
||||
*
|
||||
* @param string $stored_version Version string recorded in psa_db_version.
|
||||
* @param string $current_version Version string of the running code.
|
||||
* @return bool True if an upgrade must run; false otherwise.
|
||||
*/
|
||||
function psa_upgrade_needed( $stored_version, $current_version ) {
|
||||
return version_compare( $stored_version, $current_version, '<' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Run database upgrades when the installed code version is newer than the
|
||||
* stored `psa_db_version` option. Keeps users from needing to
|
||||
* deactivate/reactivate the plugin after a schema change.
|
||||
*
|
||||
* Triggered on admin_init so dbDelta runs once per upgrade, not on every
|
||||
* frontend page load.
|
||||
*
|
||||
* Side effects: reads psa_db_version option; on upgrade, calls
|
||||
* PSA_Cost_Tracker::create_table() and writes psa_db_version.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function psa_maybe_run_upgrades() {
|
||||
$stored = get_option( 'psa_db_version', '0.0.0' );
|
||||
if ( ! psa_upgrade_needed( $stored, PSA_VERSION ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Re-run CREATE TABLE via dbDelta — additive schema changes (new columns)
|
||||
// will be applied automatically.
|
||||
PSA_Cost_Tracker::create_table();
|
||||
|
||||
update_option( 'psa_db_version', PSA_VERSION, false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current page is the Echo KB main page.
|
||||
* Uses is_page() which is reliable even in block templates.
|
||||
|
||||
Reference in New Issue
Block a user