Archived

Difference between revisions of "Managing Component Updates (Update SQL files)"

From Joomla! Documentation

(page update for series)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{version/tutor|1.6,1.7,2.5}}{{Chunk:Managing Component Updates - Contents}}{{review}}
+
{{version/tutor|2.5}}{{Chunk:Managing Component Updates - Contents}}
 
== Update SQL files ==
 
== Update SQL files ==
The update SQL file(s) are located in a directory that is listed in the manifest file:
+
The update SQL file(s) are ideally located in a directory under <code>/administrator/components/com_YOURCOMPONENT/sql/updates/mysql</code>. This folder needs to be listed in the manifest file:
  
 
<source lang="xml">
 
<source lang="xml">
 
<update>  
 
<update>  
 
<schemas>  
 
<schemas>  
<schemapath type="mysql">sql/updates</schemapath>  
+
<schemapath type="mysql">sql/updates/mysql</schemapath>  
 
</schemas>  
 
</schemas>  
 
</update>
 
</update>
 
</source>
 
</source>
  
There is an SQL file for each component version.  Each file name must match the version string in the manifest file for that version. Joomla uses this string to determine which SQL files(s) to execute, and in what order they will be executed.
+
Observe that we create a folder for each database engine that the component supports. This example will only show how it's done for MySQL. For other engiens, the procedure is similar.
 +
 
 +
There is an SQL file for each component version.  Each file name must match the version string in the manifest file for that version. Joomla uses this string to determine which SQL files(s) to execute, and in what order they will be executed.
 +
 
 +
Don't forget to update your component manifest file, by adding the <code>sql</code> folder under the admin folders.
 +
 
 +
<blockquote>
 +
<folder>sql</folder>
 +
</blockquote>
  
 
''Important Note:'' These files are also used to set the version number in the <code>#__schemas</code> table. This version number must be present in the current version of the component in order for the new SQL files to be run during the update. For example, if you have version 1.0 and are updating to version 1.1, the <code>1.1.sql</code> file will not be executed if there was no <code>1.0.sql</code> file in the 1.0 release. For this reason, it is good practice to have a SQL update file for each version, even if there is no SQL change in that version. You can just use an empty file or a file with a comment line.
 
''Important Note:'' These files are also used to set the version number in the <code>#__schemas</code> table. This version number must be present in the current version of the component in order for the new SQL files to be run during the update. For example, if you have version 1.0 and are updating to version 1.1, the <code>1.1.sql</code> file will not be executed if there was no <code>1.0.sql</code> file in the 1.0 release. For this reason, it is good practice to have a SQL update file for each version, even if there is no SQL change in that version. You can just use an empty file or a file with a comment line.
  
 
Joomla uses the PHP ''version_compare'' function to execute the correct SQL files..  Familiarize yourself with this function.  Your version strings need to adhere to the conventions of this function.
 
Joomla uses the PHP ''version_compare'' function to execute the correct SQL files..  Familiarize yourself with this function.  Your version strings need to adhere to the conventions of this function.
 +
 
For example, the SQL files for version 1.3 of this example include:
 
For example, the SQL files for version 1.3 of this example include:
 
<blockquote>
 
<blockquote>
Line 52: Line 61:
 
== Contributors ==
 
== Contributors ==
 
*[[User:sm990|Kim Eckert]]
 
*[[User:sm990|Kim Eckert]]
 +
*[[User:vdespa|Valentin Despa]]
  
[[Category:Joomla! 1.6]]
 
[[Category:Joomla! 1.7]]
 
 
[[Category:Joomla! 2.5]]
 
[[Category:Joomla! 2.5]]
 
[[Category:Component Development]]
 
[[Category:Component Development]]

Latest revision as of 17:21, 26 April 2022

This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

