 ALTER TABLE `timeitems` ADD `type` ENUM( 'v', 'o', 'g', 'p', 'a' ) NOT NULL DEFAULT 'a';
 ALTER TABLE `timeitems` ADD INDEX ( `type` );
 ALTER TABLE `goals` ADD `timeframeId` INT UNSIGNED NOT NULL;
-UPDATE `goals` SET `timeframeId`= ((SELECT MAX(`timeframeId`) FROM `timeitems`)+1) WHERE `type`='weekly';
-UPDATE `goals` SET `timeframeId`= ((SELECT MAX(`timeframeId`) FROM `timeitems`)+2) WHERE `type`='quarterly';
+UPDATE `goals` SET `timeframeId`= (1 + (
+	CASE  WHEN (SELECT MAX(`timeframeId`) FROM `timeitems`) IS NULL THEN 0
+		ELSE (SELECT MAX(`timeframeId`) FROM `timeitems`)
+	END
+	)) WHERE `type`='weekly';
+UPDATE `goals` SET `timeframeId`= (2 + (
+	CASE  WHEN (SELECT MAX(`timeframeId`) FROM `timeitems`) IS NULL THEN 0
+		ELSE (SELECT MAX(`timeframeId`) FROM `timeitems`)
+	END
+	)) WHERE `type`='quarterly';
 ALTER TABLE `goals` CHANGE `type` `type` ENUM('g') NOT NULL DEFAULT 'g';
 
  -- Copy over data from goals -- 

ALTER TABLE `itemstatus` CHANGE `prikey` `itemId` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0';

DELETE FROM `nextactions` WHERE `nextaction` ='0';
UPDATE `nextactions` SET `nextaction`=`nextaction`+(SELECT MAX(`projectId`) FROM `projects`);

ALTER TABLE `lookup` ADD `prikey` INT UNSIGNED NOT NULL;
UPDATE `lookup` SET `prikey` =`itemId`+(SELECT (SELECT MAX(`id`) FROM `goals`)+(SELECT MAX(`projectId`) FROM `projects`));
ALTER TABLE `lookup` DROP PRIMARY KEY, ADD PRIMARY KEY (`parentId` , `prikey`);
ALTER TABLE `lookup` DROP `itemId`;
ALTER TABLE `lookup` CHANGE `prikey` `itemId` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0';


 -- rename old database to name-old --
 -- rename new database to preferred name -- 
 -- User may need to adjust privileges --