The complete Magento upgrade guide

Posted: November 9th, 2012 | Author: | Filed under: Magento, MySQL, PHP | Comments Off on The complete Magento upgrade guide

After having upgraded several Magento sites and having read alot of online upgrade guides, I decided to write my own manual. Many people search for how-to’s for specific Magento versions, but it actually doesn’t really matter what version of Magento you are upgrading. The only difference is that you need to upgrade to 1.4.2 if you are running a version below that. Magento stopped using PEAR as of version 1.4.2.


First of all, backup your production site and create a staging environment. After setting the new database connection in app/etc/local.xml, manually change the website urls in the database:

   `value` = REPLACE(`value`, "//", "//");

Now login to the new staging Admin panel and disable all cache under Cache Management and delete the var/cache and var/session folders in your root directory. It is important to clean up and reduce the size of the database before we start the upgrade process, since large databases take forever to upgrade and corrupt databases can cause headaches when debugging.

If you have the Magento Compiler enabled, run the following commands to remove all compiled files and to disable the include path.

$ php shell/compiler.php disable
$ php shell/compiler.php clear

Make sure that the staging website maintains all proper file and folder permissions. If this is not the case you can reset them by following this guide.

Cleanup the database

Since Magento logs alot, let’s clean up a bit first. Enter the following command to view how much data Magento has been logging until now.

$ shell/log.php status

This displays the size of the `log_` and `report_` tables. If you haven’t enabled Log Cleaning in the Magento Admin (System > Configuration > System > Log Cleaning), this can quickly grow to a couple of hundred MB or maybe some GB’s!
You can manually cleanup these tables by running this command:

$ shell/log.php clean

However there are more tables that can be emptied. You can safely run this query:

TRUNCATE `catalog_compare_item`;
TRUNCATE `dataflow_batch_export`;
TRUNCATE `dataflow_batch_import`;
TRUNCATE `log_customer`;
TRUNCATE `log_quote`;
TRUNCATE `log_summary`;
TRUNCATE `log_summary_type`;
TRUNCATE `log_url`;
TRUNCATE `log_url_info`;
TRUNCATE `log_visitor`;
TRUNCATE `log_visitor_info`;
TRUNCATE `log_visitor_online`;
TRUNCATE `report_viewed_product_index`;
TRUNCATE `report_compared_product_index`;
TRUNCATE `report_event`;
TRUNCATE `sales_flat_quote_item`;

You could also truncate the `core_url_rewrite` table, which can grow pretty large too if you have alot of products and websites/stores-views. Attention: When emptying this you will lose all old url redirects for products, categories, cms pages, etc, but it greatly increases the speed of the upgrade process. Of course it is safe to do this when the website is not yet in production in the first place.

This should leave us with a pretty slimmed down version of the database.

Now reindex the database to be sure

$ php shell/indexer.php reindexall

Clear cache again if Magento somehow managed to create some (you can’t do this enough! :))

$ rm -rf var/cache var/session

Important reminder: do not access the staging website in your browser! As this will initiate the upgrade process too early

Fix corrupt database (don’t skip this step!!)

Check for corrupt structure

It’s possible that the database structure has been changed after the moment is was installed, due to plugins or custom code. Magento has released a tool that can fix corrupted databases: The Magento Database repair tool. It adds missing table columns, foreign keys and indexes. Download the php script and upload it to the root of your staging website. Now create a fresh install of Magento of the same version as your current shop, so we can compare it’s database-structure with our staging database.

Run the tool and see if it makes some changes. If an error occurs, just run the tool again until it says there is nothing more to change.

Check for corrupt data

Besides the structure of tables, it’s also possible that some data is corrupted. I’ve had cases that duplicate order records existed that messed up the upgrade process. Run this query to find out whether there are duplicate records and delete them.

SELECT `increment_id` FROM `sales_flat_order` GROUP BY `increment_id` HAVING COUNT(*)>1;
SELECT `increment_id` FROM `sales_flat_order_grid` GROUP BY `increment_id` HAVING COUNT(*)>1;

More info on this can be found in this thread.

Personally I’ve never had other tables that contained duplicate records.

Upgrade the Magento core files

As mentioned earlier, if you are running Magento 1.4.1.x or lower you need to upgrade to 1.4.2 first.

