SELECT 'Upgrading MetaStore schema from 1.2.0 to 2.0.0' AS MESSAGE;
-- SOURCE 021-HIVE-7018.mysql.sql;
ALTER TABLE `TBLS` DROP FOREIGN KEY `TBLS_FK3`;
ALTER TABLE `TBLS` DROP KEY `TBLS_N51`;
ALTER TABLE `TBLS` DROP COLUMN `LINK_TARGET_ID`;

ALTER TABLE `PARTITIONS` DROP FOREIGN KEY `PARTITIONS_FK3` ;
ALTER TABLE `PARTITIONS` DROP KEY `PARTITIONS_N51` ;
ALTER TABLE `PARTITIONS` DROP COLUMN `LINK_TARGET_ID` ;

-- SOURCE 022-HIVE-11970.mysql.sql;
ALTER TABLE `COLUMNS_V2` MODIFY `COLUMN_NAME` varchar(767) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;
ALTER TABLE `PART_COL_PRIVS` MODIFY `COLUMN_NAME` varchar(767) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL;
ALTER TABLE `TBL_COL_PRIVS` MODIFY `COLUMN_NAME` varchar(767) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL;
ALTER TABLE `SORT_COLS` MODIFY `COLUMN_NAME` varchar(767) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL;
ALTER TABLE `TAB_COL_STATS` MODIFY `COLUMN_NAME` varchar(767) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;
ALTER TABLE `PART_COL_STATS` MODIFY `COLUMN_NAME` varchar(767) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL;

-- SOURCE 023-HIVE-12807.mysql.sql;
ALTER TABLE `COMPACTION_QUEUE` ADD `CQ_HIGHEST_TXN_ID` bigint;

-- SOURCE 024-HIVE-12814.mysql.sql;
ALTER TABLE `COMPACTION_QUEUE` ADD `CQ_META_INFO` varbinary(2048);

-- SOURCE 025-HIVE-12816.mysql.sql;
ALTER TABLE `COMPACTION_QUEUE` ADD `CQ_HADOOP_JOB_ID` varchar(32);

-- SOURCE 026-HIVE-12818.mysql.sql;
CREATE TABLE COMPLETED_COMPACTIONS (
  CC_ID bigint PRIMARY KEY,
  CC_DATABASE varchar(128) NOT NULL,
  CC_TABLE varchar(128) NOT NULL,
  CC_PARTITION varchar(767),
  CC_STATE char(1) NOT NULL,
  CC_TYPE char(1) NOT NULL,
  CC_WORKER_ID varchar(128),
  CC_START bigint,
  CC_END bigint,
  CC_RUN_AS varchar(128),
  CC_HIGHEST_TXN_ID bigint,
  CC_META_INFO varbinary(2048),
  CC_HADOOP_JOB_ID varchar(32)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



-- SOURCE 027-HIVE-12819.mysql.sql;
ALTER TABLE `TXNS` ADD `TXN_AGENT_INFO` varchar(128);

-- SOURCE 028-HIVE-12821.mysql.sql;
ALTER TABLE `TXNS` ADD `TXN_HEARTBEAT_COUNT` int;
ALTER TABLE `HIVE_LOCKS` ADD `HL_HEARTBEAT_COUNT` int;

-- SOURCE 029-HIVE-12822.mysql.sql;
ALTER TABLE `TXNS` ADD `TXN_META_INFO` varchar(128);

-- SOURCE 030-HIVE-12823.mysql.sql;
ALTER TABLE `HIVE_LOCKS` ADD `HL_AGENT_INFO` varchar(128);

-- SOURCE 031-HIVE-12831.mysql.sql;
ALTER TABLE `HIVE_LOCKS` ADD `HL_BLOCKEDBY_EXT_ID` bigint;
ALTER TABLE `HIVE_LOCKS` ADD `HL_BLOCKEDBY_INT_ID` bigint;

-- SOURCE 032-HIVE-12832.mysql.sql;
CREATE TABLE AUX_TABLE (
  MT_KEY1 varchar(128) NOT NULL,
  MT_KEY2 bigint NOT NULL,
  MT_COMMENT varchar(255),
  PRIMARY KEY(MT_KEY1, MT_KEY2)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


UPDATE VERSION SET SCHEMA_VERSION='2.0.0', VERSION_COMMENT='Hive release version 2.0.0' where VER_ID=1;
SELECT 'Finished upgrading MetaStore schema from 1.2.0 to 2.0.0' AS MESSAGE;

