2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
14 // Utility class contains basic database upgrade routines.
17 var $previous; // previous database version
18 var $version; // version after upgrade
19 var $description; // short patch description
21 var $sql; // basic sql file
24 var $backup; // pre-upgrade backup filename
26 var $errors = array();
27 var $ma_upgrade_time = 300;
33 include $path_to_root."/config_db.php";
35 $this->companies = $db_connections;
37 return $this->companies;
41 Collect/log messages generated during upgrade process.
43 function log_error($msg, $type='Error')
46 $this->errors[] = $msg;
47 error_log(sprintf('[%s] %s', $type, $msg));
52 Pre-install maintenance: login to company, open upgrade log, make a backup
54 function pre_install($company)
58 $this->cur_company = $company;
59 $this->errors = array();
62 $this->save_log = ini_set('error_log', dirname(__FILE__).'/../../tmp/upgrade.'.$this->cur_company.'.log');
63 $this->log_error(sprintf(_("Upgrade started for company %s."), $this->cur_company), 'Info');
65 if (!set_global_connection($this->cur_company))
66 return $this->log_error(_("Cannot connect to company database."));
68 $cur_ver = get_company_pref('version_id', true);
69 if ($cur_ver != $this->previous)
70 return $this->log_error(sprintf(_("Cannot upgrade company %s: database version is incompatible ('%s' instead of '%s')."),
71 $this->cur_company, $cur_ver, $this->previous));
73 if (!$this->prepare()) // fetch params, perform additional checks (if any)
77 return true; // skip security backup if database content is not changed
79 $this->backup = db_backup($this->companies[$this->cur_company], 'no', 'Security backup before upgrade',
80 $SysPrefs->backup_dir($this->cur_company));
83 return $this->log_error(_("Security backup failed."));
85 $this->log_error(sprintf(_("Security backup in file %s done."), $this->backup), 'Info');
90 Basic install procedure using sql file.
92 function sql_install($company, $force=false)
96 if ($this->sql != '') // perform basic upgrade operations defined in sql file
100 if ($result === true)
101 $result = db_import($path_to_root. '/sql/'.$this->sql, $this->companies[$company],
102 $force, true, false, true);
104 if ($result !== true)
106 if (is_array($result))
108 foreach($result as $err)
109 $this->log_error($err[1] . ':'. $err[0]);
112 $this->log_error($result);
113 unset($this->backup); // prevent restore (database was not touched due to other errors)
122 Post install procedures: update database version, or restore databse from backup file in case of errors
124 function post_install($result=true)
128 if ($result !== true)
132 if (!set_global_connection($this->cur_company)) // reset connection to clear encoding
133 return $this->log_error(_("Cannot connect to company database for database restore."));
135 set_time_limit($this->max_upgrade_time);
136 $result = db_import($this->backup, $this->companies[$this->cur_company], true, false);
138 $this->log_error(_("Upgrade failed. Original database content restored successfully."), 'Info');
140 $thi->log_error(sprintf(_("Database restore operation failed. Original database content is in %s file."), $this->backup));
141 $this->post_fail($this->cur_company);
144 update_company_prefs(array('version_id' => $this->version));
147 $this->log_error(sprintf(_("Upgrade for company %s finished."), $this->cur_company), 'Info');
149 set_global_connection();
150 ini_set('error_log', $this->save_log);
159 Main routine for single company upgrade.
161 function upgrade_company($comp, $force=false)
163 $result = $this->pre_install($comp) && $this->sql_install($comp, $force) && $this->install($comp, $force);
165 $this->post_install($result);
167 return count($this->errors) == 0;
171 Additional version specific php/sql upgrade procedures.
172 This procedure is performed after basic sql upgrade script is run.
174 function install($company, $force=false)
179 Optional cleanup procedure.
180 This procedure is run in case of upgrade failure, before the backup is restored.
182 function post_fail($company)
187 Present upgrade parameters to administrator
188 This function presents upgrade choices, after selection company to be upgraded.
190 function show_params($comp)
195 Fetch & check upgrade parameters, check additional upgrade pre-conditions.
196 This function is run after successfull switching to target database connection.
206 Return databases status info.
208 function get_site_status($connections)
214 foreach($connections as $i => $conn)
216 $info[$i]['status'] = set_global_connection($i) !== false;
218 $info[$i]['name'] = $conn['name'];
219 $info[$i]['table_set'] = $conn['host'].'/'.$conn['dbname'].':'.$conn['tbpref'].'*';
220 if ($info[$i]['status'])
222 $info[$i]['version'] = get_company_pref('version_id');
225 set_global_connection();
226 $SysPrefs->refresh();
232 Creates table of installer objects sorted by applicable db scheme version.
234 function get_installers()
236 global $path_to_root;
238 $patchdir = $path_to_root."/sql/";
240 $datadir = @opendir($patchdir);
244 while(false !== ($fname = readdir($datadir)))
245 { // check all php files but index.php
246 if (!is_dir($patchdir . $fname) && ($fname != 'index.php')
247 && stristr($fname, '.php') != false && $fname[0] != '.')
250 include_once($patchdir . $fname);
251 if (isset($install)) // add installer if found
252 $upgrades[$install->previous] = $install;
255 ksort($upgrades); // sort by file name