Jump to content


Photo

Software Semantic Versioning

php

  • Please log in to reply
9 replies to this topic

#1 wotw

wotw

    Member

  • Members
  • PipPip
  • 10 posts

Posted 30 December 2012 - 04:35 PM

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 :)

#2 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 30 December 2012 - 04:48 PM

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. ;)
Keeping it simple.

#3 wotw

wotw

    Member

  • Members
  • PipPip
  • 10 posts

Posted 30 December 2012 - 04:59 PM

Can you do me a little sample? I am kind of lost at what you mean mate. Thanks :)

#4 wotw

wotw

    Member

  • Members
  • PipPip
  • 10 posts

Posted 30 December 2012 - 05:00 PM

Say for instance. I release version 3.0.4 But the user is still on version 1.0.2. It needs to update them all in order.

#5 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 30 December 2012 - 05:10 PM

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., 30 December 2012 - 05:10 PM.

Keeping it simple.

#6 wotw

wotw

    Member

  • Members
  • PipPip
  • 10 posts

Posted 30 December 2012 - 05:16 PM

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 :)

#7 cpd

cpd

    ¬_¬

  • Members
  • PipPipPip
  • 881 posts
  • LocationLondon, UK

Posted 30 December 2012 - 06:34 PM

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, 30 December 2012 - 06:34 PM.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

"One of my most productive days was throwing away 1000 lines of code."

#8 wotw

wotw

    Member

  • Members
  • PipPip
  • 10 posts

Posted 30 December 2012 - 08:03 PM

But doing a software update this way is the very best way to do it?

#9 cpd

cpd

    ¬_¬

  • Members
  • PipPipPip
  • 881 posts
  • LocationLondon, UK

Posted 31 December 2012 - 06:06 AM

Its a method of doing it. Best/worst methods are argued from person to person as opinion varies. Just remember there's usually many ways to carry out individual tasks all of which are just as good as the next.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

"One of my most productive days was throwing away 1000 lines of code."

#10 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,208 posts
  • LocationBelgium

Posted 31 December 2012 - 07:37 AM

Your update script should be several components.

  • Create a backup (files and database).
  • 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
  • Query your update server for the updates (http://updates.serve...rade?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
  • Unpack

Once you unpack this could contain a few things.

  • The replacement files (duh!)
  • 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.
  • A cleanup script, whatever mess you made, clean it up!
  • A test script that verifies all has gone well
  • 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, 31 December 2012 - 07:40 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com