 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 -- 

 -- Increment goals table by max projectId -- 
ALTER TABLE `goals` ADD `prikey` INT UNSIGNED NOT NULL FIRST;
ALTER TABLE `goals` CHANGE `id` `id` INT( 10 ) UNSIGNED NOT NULL;
UPDATE `goals` SET `prikey`=`id`+(SELECT MAX(`projectId`) FROM `projects`);
ALTER TABLE `goals` DROP PRIMARY KEY, ADD PRIMARY KEY (`prikey`);
ALTER TABLE `goals` DROP `id`;
ALTER TABLE `goals` CHANGE `prikey` `id` 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 --