This tutorial explains how Map and Entity tables can be altered in Gallery 2.
Before following this tutorial, you should have a basic understanding how to create your own tables in Gallery 2 and what Map and Entity tables are.
Related documentation: Development Documentation for Gallery 2
This section covers how to update the table definition (CREATE TABLE SQL).
If you want to deploy your module, you'll want that users of a previous version of your module can easily upgrade to the new version of your module and part thereof is altering the existing table structure to the new definition. This is done by ALTER TABLE statements.
In Gallery 2, you don't have to write your own ALTER TABLE SQL for all the supported database systems. Instead, you need to write a XML description of the ALTER TABLE statement. Based on the XML description, Gallery will then generate the necessary SQL for all supported database systems for you.
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE change SYSTEM "../../../../../lib/tools/dtd/DatabaseChangeDefinition2.0.dtd"> <change> <table-name>Item</table-name> <schema-from> <schema-major>1</schema-major> <schema-minor>1</schema-minor> </schema-from> <schema-to> <schema-major>1</schema-major> <schema-minor>2</schema-minor> </schema-to> <add> <column> <column-name>renderer</column-name> <column-type>STRING</column-type> <column-size>MEDIUM</column-size> </column> </add> </change>
After having changed the Map / Entity definition and having created an A_[table-name]_[major].[minor].xml file for upgrades, you're now ready to generate the new SQL.
In the command line, you enter:
cd modules/[your module]/classes/ make
Verify that the SQL has been generated correctly by looking at modules/[your module]/classes/GalleryStorage/schema.tpl. The CREATE TABLE statement should now reflect the new table structure and there should be a new ALTER TABLE statement too.
New installations of your module should work fine at this point. The table is created with the new table structure at module installation time.
But for module upgrades from module versions with older table structures, you need to add a tiny bit of module upgrade code to instruct it when to execute the ALTER TABLE code.
case '1.1.5':
case '1.1.5': global $gallery; $storage =& $gallery->getStorage(); $ret = $storage->configureStore($this->getId(), array('GalleryFooMap:1.1')); if ($ret) { return $ret; } case 'end of upgrade path':
That's it!
You may want to populate / copy data in your upgrade code as well. That's up to you.