Jump to content

Recommended Posts

Hey guys,

 

I registered on another forum hot.... they never activated my account or help me so here it goes. I hope you guys are friendly :)

 

I am basically trying to return an array with all the versions that the current software hasn't updated too. (I am making a software update script)

I have built everything else including an API etc.. I have 12 years php knowlegde but this is bugging me and I have never come across this before.

 

So I have an array:

 

private $current_versions = array(

'1.0.0',
'1.0.1',
'1.0.2',
'1.1.0',
'1.1.1',
'1.1.2'

);

 

This array is all of the versions for the software.

 

I have two other variables:

 

$current_version = '1.0.2';
$new_version = '1.1.2';

 

I am basically trying to get the difference. The current version the software is running is: 1.0.2 and the current actual version of the software is: 1.1.2. I need to return an array with just the versions the software is missing, so that I can run the update based on them versions only; in order.

 

Thanks for your help and I will be taking part in this forum full time :)

Link to comment
https://forums.phpfreaks.com/topic/272526-software-semantic-versioning/
Share on other sites

I'd do away with the array all together, and just build the upgrade script into a switch-case with fall-through. I assume you're using a version constant in the application, which is all you need to swtich on.

Then you can simply just add another case (at the bottom) for each new version of the software you're releasing, and the switch statement will take care of all the necessary upgrades. No matter which version is running. ;)

switch (VERSION_NUMBER) {
   case '1.0.2':
       // Upgrade from 1.0.2 to 1.1.0
   case '1.1.0':
       // Upgrade from 1.1.0 to 2.0.0
   case '2.0.0':
       // Upgrade from 2.0.0 to 2.0.1
   case '3.0.4':
       // Upgrade from previous to current version.
       break;
   default:
       // Show error about unknown version.

Quite simple, as you can see. ;)

Edited by Christian F.

Christian, Thanks man. I never knew you could do it like that with a switch statement. I always thought it read everything at once doing switches like that. Thats awesome. Thanks :)

 

It doesn't do everything at once, the parser reads it line by line. When it finds a matching case it begins to execute the code thereafter until it reaches a break statement. A fall-through, as Christian mentioned, is when another case is found before a break is found and the code in this new case is then executed; you can have as many or as few as you like.

Edited by CPD

Your update script should be several components.

 

  1. Create a backup (files and database).
  2. Put the website into maintenance mode (and make sure for proper headers for search engines) If your app supports CLI this could be done through it:
    $ php app/console admin:config:set --maintenance=true --user=upgrade --pass=upgradepass


  3. Query your update server for the updates (http://updates.server/upgrade?from=1.0.1). This would return a zip/tgz file with the changes. If you use a VCS then you could simply query for a diff between tag X (current) and Y (latest) or simply the master:HEAD
  4. Unpack

 

Once you unpack this could contain a few things.

 

  1. The replacement files (duh!)
  2. A database upgrade script (ALTER/DROP TABLE ..) this is often forgotten when people develop software, they use a VCS yet they don't version their database.
  3. A cleanup script, whatever mess you made, clean it up!
  4. A test script that verifies all has gone well
  5. Optional, a PHP script that does extra steps, like creating a new cronjob, etc..

 

The update should be done in a transaction, meaning that all must pass or none. Make sure that when something fails everything is rolled back properly without breaking stuff!

Edited by ignace
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.