Q. How and where does Magento differentiate between install, upgrade, and downgrade scripts?
In Mage_Core_Model_App::_initModules(), calls Mage_Core_Model_Resource_Setup::applyAllUpdates(), and it then calls applyUpdates() to check version number from config.xml.. if lower version number, it rollbacks; if greater version number, it upgrades. The config.xml version number is checked against core_resource table’s version of the module.