<sidebar>

  • Managing Component Updates
    • J2.5:Managing Component Updates|Overview
    • J2.5:Managing Component Updates (Manifest file)|Manifest file
    • J2.5:Managing Component Updates (Script.php)|Script.php
    • J2.5:Managing Component Updates (Update SQL files)|Update SQL files
    • J2.5:Managing Component Updates (Component release files)|Component release files
    • J2.5:Managing Component Updates (How to use this example)|How to use this example
  • Help
    • JDOC:How to Contribute to Joomla! Documentation|Contribute to Joomla! Docs
    • JDOC:Documentation_Translators|Translate Joomla! Docs
    • Help:Cheatsheet|Editing Help
    • Sandbox|Play in the Sandbox
    • JDOC:Wiki_policy|JDOC's Policies
    • JEDL|Documentation License
    • helppage|More Help

</sidebar>

Update SQL files[edit]

The update SQL file(s) are ideally located in a directory under /administrator/components/com_YOURCOMPONENT/sql/updates/mysql. This folder needs to be listed in the manifest file:

<update> 
	<schemas> 
		<schemapath type="mysql">sql/updates/mysql</schemapath> 
	</schemas> 
</update>

Observe that we create a folder for each database engine that the component supports. This example will only show how it's done for MySQL. For other engiens, the procedure is similar.

There is an SQL file for each component version. Each file name must match the version string in the manifest file for that version. Joomla uses this string to determine which SQL files(s) to execute, and in what order they will be executed.

Don't forget to update your component manifest file, by adding the sql folder under the admin folders.

<folder>sql</folder>

Important Note: These files are also used to set the version number in the #__schemas table. This version number must be present in the current version of the component in order for the new SQL files to be run during the update. For example, if you have version 1.0 and are updating to version 1.1, the 1.1.sql file will not be executed if there was no 1.0.sql file in the 1.0 release. For this reason, it is good practice to have a SQL update file for each version, even if there is no SQL change in that version. You can just use an empty file or a file with a comment line.

Joomla uses the PHP version_compare function to execute the correct SQL files.. Familiarize yourself with this function. Your version strings need to adhere to the conventions of this function.

For example, the SQL files for version 1.3 of this example include:

1.0.sql
  (empty) OR
  # Dummy SQL file to set schema version to 1.0 so next update will work

1.1.sql
  UPDATE `#__democompupdate_mytable` SET myvalue='Version 1.1 text' WHERE id=1;

1.1.1.sql
  UPDATE `#__democompupdate_mytable` SET myvalue='Version 1.1.1 text' WHERE id=1;

1.2.sql
  INSERT INTO `#__democompupdate_mytable` (id,myvalue) VALUES (2,'Version 1.2 text');

1.2.1.sql
  UPDATE `#__democompupdate_mytable` SET myvalue='Version 1.2.1 text' WHERE id=2;

1.3.sql
  INSERT INTO `#__democompupdate_mytable` (id,myvalue) VALUES (3,'Version 1.3 text');

This example has complete install files for component versions 1.0 (initial), 1.1.1 and 1.3. Update SQL files are included for additional incremental component versions (1.1, 1.2 and 1.2.1) between these three versions. These extra versions illustrate the process for updates that span more than one incremental version.

  • When updating from version 1.0 to version 1.1.1, both files 1.1.sql and 1.1.1.sql are executed. The SQL commands update the value of the first, and only, row in the database table. The command in 1.1.1.sql over-writes the value that was written by 1.1.sql.
  • When updating from version 1.1.1 to version 1.3, files 1.2.sql, 1.2.1.sql and 1.3.sql are executed. The commands in 1.2.sql and 1.3.sql each create a new row in the database table.
  • When updating from version 1.0 directly to 1.3, files 1.1.sql, 1.1.1.sql, 1.2.sql, 1.2.1.sql and 1.3.sql are all executed. You can see the cumulative effect of the table changes in the component front-end.

Joomla executes the database updates in the sequence that would occur, as if the user had updated each version separately. This would be necessary if an incremental database update depended on the correct update from the previous version. An example would be: if a modification of a table value during one update depended on the addition of a new table field during a previous update.

Joomla uses the PHP function version_compare to define the execution order of the SQL files. The sequencing of your component version string must adhere to that defined by the version_compare function.

Contributors[edit]