WP 更新插件数据库 table

WP Update plugin DB table

我正在构建一个插件,该插件在激活和停用时会创建和删除数据库,效果很好。

问题出在我尝试为数据库创建更新时 table 它出错并中断。

我有一个名为 class WIP_Forecast 的 class 文件,下面是我的 Class.

中的更新函数

我对插件构建还很陌生,真的很想得到一些帮助来解决这个问题。

global $wip_forecast_db_version, $current_version;

$wip_forecast_db_version = '1.1';
$get_current_version = get_option('wip_forecast_db_version');

class WIP_Forecast
{
    public function __construct()
    {
        // register actions
        add_action( 'plugins_loaded', 'update_db_checker' );            
    }

    public static function activate(){
    // Works
    }
....
    /**
     * Update Plugin DB
     */
    function update_db_checker()
    {
        if ( $get_current_version != $wip_forecast_db_version ) {
            wip_forecast_updates($wip_forecast_db_version);
        }
    }

    function wip_forecast_updates($wip_forecast_db_version)
    {           
        global $wpdb;
        $table_name = $wpdb->prefix . 'wip_forecast';

        if ($get_current_version === 1.1)
        {
            $wpdb->query("ALTER TABLE $table_name ADD COLUMN `count` SMALLINT(6) NOT NULL");
        } else if ($get_current_version === 1.2){
            //$wpdb->query("ALTER TABLE $table_name ADD COLUMN `count` SMALLINT(6) NOT NULL");
        }else{
            //Do Nothing
        }

        update_option('wip_forecast_db_version', $wip_forecast_db_version);
    }
...
    public static function deactivate(){
    // Works
    }
}
if(class_exists('WIP_Forecast'))
{
    // Installation and uninstallation hooks
    register_activation_hook(__FILE__, array('WIP_Forecast', 'activate'));
    register_activation_hook( __FILE__, 'wip_forecast_create_db' );

    register_deactivation_hook(__FILE__, array('WIP_Forecast', 'deactivate'));

    // instantiate the plugin class
    $wip_forecast = new WIP_Forecast();
}
global $wip_forecast_db_version, $get_current_version;

class WIP_Forecast
{
private $wip_forecast_db_version;
private $get_current_version;

public function __construct()
{
    $wip_forecast_db_version = '1.1';
    $get_current_version = get_option('wip_forecast_db_version');

    // register actions
    add_action( 'plugins_loaded', 'update_db_checker' );
}
}