$ chmod -R 777 lib/PEAR
$ ./pear mage-setup
$ ./pear upgrade -f magento-core/Mage_All_latest-stable
$ chmod 550 ./mage
$ ./mage mage-setup
$ ./mage sync

If pear displays errors, if may have to be upgraded.

$ ./pear channel-update
$ ./pear upgrade --force PEAR

Repeat this step if it shows any errors

The following steps apply to all versions of Magento (1.4.2 and higher) that want to upgrade to the latest stable version (currently Reminder: unfortunately it’s not possible to upgrade to a version other than the latest release. If you really need this, you need to manually download the specific version of Magento and connect the older database to initiate the upgrade process.

View which packages can be upgraded

$ ./mage list-upgrades

Configure Magento to upgrade to the latest stable version, as we do not want a bèta version. (Bèta state is default!)

$ ./mage config-set preferred_state stable

Upgrade all packages! This will also upgrade any plugin installed via SSH if possible. Magento will replace all php files in the app/code/core and in the app/design/base/default map
Custom theme files and php files from app/code/local remain untouched!

$ ./mage upgrade-all --force

The filesystem has now been fully upgraded.

Upgrade the database

Since all package files on the server have been upgraded. Magento will check for version changes when a page is loaded in the browser. All packages will execute the upgrade php scripts which change the database structure. Now open the staging website in your browser and Magento will start the upgrade. You can monitor this progress via SSH:


Depending on your database size, this process can be ready in a few minutes or can take up a few hours. I usually like to refresh the processlist every 10 minutes to see what Magento is up to. If anything goes wrong, you’ll at least know where to look :). It is important not to open the site in another browser during the process, so be sure that no-one else does too.

When the process is finished (no more activity in the mysql processlist), close the browser and clear cache.

$ rm -rf var/cache var/session

Finishing up

To be sure the database upgrade was succesful, run the Magento Database Repair tool again. Now you’ll need a fresh database of the latest Magento version to compare against, so create this first.
If the upgrade went ok, the tool shouldn’t find any differences.

The last step is to reindex everything again!

$ php shell/indexer.php reindexall

The upgrade is now complete! You can safely open the website in the browser and see if everything still works. Remember: All errors are almost always related to corrupt database structure, custom theme files, incompatible plugins or custom code (app/code/community and app/code/local).

IE, SSL and file downloads

Posted: June 21st, 2012 | Author: | Filed under: PHP | Comments Off on IE, SSL and file downloads

A small reminder for everyone developing applications using SSL (Secure Sockets Layer) for data encryption: When downloading files Internet Explorer has to use it’s cache in order to be able to open the file. states:

In order for Internet Explorer to open documents in Office (or any out-of-process, ActiveX document server), Internet Explorer must save the file to the local cache directory and ask the associated application to load the file by using IPersistFile::Load. If the file is not stored to disk, this operation fails.

When Internet Explorer communicates with a secure Web site through SSL, Internet Explorer enforces any no-cache request. If the header or headers are present, Internet Explorer does not cache the file. Consequently, Office cannot open the file.

This means that IE should always be forced to cache the file before opening. In PHP this is achieved by sending the following HTTP headers,:

// Do not use header("Pragma: no-cache");
header("Pragma: private");
// Do not use header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: cache");


Facebook PHP SDK for Canvas and FB Login

Posted: January 10th, 2011 | Author: | Filed under: Facebook, PHP | Tags: , , , | Comments Off on Facebook PHP SDK for Canvas and FB Login

This is a short article on how to best implement the Facebook PHP SDK for two integration methods: Canvas applications and external websites offering Facebook Login to their visitors. The difference is not very well documented in the example provided with the SDK. Read the rest of this entry »

The jQuery/PHP Validator

Posted: April 6th, 2010 | Author: | Filed under: JS, PHP | Comments Off on The jQuery/PHP Validator

Check out our demo which uses the jQuery as well as the PHP validator:

We the developers already know about the famous jQuery validator and about all the wonderful things it is capable of. But we also know that this validation is only on the client side.

What about server side validation on form input? Isn’t that important too? Of course it is. And that is exactly why we’ve come up with the following solution for this issue: The php validator. This validator consists of a class which contains a collection of methods similar to the jQuery validator. This way, we have validation on both client- and server sides using exactly the same rules and error messages.

