Pages

Wednesday, January 11, 2012

Database Administration Redux

Some time ago I wrote a post describing an unorthodox method of leveraging the SugarCRM framework to perform database administration duties. It sounds odd, but it is quite helpful for scenarios where one has severely limited access to the server hosting the SugarCRM instance.

While the technique works, its potential dangers make it a bit unattractive. One such danger is that syntax errors in the SQL query to be executed can cause the entire SugarCRM instance to become inaccessible. Coupled with limited access to the server, this danger alone is reason enough to be extra careful. Another high risk danger is the possibility of inadvertently executing the corresponding query more than once, as it would execute every time a specific menu option was selected.

These dangers have encouraged me to look for alternative techniques that would yield similar functionality and at the same time, reduce or eliminate the aforementioned dangers. As it turns out, a minor modification to the previous method accomplishes this goal.

One of the features of the Module Loader install packages is the ability to execute SQL queries when a module is installed. This process occurs automatically, assuming certain guidelines are met, such as including a specifically named file within the package.

The advantage of this approach is that the query only gets executed once, at the time that the module is installed, effectively eliminating the danger of accidentally executing the query again. More importantly, syntax errors and the like do not cause the system to become inaccessible. Such problems simply generate an error and install process is deemed unsuccessful. One is then free to correct the errors without further complications.

What do we need to do to take advantage of this approach?

First, we need to make a file named pre_install.php. Within it, we must define a method by the name of pre_install(). The code placed within that method is automatically executed when the module is installed.

The contents of this file are governed by the GNU General Public License (GPL).A copy of said license is available here: http://www.gnu.org/copyleft/gpl.htmlThis code is provided AS IS and WITHOUT WARRANTY OF ANY KIND.*************************************/

function pre_install()

{

require_once('modules/Administration/Administration.php');

$focus = new Administration();

$query = "DELETE FROM emails WHERE created_by = '1001'; ";

$focus->db->query($query, true);

}

?>

The code that you see within the pre_install() method does two things for us: one, it establishes a connection to the database via $focus and two, it executes the SQL query, via $focus->db->query(). Should we have a need to execute more than one query, we can simply alter the $query variable accordingly and then call query() again, i.e. repeat the last two lines of code, but with a different SQL query.

Side note: one can execute any PHP code one would like within this method, it is not limited to database operations.

After creating the file, the other important step is to store it in a specially named folder within the structure of the package. The folder in which you need to place it must be named scripts.

SugarCRM will know to look for the pre_install.php file in that folder and automatically execute the code in the pre_install() method. There is no need to add any special instructions to the manifest file or the like, but it is posted below:

The contents of this file are governed by the GNU General Public License (GPL).A copy of said license is available here: http://www.gnu.org/copyleft/gpl.htmlThis code is provided AS IS and WITHOUT WARRANTY OF ANY KIND.*************************************/

Now all you need to do to use it is zip the files as you would any other Module Loader package and install it via the Module Loader. The SQL query will automatically execute during the install process.

Related files are available for download from my personal site, via this link: