Advanced Database Install Scripts

Download Report

Transcript Advanced Database Install Scripts

Advanced Database Install Scripts
Ibiza, June 6th 2011
Presentation Summary
Advanced Database Install Scripts
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
My Background
Advanced Database Install Scripts
Jay El-Kaake, CANADA
[email protected]
• 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)
1. What Are They and Why Are They Awesome?
Advanced Database Install Scripts
What are they?
• run code when the module first runs
• run code when the module updates
• elegantly build the database architecture
Why are they awesome?
• Couples database architecture with software
architecture
• Allows consistent migration when transporting
Magento modules
2. Your First Magento Module Install Script
Advanced Database Install Scripts
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. …
For this example we will use
producthistory_setup and the
TBT_Producthistory module at
v1.0.0.0
. . .
<modules>
<TBT_Producthistory>
<version>1.0.0.0</version>
</TBT_Producthistory>
</modules>
. . .
Watch out: Magento only
recognized version numbers >= 3
decimals!
Advanced Database Install Scripts
2. Your First Magento Module Install Script
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
. . .
<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>. . .
2. Your First Magento Module Install Script
Advanced Database Install Scripts
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/mysql4install-1.0.0.0.php
•
Runs if v1.0 is installed
2. Your First Magento Module Install Script
Advanced Database Install Scripts
mysql4-install-1.0.0.0.php may look like this:
$installer = $this;
$installer->startSetup();
$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->endSetup();
2. Your First Magento Module Install Script
Advanced Database Install Scripts
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/mysql4install-1.0.0.0.php
4. Run the extension and see it go!
Time for an EPIC demo…
2. Your First Magento Module Install Script
Advanced Database Install Scripts
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/mysql4install-1.0.0.0.php
4. Run the extension and see it go!
Time for an EPIC demo…
3. Upgrade scripts
Advanced Database Install Scripts
• 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
Time for (another) EPIC Demo…
4. Combining Upgrade and Install Scripts
Advanced Database Install Scripts
• Scripts are run in sequential order, starting with biggest install.
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
4. Combining Upgrade and Install Scripts
Advanced Database Install Scripts
• Scripts are run in sequential order, starting with biggest install.
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
4. Combining Upgrade and Install Scripts
Advanced Database Install Scripts
• Scripts are run in sequential order, starting with biggest install.
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
4. Combining Upgrade and Install Scripts
Advanced Database Install Scripts
• Scripts are run in sequential order, starting with biggest install.
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
5. Utilizing the installer class
Advanced Database Install Scripts
• Instead of putting core setup functions in the sql files
1. Specify Install class in config.xml
<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 class
Example: app/…/Producthistory/Model/Resource/Mysql4/Setup.php
6. What Magento Is Doing Behind The Scenes
Advanced Database Install Scripts
• 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
7. Tips & Tricks & Good Resources
Advanced Database Install Scripts
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
Happy DB-installing with Magento!