Our PHP validator is based on the bassistance jQuery validator found at:

Read the rest of this entry »

Thoughts on php’s print_r

Posted: March 25th, 2010 | Author: | Filed under: PHP | Comments Off on Thoughts on php’s print_r

Just a quick quick question: Why does php’s print_r function, which is pretty usefull to all, feature a $return (optional) argument? Not a question about it’s usefulness, but rather the choice to include this into the print_r. I would prefer to see an sprint_r variant behaving like

function sprint_r($sIn){
     return print_r($sIn, true);

to conform to choices made for (s)printf. While on the subject, why not include a fprint_r as well (useful perhaps for logging):

function fprint_r($oFP, $sIn){
     return fwrite($oFP, sprint_r($sIn));

and perhaps a vprint_r to complete the symmetry, though I have no clue as to what it should do :).

__PHP_Incomplete_Class Object

Posted: July 21st, 2009 | Author: | Filed under: PHP | 3 Comments »

Just a quick note, as I could not find a satisfactory answer fast enough googling the web. I got a (not so) nice __PHP_Incomplete_Class Object notice the other day, happily programming until that moment. Such a notice indicates that somewhere (very likely in your session) a class has been unserialized that php does not know how to handle. This means that php knows which class the unserialized data should be, but cannot find the class declaration. It may happen using this type of code:

class SomeClass { ... }
$_SESSION['some_instance']; // which is a SomeClass assigned in some previous request

Depending on the implementation of the class, e.g. use of SPL, this will result in a __PHP_Incomplete_Class Object. To fix this, tell php what the class is/can do before unserializing, i.e. move session_start below the class declaration.

Sideskipping PHP’s Late Static Binding until 5.3

Posted: June 30th, 2009 | Author: | Filed under: PHP | Comments Off on Sideskipping PHP’s Late Static Binding until 5.3

bindingThose of you who have experience with mixing inheritance and static members have undoubtedly run into problems regarding late static binding. When some parent class higher up in the inheritance tree has static members (methods or attibutes), getting these to work with child-classes is impossible, e.g.:

class A {
    private static $sValue = 'show from class A';
    public static function show(){
        echo self::$sValue;
class B extends A {
    private static $sValue = 'show from class B';
B::show(); // echos "show from class A", not "show from class B"

This behaviour is noted as “bug” and will be changed in version 5.3.
Read the rest of this entry »


Posted: June 23rd, 2009 | Author: | Filed under: (X)HTML, JS, PHP | Tags: , , , | Comments Off on HTTP_REFERER in PHP

linkingThis morning I added some new features to a webapplication we are developing. They worked fine in Firefox, so I opened Internet Explorer to see if everything worked in there too. Of course it didn’t…

In webapplications you may need to know which webpage requested the current one. PHP offers an easy way to find the URL of that page: the predefined server variable $_SERVER[‘HTTP_REFERER’]. Unfortunately this variable was not set in IE. So where did it go?

Read the rest of this entry »

Searchresults with misspelled Names

Posted: June 23rd, 2009 | Author: | Filed under: PHP | Comments Off on Searchresults with misspelled Names

search resultsOften, we like to present our users with the best care possible. Unfortunately, when these users fill in the forms in applications, they tend to make type-o’s. This leaves us to correct them and “interpret” what our dear users meant. Obviously, in the interest of time (but more importantly: work), this task should be automated. As I sometimes think it is really difficult to interpret what other people mean, so may computers. This article is about correcting type-o’s in city names.

Read the rest of this entry »

DB-id’s to “readable” URLs (as in tinyurl, youtube)

Posted: June 18th, 2009 | Author: | Filed under: PHP | Comments Off on DB-id’s to “readable” URLs (as in tinyurl, youtube)

eyetestTiny urls come in handy in many different occations, such as twitter, chat messages and emails. For example, sharing this article with your friends could mean sending them the url, which is long. This length could be a problem, e.g. when the link is split over multiple lines in an email: the mail client may not recognize the continued link on the next line. A tiny url can be a solution to this: links to the same article and is much shorter. Several online services are available online, such as or
Read the rest of this entry »