ibiza, june 6 th 2011 advanced database install scripts
TRANSCRIPT
Ibiza, June 6th 2011
Advanced Database Install Scripts
Presentation Summary
1. What Are They and Why Are They Awesome?
2. Your First Magento Module Install Script
3. Upgrade Scripts
4. Combining Upgrade and Install Scripts
5. Utilizing The Installer Class
6. What Magento Is Doing Behind The Scenes
7. Tips & Tricks & Good Resources
Advanced Database Install Scripts
My Background
Jay El-Kaake, CANADA
• Worked with Magento in BETA stages
• Previous history at TELUS Inc, etc
• Developed Free Enhanced Admin Grid ext
• CEO of WDCA (WDCA.ca)
• Better Store Search (BetterStoreSearch.com)
• Sweet Tooth (SweetToothRewards.com)
Advanced Database Install Scripts
1. What Are They and Why Are They Awesome?
What are they?
• run code when the module first runs
• run code when the module updates
• elegantly build the database architecture
Advanced Database Install Scripts
Why are they awesome?
• Couples database architecture with software architecture
• Allows consistent migration when transporting Magento modules
1. Make sure you module is ready to go, but has never run on this store.
2. Add config.xml entry:
1. Set your version number
2. …
Advanced Database Install Scripts2. Your First Magento Module Install Script
. . .<modules> <TBT_Producthistory> <version>1.0.0.0</version> </TBT_Producthistory> </modules>. . .
For this example we will use producthistory_setup and the TBT_Producthistory module at v1.0.0.0
Watch out: Magento only recognized version numbers >= 3 decimals!
1. Make sure you module is ready to go, but has never run on this store.
2. Add config.xml entry:
1. Set your version number
2. Set the resource setup class/connection
3. Define entities
Advanced Database Install Scripts2. Your First Magento Module Install Script
. . .<modules> <TBT_Producthistory> <version>1.0.0.0</version> </TBT_Producthistory> </modules>. . .
. . .<global> <models> <producthistory> <class>TBT_Producthistory_Model</class> <resourceModel>producthistory_mysql4</resourceModel> </producthistory> <producthistory_mysql4> <class>TBT_Producthistory_Model_Mysql4</class> <entities> <revision> <table>producthistory_revision</table> </revision> </entities> </producthistory_mysql4> </models>. . .
1. Make sure you module is ready to go, but has never run on this store.
2. Add config.xml entry
3. In your module folder, add app/code/community/TBT/Producthistory/sql/producthistory_setup/mysql4-install-1.0.0.0.php
• Runs if v1.0 is installed
Advanced Database Install Scripts2. Your First Magento Module Install Script
mysql4-install-1.0.0.0.php may look like this:
Advanced Database Install Scripts2. Your First Magento Module Install Script
$installer->run("CREATE TABLE IF NOT EXISTS `{$this->getTable('producthistory_revision')}` ( `producthistory_revision_id` int(11) NOT NULL AUTO_INCREMENT, `store_id` int(11) DEFAULT NULL, `product_id` int(11) DEFAULT NULL, `revision_date` datetime DEFAULT NULL, `data_hash` text, PRIMARY KEY (`producthistory_revision_id`), UNIQUE KEY `producthistory_revision_id` (`producthistory_revision_id`)) ENGINE=MyISAM DEFAULT CHARSET=UTF8;");
$installer = $this;$installer->startSetup();
$installer->endSetup();
1. Make sure you module is ready to go, but has never run on this store.
2. Add config.xml entry
3. In your module folder, add app/code/community/TBT/Producthistory/sql/producthistory_setup/mysql4-install-1.0.0.0.php
4. Run the extension and see it go!
Advanced Database Install Scripts2. Your First Magento Module Install Script
Time for an EPIC demo…
1. Make sure you module is ready to go, but has never run on this store.
2. Add config.xml entry
3. In your module folder, add app/code/community/TBT/Producthistory/sql/producthistory_setup/mysql4-install-1.0.0.0.php
4. Run the extension and see it go!
Advanced Database Install Scripts2. Your First Magento Module Install Script
Time for an EPIC demo…
• Very similar, just use “upgrade” instead of install and set both versions
• Naming: mysql4-upgrade-1.0.0.0-1.1.0.0.php
• This will run if module version is upgraded from v1 to v1.1.0.0
Advanced Database Install Scripts3. Upgrade scripts
Time for (another) EPIC Demo…
• Scripts are run in sequential order, starting with biggest install.
Advanced Database Install Scripts4. Combining Upgrade and Install Scripts
Example A: What order would these go in when installing v1.3?
• mysql4-install-1.0.0.0.php
• mysql4-upgrade-1.0.0.0-1.1.0.0.php
• mysql4-upgrade-1.1.0.0-1.3.0.0.php
• Scripts are run in sequential order, starting with biggest install.
Advanced Database Install Scripts4. Combining Upgrade and Install Scripts
Example A: What order would these go in for installing v1.3?
1. mysql4-install-1.0.0.0.php
2. mysql4-upgrade-1.0.0.0-1.1.0.0.php
3. mysql4-upgrade-1.1.0.0-1.3.0.0.php
• Scripts are run in sequential order, starting with biggest install.
Advanced Database Install Scripts4. Combining Upgrade and Install Scripts
Example B: What order would these go in for installing v1.3?
• mysql4-install-1.0.0.0.php
• mysql4-install-1.1.0.0.php
• mysql4-upgrade-1.0.0.0-1.1.0.0.php
• mysql4-upgrade-1.1.0.0-1.3.0.0.php
• Scripts are run in sequential order, starting with biggest install.
Advanced Database Install Scripts4. Combining Upgrade and Install Scripts
Example B: What order would these go in for installing v1.3?
mysql4-install-1.0.0.0.php
mysql4-upgrade-1.0.0.0-1.1.0.0.php
1. mysql4-install-1.1.0.0.php
2. mysql4-upgrade-1.1.0.0-1.3.0.0.php
• Instead of putting core setup functions in the sql files
1. Specify Install class in config.xml
Advanced Database Install Scripts5. Utilizing the installer class
<resources> <!-- ... --> <producthistory_setup> <setup> <module>TBT_Producthistory</module> <class>TBT_Producthistory_Model_Resource_Mysql4_Setup</class> </setup> <connection> <use>core_setup</use> </connection> </producthistory_setup> <!-- ... --></resources>
2. Create classExample: app/…/Producthistory/Model/Resource/Mysql4/Setup.php
Advanced Database Install Scripts6. What Magento Is Doing Behind The Scenes
• Magento core_resources table tracks versions installed
mysql> select * from core_resource;+-------------------------+---------+| code | version |+-------------------------+---------+| adminnotification_setup | 1.0.0 | | admin_setup | 0.7.1 | | amazonpayments_setup | 0.1.2 | | api_setup | 0.8.1 | | backup_setup | 0.7.0 | | bundle_setup | 0.1.7 | | catalogindex_setup | 0.7.10 | | cataloginventory_setup | 0.7.5 | | catalogrule_setup | 0.7.7 | ….| producthistory | 1.0.0.0 |
• You can remove an entry to force DB scripts to re-run
Subscribe to Alan Storm’s Blog @ http://alanstorm.com/magento_setup_resources
Subscribe to our WDCA Blog @ www.wdca.ca/blog
E-mail me at [email protected] if you have any questions
Download the 3-hour developed hack-a-thon extension here:
http://www.wdca.ca/downloads/producthistory.zip
Advanced Database Install Scripts7. Tips & Tricks & Good Resources
Happy DB-installing with Magento!