2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 require_once('WEB-INF/config.php');
30 require_once('WEB-INF/lib/common.lib.php');
31 require_once('initialize.php');
32 import('ttUserHelper');
33 import('ttTaskHelper');
35 function setChange($sql) {
36 print "<pre>".$sql."</pre>";
37 $mdb2 = getConnection();
38 $affected = $mdb2->exec($sql);
39 if (is_a($affected, 'PEAR_Error'))
40 print "error: ".$affected->getMessage()."<br>";
42 print "successful update<br>\n";
47 print "Processing...<br>\n";
49 if ($_POST["crstructure"]) {
50 $sqlQuery = join("\n", file("mysql.sql"));
51 $sqlQuery = str_replace("TYPE=MyISAM","",$sqlQuery);
52 $queries = explode(";",$sqlQuery);
53 if (is_array($queries)) {
54 foreach ($queries as $query) {
55 $query = trim($query);
56 if (strlen($query)>0) {
63 if ($_POST["convert5to7"]) {
64 setChange("alter table `activity_log` CHANGE al_comment al_comment BLOB");
65 setChange("CREATE TABLE `sysconfig` (`sysc_id` int(11) unsigned NOT NULL auto_increment,`sysc_name` varchar(32) NOT NULL default '',`sysc_value` varchar(70) default NULL, PRIMARY KEY (`sysc_id`), UNIQUE KEY `sysc_id` (`sysc_id`), UNIQUE KEY `sysc_name` (`sysc_name`))");
66 setChange("alter table `companies` add c_locktime int(4) default -1");
67 setChange("alter table `activity_log` add al_billable tinyint(4) default 0");
68 setChange("alter table `sysconfig` drop INDEX `sysc_name`");
69 setChange("alter table `sysconfig` add sysc_id_u int(4)");
70 setChange("alter table `report_filter_set` add rfs_billable VARCHAR(10)");
71 setChange("ALTER TABLE clients MODIFY clnt_id int(11) NOT NULL AUTO_INCREMENT");
72 setChange("ALTER TABLE `users` ADD `u_show_pie` smallint(2) DEFAULT '1'");
73 setChange("alter table `users` ADD `u_pie_mode` smallint(2) DEFAULT '1'");
74 setChange("alter table users drop `u_aprojects`");
77 if ($_POST["convert7to133"]) {
78 setChange("ALTER TABLE users ADD COLUMN u_lang VARCHAR(20) DEFAULT NULL");
79 setChange("ALTER TABLE users ADD COLUMN u_email VARCHAR(100) DEFAULT NULL");
80 setChange("ALTER TABLE `activity_log` drop `al_proof`");
81 setChange("ALTER TABLE `activity_log` drop `al_charge`");
82 setChange("ALTER TABLE `activities` drop `a_project_id`");
83 setChange("DROP TABLE `activity_status_list`");
84 setChange("DROP TABLE `project_status_list`");
85 setChange("DROP TABLE `user_status_list`");
86 setChange("DROP TABLE `companies_c_id_seq`");
87 setChange("ALTER TABLE projects ADD COLUMN p_activities TEXT");
90 // The update_projects function updates p_activities field in the projects table so that we could
91 // improve performance of the application by using this field instead of activity_bind table.
92 if ($_POST["update_projects"]) {
93 $mdb2 = getConnection();
94 // $sql = "select p_id from projects where p_status = 1 and p_activities is NULL";
95 $sql = "select p_id from projects where p_status = 1";
96 $res = $mdb2->query($sql);
97 if (is_a($res, 'PEAR_Error')) {
98 die($res->getMessage());
100 // Iterate through projects.
101 while ($val = $res->fetchRow()) {
102 $project_id = $val['p_id'];
104 // Get activity binds for project (old way).
105 // $sql = "select ab_id_a from activity_bind where ab_id_p = $project_id";
106 $sql = "select ab_id_a, a_id, a_name from activity_bind
107 inner join activities on (ab_id_a = a_id)
108 where ab_id_p = $project_id order by a_name";
110 $result = $mdb2->query($sql);
111 if (is_a($result, 'PEAR_Error')) {
112 die($result->getMessage());
114 $activity_arr = array();
115 while ($value = $result->fetchRow()) {
116 $activity_arr[] = $value['ab_id_a'];
118 $a_comma_separated = implode(",", $activity_arr); // This is a comma-separated list of associated activity ids.
120 // Re-bind the project to activities (new way).
121 $sql = "update projects set p_activities = ".$mdb2->quote($a_comma_separated)." where p_id = $project_id";
122 $affected = $mdb2->exec($sql);
123 if (is_a($affected, 'PEAR_Error')) {
124 die($affected->getMessage());
129 if ($_POST["convert133to1340"]) {
130 setChange("ALTER TABLE companies ADD COLUMN c_show_pie smallint(2) DEFAULT 1");
131 setChange("ALTER TABLE companies ADD COLUMN c_pie_mode smallint(2) DEFAULT 1");
132 setChange("ALTER TABLE companies ADD COLUMN c_lang varchar(20) default NULL");
135 // The update_companies function sets up c_show_pie, c_pie_mode, and c_lang
136 // fields in the companies table from the corresponding manager fields.
137 if ($_POST["update_companies"]) {
138 $mdb2 = getConnection();
139 // Get all active managers.
140 $sql = "select u_company_id, u_show_pie, u_pie_mode, u_lang from users
141 where u_manager_id is NULL and u_login <> 'admin' and u_company_id is not NULL and u_active = 1";
142 $res = $mdb2->query($sql);
143 if (is_a($res, 'PEAR_Error')) {
144 die($res->getMessage());
146 // Iterate through managers and set fields in the companies table.
147 while ($val = $res->fetchRow()) {
148 $company_id = $val['u_company_id'];
149 $show_pie = $val['u_show_pie'];
150 $pie_mode = $val['u_pie_mode'];
151 $lang = $val['u_lang'];
153 $sql = "update companies set
154 c_show_pie = $show_pie, c_pie_mode = $pie_mode, c_lang = ".$mdb2->quote($lang).
155 " where c_id = $company_id";
157 $result = $mdb2->query($sql);
158 if (is_a($result, 'PEAR_Error')) {
159 die($result->getMessage());
164 if ($_POST["convert1340to1485"]) {
165 setChange("ALTER TABLE users DROP u_show_pie");
166 setChange("ALTER TABLE users DROP u_pie_mode");
167 setChange("ALTER TABLE users DROP u_lang");
168 setChange("ALTER TABLE `users` modify u_login varchar(100) NOT NULL");
169 setChange("ALTER TABLE `users` modify u_active smallint(6) default '1'");
170 setChange("drop index u_login_idx on users");
171 setChange("create unique index u_login_idx on users(u_login, u_active)");
172 setChange("ALTER TABLE companies MODIFY `c_lang` varchar(20) NOT NULL default 'en'");
173 setChange("ALTER TABLE companies ADD COLUMN `c_date_format` varchar(20) NOT NULL default '%Y-%m-%d'");
174 setChange("ALTER TABLE companies ADD COLUMN `c_time_format` varchar(20) NOT NULL default '%H:%M'");
175 setChange("ALTER TABLE companies ADD COLUMN `c_week_start` smallint(2) NOT NULL DEFAULT '0'");
176 setChange("ALTER TABLE clients MODIFY `clnt_status` smallint(6) default '1'");
177 setChange("create unique index clnt_name_idx on clients(clnt_id_um, clnt_name, clnt_status)");
178 setChange("ALTER TABLE projects modify p_status smallint(6) default '1'");
179 setChange("update projects set p_status = NULL where p_status = 1000");
180 setChange("drop index p_manager_idx on projects");
181 setChange("create unique index p_name_idx on projects(p_manager_id, p_name, p_status)");
182 setChange("ALTER TABLE activities modify a_status smallint(6) default '1'");
183 setChange("update activities set a_status = NULL where a_status = 1000");
184 setChange("drop index a_manager_idx on activities");
185 setChange("create unique index a_name_idx on activities(a_manager_id, a_name, a_status)");
186 setChange("RENAME TABLE companies TO teams");
187 setChange("RENAME TABLE teams TO att_teams");
188 setChange("ALTER TABLE att_teams CHANGE c_id id int(11) NOT NULL auto_increment");
189 setChange("RENAME TABLE users TO att_users");
190 setChange("update att_users set u_company_id = 0 where u_company_id is NULL");
191 setChange("ALTER TABLE att_users CHANGE u_company_id team_id int(11) NOT NULL");
192 setChange("RENAME TABLE att_teams TO tt_teams");
193 setChange("RENAME TABLE att_users TO tt_users");
194 setChange("ALTER TABLE tt_teams CHANGE c_name name varchar(80) NOT NULL");
195 setChange("ALTER TABLE `tt_teams` drop `c_www`");
196 setChange("ALTER TABLE `tt_teams` MODIFY `name` varchar(80) default NULL");
197 setChange("ALTER TABLE clients ADD COLUMN `your_name` varchar(255) default NULL");
198 setChange("ALTER TABLE tt_teams ADD COLUMN `address` varchar(255) default NULL");
199 setChange("ALTER TABLE invoice_header ADD COLUMN `client_name` varchar(255) default NULL");
200 setChange("ALTER TABLE invoice_header ADD COLUMN `client_addr` varchar(255) default NULL");
201 setChange("ALTER TABLE report_filter_set ADD COLUMN `rfs_cb_cost` tinyint(4) default '0'");
202 setChange("ALTER TABLE activity_log DROP primary key");
203 setChange("ALTER TABLE activity_log ADD COLUMN `id` bigint NOT NULL auto_increment primary key");
204 setChange("CREATE TABLE `tt_custom_fields` (`id` int(11) NOT NULL auto_increment, `team_id` int(11) NOT NULL, `type` tinyint(4) NOT NULL default '0', `label` varchar(32) NOT NULL default '', PRIMARY KEY (`id`))");
205 setChange("CREATE TABLE `tt_custom_field_options` (`id` int(11) NOT NULL auto_increment, `field_id` int(11) NOT NULL, `value` varchar(32) NOT NULL default '', PRIMARY KEY (`id`))");
206 setChange("CREATE TABLE `tt_custom_field_log` (`id` bigint NOT NULL auto_increment, `al_id` bigint NOT NULL, `field_id` int(11) NOT NULL, `value` varchar(255) default NULL, PRIMARY KEY (`id`))");
207 setChange("ALTER TABLE tt_users DROP u_level");
208 setChange("ALTER TABLE tt_custom_fields ADD COLUMN `status` tinyint(4) default '1'");
209 setChange("ALTER TABLE report_filter_set ADD COLUMN `rfs_cb_cf_1` tinyint(4) default '0'");
210 setChange("ALTER TABLE tt_teams ADD COLUMN `plugins` varchar(255) default NULL");
211 setChange("ALTER TABLE tt_teams MODIFY c_locktime int(4) default '0'");
212 setChange("ALTER TABLE clients DROP your_name");
213 setChange("ALTER TABLE clients DROP clnt_addr_your");
214 setChange("ALTER TABLE `tt_custom_fields` ADD COLUMN `required` tinyint(4) default '0'");
215 setChange("ALTER TABLE tt_teams DROP c_pie_mode");
216 setChange("RENAME TABLE report_filter_set TO tt_fav_reports");
217 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_id id int(11) unsigned NOT NULL auto_increment");
218 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_name name varchar(200) NOT NULL");
219 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_id_u user_id int(11) NOT NULL");
220 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_id_p project_id int(11) default NULL");
221 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_id_a task_id int(11) default NULL");
222 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_users users text default NULL");
223 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_period period tinyint(4) default NULL");
224 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_period_start period_start date default NULL");
225 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_period_finish period_end date default NULL");
226 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_project show_project tinyint(4) NOT NULL default '0'");
227 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_activity show_task tinyint(4) NOT NULL default '0'");
228 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_note show_note tinyint(4) NOT NULL default '0'");
229 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_start show_start tinyint(4) NOT NULL default '0'");
230 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_finish show_end tinyint(4) NOT NULL default '0'");
231 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_duration show_duration tinyint(4) NOT NULL default '0'");
232 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_cost show_cost tinyint(4) NOT NULL default '0'");
233 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_cf_1 show_custom_field_1 tinyint(4) NOT NULL default '0'");
234 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_idle show_empty_days tinyint(4) NOT NULL default '0'");
235 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_cb_totals_only show_totals_only tinyint(4) NOT NULL default '0'");
236 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_groupby group_by varchar(20) default NULL");
237 setChange("ALTER TABLE tt_fav_reports CHANGE rfs_billable billable tinyint(4) default NULL");
238 setChange("ALTER TABLE projects CHANGE p_activities tasks text default NULL");
239 setChange("ALTER TABLE tt_teams CHANGE c_currency currency varchar(7) default NULL");
240 setChange("ALTER TABLE tt_teams CHANGE c_locktime locktime int(4) default '0'");
241 setChange("ALTER TABLE tt_teams CHANGE c_show_pie show_pie smallint(2) DEFAULT '1'");
242 setChange("ALTER TABLE tt_teams CHANGE c_lang lang varchar(10) NOT NULL default 'en'");
243 setChange("ALTER TABLE tt_teams CHANGE c_date_format date_format varchar(20) NOT NULL default '%Y-%m-%d'");
244 setChange("ALTER TABLE tt_teams CHANGE c_time_format time_format varchar(20) NOT NULL default '%H:%M'");
245 setChange("ALTER TABLE tt_teams CHANGE c_week_start week_start smallint(2) NOT NULL DEFAULT '0'");
246 setChange("ALTER TABLE tt_users CHANGE u_id id int(11) NOT NULL auto_increment");
247 setChange("ALTER TABLE tt_users CHANGE u_timestamp timestamp timestamp NOT NULL");
248 setChange("ALTER TABLE tt_users CHANGE u_login login varchar(50) NOT NULL");
249 setChange("drop index u_login_idx on tt_users");
250 setChange("create unique index login_idx on tt_users(login, u_active)");
251 setChange("ALTER TABLE tt_users CHANGE u_password password varchar(50) default NULL");
252 setChange("ALTER TABLE tt_users CHANGE u_name name varchar(100) default NULL");
253 setChange("ALTER TABLE tt_users CHANGE u_email email varchar(100) default NULL");
254 setChange("ALTER TABLE tt_users CHANGE u_rate rate float(6,2) NOT NULL default '0.00'");
255 setChange("update tt_users set u_active = NULL where u_active = 1000");
256 setChange("ALTER TABLE tt_users CHANGE u_active status tinyint(4) default '1'");
257 setChange("ALTER TABLE tt_teams ADD COLUMN status tinyint(4) default '1'");
258 setChange("ALTER TABLE tt_users ADD COLUMN role int(11) default '4'");
259 setChange("update tt_users set role = 1024 where login = 'admin'");
260 setChange("update tt_users set role = 68 where u_comanager = 1");
261 setChange("update tt_users set role = 324 where u_manager_id is null and login != 'admin'");
262 setChange("ALTER TABLE user_bind CHANGE ub_checked status tinyint(4) default '1'");
263 setChange("ALTER TABLE activities ADD COLUMN team_id int(11) NOT NULL");
264 setChange("ALTER TABLE clients ADD COLUMN team_id int(11) NOT NULL");
265 setChange("ALTER TABLE projects ADD COLUMN team_id int(11) NOT NULL");
268 // The update_to_team_id function sets team_id field projects, activities, and clients tables.
269 if ($_POST["update_to_team_id"]) {
270 $mdb2 = getConnection();
273 $sql = "select p_id, p_manager_id from projects where team_id = 0 limit 1000";
274 $res = $mdb2->query($sql);
275 if (is_a($res, 'PEAR_Error')) {
276 die($res->getMessage());
278 // Iterate through projects.
279 $projects_updated = 0;
280 while ($val = $res->fetchRow()) {
281 $project_id = $val['p_id'];
282 $manager_id = $val['p_manager_id'];
284 $sql = "select team_id from tt_users where id = $manager_id";
285 $res2 = $mdb2->query($sql);
286 if (is_a($res2, 'PEAR_Error')) {
287 die($res2->getMessage());
289 $val2 = $res2->fetchRow();
290 $team_id = $val2['team_id'];
293 $sql = "update projects set team_id = $team_id where p_id = $project_id";
294 $affected = $mdb2->exec($sql);
295 if (is_a($affected, 'PEAR_Error')) {
296 die($affected->getMessage());
298 $projects_updated += $affected;
301 print "Updated $projects_updated projects...<br>\n";
304 $sql = "select a_id, a_manager_id from activities where team_id = 0 limit 1000";
305 $res = $mdb2->query($sql);
306 if (is_a($res, 'PEAR_Error')) {
307 die($res->getMessage());
309 // Iterate through tasks.
311 while ($val = $res->fetchRow()) {
312 $task_id = $val['a_id'];
313 $manager_id = $val['a_manager_id'];
315 $sql = "select team_id from tt_users where id = $manager_id";
316 $res2 = $mdb2->query($sql);
317 if (is_a($res2, 'PEAR_Error')) {
318 die($res2->getMessage());
320 $val2 = $res2->fetchRow();
321 $team_id = $val2['team_id'];
324 $sql = "update activities set team_id = $team_id where a_id = $task_id";
325 $affected = $mdb2->exec($sql);
326 if (is_a($affected, 'PEAR_Error')) {
327 die($affected->getMessage());
329 $tasks_updated += $affected;
332 print "Updated $tasks_updated tasks...<br>\n";
335 $sql = "select clnt_id, clnt_id_um from clients where team_id = 0 limit 1000";
336 $res = $mdb2->query($sql);
337 if (is_a($res, 'PEAR_Error')) {
338 die($res->getMessage());
340 // Iterate through clients.
341 $clients_updated = 0;
342 while ($val = $res->fetchRow()) {
343 $client_id = $val['clnt_id'];
344 $manager_id = $val['clnt_id_um'];
346 $sql = "select team_id from tt_users where id = $manager_id";
347 $res2 = $mdb2->query($sql);
348 if (is_a($res2, 'PEAR_Error')) {
349 die($res2->getMessage());
351 $val2 = $res2->fetchRow();
352 $team_id = $val2['team_id'];
355 $sql = "update clients set team_id = $team_id where clnt_id = $client_id";
356 $affected = $mdb2->exec($sql);
357 if (is_a($affected, 'PEAR_Error')) {
358 die($affected->getMessage());
360 $clients_updated += $affected;
363 print "Updated $clients_updated clients...<br>\n";
366 if ($_POST["convert1485to1579"]) {
367 setChange("ALTER TABLE tt_fav_reports MODIFY id int(11) NOT NULL auto_increment");
368 setChange("RENAME TABLE clients TO tt_clients");
369 setChange("ALTER TABLE tt_clients CHANGE clnt_id id int(11) NOT NULL AUTO_INCREMENT");
370 setChange("ALTER TABLE tt_clients CHANGE clnt_status status tinyint(4) default '1'");
371 setChange("ALTER TABLE tt_clients DROP clnt_id_um");
372 setChange("ALTER TABLE tt_clients CHANGE clnt_name name varchar(80) NOT NULL");
373 setChange("drop index clnt_name_idx on tt_clients");
374 setChange("drop index client_name_idx on tt_clients");
375 setChange("create unique index client_name_idx on tt_clients(team_id, name, status)");
376 setChange("ALTER TABLE tt_teams ADD COLUMN `timestamp` timestamp NOT NULL");
377 setChange("ALTER TABLE tt_clients CHANGE clnt_addr_cust address varchar(255) default NULL");
378 setChange("ALTER TABLE tt_clients DROP clnt_discount");
379 setChange("ALTER TABLE tt_clients DROP clnt_comment");
380 setChange("ALTER TABLE tt_clients DROP clnt_fsubtotals");
381 setChange("ALTER TABLE tt_clients CHANGE clnt_tax tax float(6,2) NOT NULL default '0.00'");
382 setChange("ALTER TABLE activity_log ADD COLUMN client_id int(11) default NULL");
383 setChange("ALTER TABLE tt_teams DROP show_pie");
384 setChange("ALTER TABLE tt_fav_reports CHANGE group_by sort_by varchar(20) default 'date'");
385 setChange("RENAME TABLE tmp_refs TO tt_tmp_refs");
386 setChange("ALTER TABLE tt_tmp_refs CHANGE tr_created timestamp timestamp NOT NULL");
387 setChange("ALTER TABLE tt_tmp_refs CHANGE tr_code ref char(32) NOT NULL default ''");
388 setChange("ALTER TABLE tt_tmp_refs CHANGE tr_userid user_id int(11) NOT NULL");
389 setChange("RENAME TABLE projects TO tt_projects");
390 setChange("ALTER TABLE tt_projects CHANGE p_id id int(11) NOT NULL auto_increment");
391 setChange("ALTER TABLE tt_projects DROP p_timestamp");
392 setChange("ALTER TABLE tt_projects CHANGE p_name name varchar(80) NOT NULL");
393 setChange("ALTER TABLE tt_projects CHANGE p_status status tinyint(4) default '1'");
394 setChange("drop index p_name_idx on tt_projects");
395 setChange("create unique index project_idx on tt_projects(team_id, name, status)");
396 setChange("RENAME TABLE activities TO tt_tasks");
397 setChange("ALTER TABLE tt_tasks CHANGE a_id id int(11) NOT NULL auto_increment");
398 setChange("ALTER TABLE tt_tasks DROP a_timestamp");
399 setChange("ALTER TABLE tt_tasks CHANGE a_name name varchar(80) NOT NULL");
400 setChange("ALTER TABLE tt_tasks CHANGE a_status status tinyint(4) default '1'");
401 setChange("drop index a_name_idx on tt_tasks");
402 setChange("create unique index task_idx on tt_tasks(team_id, name, status)");
403 setChange("RENAME TABLE invoice_header TO tt_invoice_headers");
404 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_user_id user_id int(11) NOT NULL");
405 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_number number varchar(20) default NULL");
406 setChange("ALTER TABLE tt_invoice_headers DROP ih_addr_your");
407 setChange("ALTER TABLE tt_invoice_headers DROP ih_addr_cust");
408 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_comment comment varchar(255) default NULL");
409 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_tax tax float(6,2) default '0.00'");
410 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_discount discount float(6,2) default '0.00'");
411 setChange("ALTER TABLE tt_invoice_headers CHANGE ih_fsubtotals subtotals tinyint(4) NOT NULL default '0'");
412 setChange("ALTER TABLE tt_users DROP u_comanager");
413 setChange("ALTER TABLE tt_tasks DROP a_manager_id");
414 setChange("ALTER TABLE tt_projects DROP p_manager_id");
415 setChange("ALTER TABLE tt_users DROP u_manager_id");
416 setChange("ALTER TABLE activity_bind DROP ab_id");
417 setChange("RENAME TABLE activity_bind TO tt_project_task_binds");
418 setChange("ALTER TABLE tt_project_task_binds CHANGE ab_id_p project_id int(11) NOT NULL");
419 setChange("ALTER TABLE tt_project_task_binds CHANGE ab_id_a task_id int(11) NOT NULL");
420 setChange("RENAME TABLE user_bind TO tt_user_project_binds");
421 setChange("ALTER TABLE tt_user_project_binds CHANGE ub_rate rate float(6,2) NOT NULL default '0.00'");
422 setChange("ALTER TABLE tt_user_project_binds CHANGE ub_id_p project_id int(11) NOT NULL");
423 setChange("ALTER TABLE tt_user_project_binds CHANGE ub_id_u user_id int(11) NOT NULL");
424 setChange("ALTER TABLE tt_user_project_binds CHANGE ub_id id int(11) NOT NULL auto_increment");
425 setChange("CREATE TABLE `tt_client_project_binds` (`client_id` int(11) NOT NULL, `project_id` int(11) NOT NULL)");
426 setChange("ALTER TABLE tt_user_project_binds MODIFY rate float(6,2) default '0.00'");
427 setChange("ALTER TABLE tt_clients MODIFY tax float(6,2) default '0.00'");
428 setChange("RENAME TABLE activity_log TO tt_log");
429 setChange("ALTER TABLE tt_log CHANGE al_timestamp timestamp timestamp NOT NULL");
430 setChange("ALTER TABLE tt_log CHANGE al_user_id user_id int(11) NOT NULL");
431 setChange("ALTER TABLE tt_log CHANGE al_date date date NOT NULL");
432 setChange("drop index al_date_idx on tt_log");
433 setChange("create index date_idx on tt_log(date)");
434 setChange("ALTER TABLE tt_log CHANGE al_from start time default NULL");
435 setChange("ALTER TABLE tt_log CHANGE al_duration duration time default NULL");
436 setChange("ALTER TABLE tt_log CHANGE al_project_id project_id int(11) NOT NULL");
437 setChange("ALTER TABLE tt_log MODIFY project_id int(11) default NULL");
438 setChange("ALTER TABLE tt_log CHANGE al_activity_id task_id int(11) default NULL");
439 setChange("ALTER TABLE tt_log CHANGE al_comment comment blob");
440 setChange("ALTER TABLE tt_log CHANGE al_billable billable tinyint(4) default '0'");
441 setChange("drop index al_user_id_idx on tt_log");
442 setChange("drop index al_project_id_idx on tt_log");
443 setChange("drop index al_activity_id_idx on tt_log");
444 setChange("create index user_idx on tt_log(user_id)");
445 setChange("create index project_idx on tt_log(project_id)");
446 setChange("create index task_idx on tt_log(task_id)");
447 setChange("ALTER TABLE tt_custom_field_log CHANGE al_id log_id bigint NOT NULL");
448 setChange("RENAME TABLE sysconfig TO tt_config");
449 setChange("ALTER TABLE tt_config DROP sysc_id");
450 setChange("ALTER TABLE tt_config CHANGE sysc_id_u user_id int(11) NOT NULL");
451 setChange("ALTER TABLE tt_config CHANGE sysc_name param_name varchar(32) NOT NULL");
452 setChange("ALTER TABLE tt_config CHANGE sysc_value param_value varchar(80) default NULL");
453 setChange("create unique index param_idx on tt_config(user_id, param_name)");
454 setChange("ALTER TABLE tt_log ADD COLUMN invoice_id int(11) default NULL");
455 setChange("ALTER TABLE tt_projects ADD COLUMN description varchar(255) default NULL");
456 setChange("CREATE TABLE `tt_invoices` (`id` int(11) NOT NULL auto_increment, `team_id` int(11) NOT NULL, `number` varchar(20) default NULL, `client_name` varchar(255) default NULL, `client_addr` varchar(255) default NULL, `comment` varchar(255) default NULL, `tax` float(6,2) default '0.00', `discount` float(6,2) default '0.00', PRIMARY KEY (`id`))");
457 setChange("ALTER TABLE tt_invoices drop number");
458 setChange("ALTER TABLE tt_invoices drop client_name");
459 setChange("ALTER TABLE tt_invoices drop client_addr");
460 setChange("ALTER TABLE tt_invoices drop comment");
461 setChange("ALTER TABLE tt_invoices drop tax");
462 setChange("ALTER TABLE tt_invoices ADD COLUMN name varchar(80) NOT NULL");
463 setChange("ALTER TABLE tt_invoices ADD COLUMN client_id int(11) NOT NULL");
464 setChange("ALTER TABLE tt_invoices ADD COLUMN start_date date NOT NULL");
465 setChange("ALTER TABLE tt_invoices ADD COLUMN end_date date NOT NULL");
466 setChange("create unique index name_idx on tt_invoices(team_id, name)");
467 setChange("drop index ub_id_u on tt_user_project_binds");
468 setChange("create unique index bind_idx on tt_user_project_binds(user_id, project_id)");
469 setChange("create index client_idx on tt_log(client_id)");
470 setChange("create index invoice_idx on tt_log(invoice_id)");
473 if ($_POST["convert1579to1600"]) {
474 setChange("ALTER TABLE tt_invoices ADD COLUMN date date NOT NULL");
475 setChange("ALTER TABLE tt_teams ADD COLUMN custom_logo tinyint(4) default '0'");
476 setChange("ALTER TABLE tt_tasks ADD COLUMN description varchar(255) default NULL");
477 setChange("ALTER TABLE tt_projects MODIFY name varchar(80) COLLATE utf8_bin NOT NULL");
478 setChange("ALTER TABLE tt_users MODIFY login varchar(50) COLLATE utf8_bin NOT NULL");
479 setChange("ALTER TABLE tt_tasks MODIFY name varchar(80) COLLATE utf8_bin NOT NULL");
480 setChange("ALTER TABLE tt_invoices MODIFY name varchar(80) COLLATE utf8_bin NOT NULL");
481 setChange("ALTER TABLE tt_clients MODIFY name varchar(80) COLLATE utf8_bin NOT NULL");
482 setChange("ALTER TABLE tt_clients ADD COLUMN projects text default NULL");
483 setChange("ALTER TABLE tt_custom_field_log ADD COLUMN option_id int(11) default NULL");
484 setChange("ALTER TABLE tt_teams ADD COLUMN tracking_mode smallint(2) NOT NULL DEFAULT '2'");
485 setChange("ALTER TABLE tt_teams ADD COLUMN record_type smallint(2) NOT NULL DEFAULT '0'");
486 setChange("ALTER TABLE tt_invoices DROP start_date");
487 setChange("ALTER TABLE tt_invoices DROP end_date");
490 if ($_POST["convert1600to1900"]) {
491 setChange("DROP TABLE IF EXISTS tt_invoice_headers");
492 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `client_id` int(11) default NULL");
493 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `cf_1_option_id` int(11) default NULL");
494 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `show_client` tinyint(4) NOT NULL default '0'");
495 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `show_invoice` tinyint(4) NOT NULL default '0'");
496 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `group_by` varchar(20) default NULL");
497 setChange("CREATE TABLE `tt_expense_items` (`id` bigint NOT NULL auto_increment, `date` date NOT NULL, `user_id` int(11) NOT NULL, `client_id` int(11) default NULL, `project_id` int(11) default NULL, `name` varchar(255) NOT NULL, `cost` decimal(10,2) default '0.00', `invoice_id` int(11) default NULL, PRIMARY KEY (`id`))");
498 setChange("create index date_idx on tt_expense_items(date)");
499 setChange("create index user_idx on tt_expense_items(user_id)");
500 setChange("create index client_idx on tt_expense_items(client_id)");
501 setChange("create index project_idx on tt_expense_items(project_id)");
502 setChange("create index invoice_idx on tt_expense_items(invoice_id)");
503 setChange("ALTER TABLE tt_fav_reports DROP sort_by");
504 setChange("ALTER TABLE tt_fav_reports DROP show_empty_days");
505 setChange("ALTER TABLE tt_invoices DROP discount");
506 setChange("ALTER TABLE tt_users ADD COLUMN `client_id` int(11) default NULL");
507 setChange("ALTER TABLE tt_teams ADD COLUMN `decimal_mark` char(1) NOT NULL default '.'");
508 setChange("ALTER TABLE tt_fav_reports ADD COLUMN `invoice` tinyint(4) default NULL");
509 setChange("CREATE TABLE `tt_cron` (`id` int(11) NOT NULL auto_increment, `cron_spec` varchar(255) NOT NULL, `last` int(11) default NULL, `next` int(11) default NULL, `report_id` int(11) default NULL, `email` varchar(100) default NULL, `status` tinyint(4) default '1', PRIMARY KEY (`id`))");
510 setChange("ALTER TABLE tt_cron ADD COLUMN `team_id` int(11) NOT NULL");
511 setChange("create index client_idx on tt_client_project_binds(client_id)");
512 setChange("create index project_idx on tt_client_project_binds(project_id)");
513 setChange("ALTER TABLE tt_log ADD COLUMN status tinyint(4) default '1'");
514 setChange("ALTER TABLE tt_custom_field_log ADD COLUMN status tinyint(4) default '1'");
515 setChange("ALTER TABLE tt_expense_items ADD COLUMN status tinyint(4) default '1'");
516 setChange("ALTER TABLE tt_invoices ADD COLUMN status tinyint(4) default '1'");
517 setChange("DROP INDEX name_idx on tt_invoices");
518 setChange("create unique index name_idx on tt_invoices(team_id, name, status)");
519 setChange("ALTER TABLE tt_teams ADD COLUMN lock_spec varchar(255) default NULL");
522 // The update_clients function updates projects field in tt_clients table.
523 if ($_POST["update_clients"]) {
524 $mdb2 = getConnection();
525 $sql = "select id from tt_clients where status = 1 or status = 0";
526 $res = $mdb2->query($sql);
527 if (is_a($res, 'PEAR_Error')) {
528 die($res->getMessage());
531 $clients_updated = 0;
532 // Iterate through clients.
533 while ($val = $res->fetchRow()) {
534 $client_id = $val['id'];
536 // Get projects binds for client.
537 $sql = "select cpb.project_id from tt_client_project_binds cpb
538 left join tt_projects p on (p.id = cpb.project_id)
539 where cpb.client_id = $client_id order by p.name";
541 $result = $mdb2->query($sql);
542 if (is_a($result, 'PEAR_Error'))
543 die($result->getMessage());
545 $project_arr = array();
546 while ($value = $result->fetchRow()) {
547 $project_arr[] = $value['project_id'];
549 $comma_separated = implode(',', $project_arr); // This is a comma-separated list of associated project ids.
551 // Update the projects field.
552 $sql = "update tt_clients set projects = ".$mdb2->quote($comma_separated)." where id = $client_id";
553 $affected = $mdb2->exec($sql);
554 if (is_a($affected, 'PEAR_Error'))
555 die($affected->getMessage());
556 $clients_updated += $affected;
558 print "Updated $clients_updated clients...<br>\n";
561 // The update_custom_fields function updates option_id field field in tt_custom_field_log table.
562 if ($_POST['update_custom_fields']) {
563 $mdb2 = getConnection();
564 $sql = "update tt_custom_field_log set option_id = value where field_id in (select id from tt_custom_fields where type = 2)";
565 $affected = $mdb2->exec($sql);
566 if (is_a($affected, 'PEAR_Error'))
567 die($affected->getMessage());
569 print "Updated $affected custom fields...<br>\n";
572 // The update_tracking_mode function sets the tracking_mode field in tt_teams table to 2 (== MODE_PROJECTS_AND_TASKS).
573 if ($_POST['update_tracking_mode']) {
574 $mdb2 = getConnection();
575 $sql = "update tt_teams set tracking_mode = 2 where tracking_mode = 0";
576 $affected = $mdb2->exec($sql);
577 if (is_a($affected, 'PEAR_Error'))
578 die($affected->getMessage());
580 print "Updated $affected teams...<br>\n";
583 if ($_POST["cleanup"]) {
585 $mdb2 = getConnection();
586 $inactive_teams = ttTeamHelper::getInactiveTeams();
588 $count = count($inactive_teams);
589 print "$count inactive teams found...<br>\n";
590 for ($i = 0; $i < $count; $i++) {
591 print " deleting team ".$inactive_teams[$i]."<br>\n";
592 $res = ttTeamHelper::delete($inactive_teams[$i]);
595 setChange("OPTIMIZE TABLE tt_client_project_binds");
596 setChange("OPTIMIZE TABLE tt_clients");
597 setChange("OPTIMIZE TABLE tt_config");
598 setChange("OPTIMIZE TABLE tt_custom_field_log");
599 setChange("OPTIMIZE TABLE tt_custom_field_options");
600 setChange("OPTIMIZE TABLE tt_custom_fields");
601 setChange("OPTIMIZE TABLE tt_expense_items");
602 setChange("OPTIMIZE TABLE tt_fav_reports");
603 setChange("OPTIMIZE TABLE tt_invoices");
604 setChange("OPTIMIZE TABLE tt_log");
605 setChange("OPTIMIZE TABLE tt_project_task_binds");
606 setChange("OPTIMIZE TABLE tt_projects");
607 setChange("OPTIMIZE TABLE tt_tasks");
608 setChange("OPTIMIZE TABLE tt_teams");
609 setChange("OPTIMIZE TABLE tt_tmp_refs");
610 setChange("OPTIMIZE TABLE tt_user_project_binds");
611 setChange("OPTIMIZE TABLE tt_users");
622 <table width="80%" border="1" cellpadding="10" cellspacing="0">
624 <td width="80%"><b>Create database structure (v1.9)</b>
625 <br>(applies only to new installations, do not execute when updating)</br></td><td><input type="submit" name="crstructure" value="Create"></td>
631 <table width="80%" border="1" cellpadding="10" cellspacing="0">
633 <td>Update database structure (v0.5 to v0.7)</td><td><input type="submit" name="convert5to7" value="Update"></td>
636 <td>Update database structure (v0.7 to v1.3.3)</td>
637 <td><input type="submit" name="convert7to133" value="Update"><br><input type="submit" name="update_projects" value="Update projects"></td>
640 <td>Update database structure (v1.3.3 to v1.3.40)</td>
641 <td><input type="submit" name="convert133to1340" value="Update"><br><input type="submit" name="update_companies" value="Update companies"></td>
644 <td>Update database structure (v1.3.40 to v1.4.85)</td>
645 <td><input type="submit" name="convert1340to1485" value="Update"><br><input type="submit" name="update_to_team_id" value="Update team_id"></td>
648 <td>Update database structure (v1.4.85 to v1.5.79)</td>
649 <td><input type="submit" name="convert1485to1579" value="Update"></td>
652 <td>Update database structure (v1.5.79 to v1.6)</td>
653 <td><input type="submit" name="convert1579to1600" value="Update"><br><input type="submit" name="update_clients" value="Update clients"><br><input type="submit" name="update_custom_fields" value="Update custom fields"><br><input type="submit" name="update_tracking_mode" value="Update tracking mode"></td>
656 <td>Update database structure (v1.6 to v1.9)</td>
657 <td><input type="submit" name="convert1600to1900" value="Update"><br></td>
661 <h2>DB Maintenance</h2>
662 <table width="80%" border="1" cellpadding="10" cellspacing="0">
664 <td width="80%">Clean up DB from inactive teams</td><td><input type="submit" name="cleanup" value="Clean up"></td>