Jump to content

[solved] which version numbers do you use? integer or string?


bernhard
 Share

Recommended Posts

Hi everybody!

As we all know modules can have a version number. It is defined in the module's info array like this:

$info = [
  'title' => 'mPDF Helper Module',
  'version' => '1.0.0',
  'summary' => 'A helper module for the mPDF library',
  'singular' => false,
  'autoload' => false,
  'icon' => 'file-pdf-o',
];

or like this:

$info = [
  'title' => 'mPDF Helper Module',
  'version' => 100,
  'summary' => 'A helper module for the mPDF library',
  'singular' => false,
  'autoload' => false,
  'icon' => 'file-pdf-o',
];

As far as I understand both should be equivalent, but there is a problem when having a version number like this:

'version' => 1013, // result: 1.0.13, not 10.1.3

The solution to that problem could be to use a string:

'version' => '10.1.3',

BUT: When using strings vor the version I cannot compare the currently installed version with the downloaded one. This is a problem for my upgrade module which handles db migrations similar (but a lot more minimalistic) to the migrations module:

<?php namespace ProcessWire;
/**
 * Bernhard Baumrock, baumrock.com
 * MIT
 * 
 * test with tracy console:
 * $up = $modules->get('UpgradesDemo');
 * $up->___upgrade(3,4);
 */
class UpgradesDemo extends WireData implements Module {

  private $from;
  private $to;

  /**
   * module info array
   * this is kept inside the module file to make sure the file changes even when
   * only the version number was changes. otherwise the module file does not change
   * and the system does not recognize the update
   */
  public static function getModuleInfo() {
    return [
      'title' => 'UpgradesDemo',
      'version' => 5,
      'summary' => 'Module to handle upgrades',
      'singular' => true,
      'autoload' => 'template=admin',
      'icon' => 'code-fork',
    ];
  }

  /**
   * execute code on upgrade
   */
  public function ___upgrade($from, $to) {
    $this->from = $from;
    $this->to = $to;

    $this->log("-- Starting upgrade from $from to $to --");

    // option 1
    // $this->execute(1, function() {
    //   $this->log('execute upgrade 1');
    // });

    // option 2
    // include(__DIR__ . '/upgrades/1.php');

    $this->execute(4, function() {
      $this->log('execute upgrade 4: install rockfinder and rockgrid');
      $this->wire->modules->get('RockFinder');
      $this->wire->modules->get('FieldtypeRockGrid');
    });
    
    $this->execute(5, function() {
      $this->log('install AdminThemeUikit');
      $this->wire->modules->get('AdminThemeUikit');
    });
    
    $this->log("-- Upgrade from $from to $to finished --");
  }

  /**
   * execute the callback if the version matches
   */
  private function execute($version, $callback) {
    // execute callback if version matches
    if($this->from < $version AND $this->to >= $version) {
      $callback->__invoke();
    }
  }

  /**
   * logging shortcut
   */
  private function log($str) {
    $this->log->save('upgrades', $str);
  }

}

So for example if I had version 3 of this module installed and I upgraded it to version 5 the code would run upgrade #4 and #5.

How do you handle that? Thanks for your ideas ? 

Edit: It seems that the modules directory treats 100 as 100 - so there is a lot of inconsistency imho.

Link to comment
Share on other sites

2 hours ago, bernhard said:

BUT: When using strings vor the version I cannot compare the currently installed version with the downloaded one.

Why not? Does version_compare() not work for you? Or extracting major and minor parts separately?

Or if you only need to have consistency for modules under your control, why not define major/minor versions explicitly?

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

3 hours ago, bernhard said:

BUT: When using strings vor the version I cannot compare the currently installed version with the downloaded one.

Hi Bernhard,

I hit a similar issue with the ReleaseNotes module. If you have a look from line 203 on for about 20 lines, you can see how my module handles it. I hope that gives you something to work with. (Ignore line 214 - it's now unused and I'll remove it from the next release.)

Best wishes,
Steve

PS> Thank-you for all the amazing modules you've produced!

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...