SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";

CREATE TABLE IF NOT EXISTS `files` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  `size` mediumint(8) unsigned NOT NULL,
  `content` mediumblob NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `infrastructure` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `name` char(64) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `jobidfiles` (
  `jobid` int(10) unsigned NOT NULL,
  `fileid` int(10) unsigned NOT NULL,
  KEY `jobid` (`jobid`),
  KEY `fileid` (`fileid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE IF NOT EXISTS `jobs` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uuid` char(36) COLLATE utf8_unicode_ci NOT NULL,
  `update_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `submit_timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `walltime` int(10) unsigned NOT NULL,
  `jobname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `cpus` tinyint(3) unsigned NOT NULL DEFAULT '1',
  `userid` smallint(5) unsigned NOT NULL,
  `softwareid` smallint(5) unsigned NOT NULL,
  `infrastructureid` smallint(5) unsigned NOT NULL,
  `status` smallint(5) unsigned NOT NULL DEFAULT '1',
  `status_details` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uuid` (`uuid`),
  KEY `userid` (`userid`),
  KEY `softwareid` (`softwareid`),
  KEY `infrastructureid` (`infrastructureid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `params` (
  `jobid` int(10) unsigned NOT NULL,
  `key` char(32) COLLATE utf8_unicode_ci NOT NULL,
  `value` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  KEY `jobid` (`jobid`),
  KEY `key` (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE IF NOT EXISTS `proxycerts` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `userid` smallint(5) unsigned NOT NULL,
  `vo` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `attrs_hash` char(32) COLLATE utf8_unicode_ci NOT NULL,
  `attrs` text COLLATE utf8_unicode_ci NOT NULL,
  `validity` int(10) unsigned NOT NULL,
  `fileid` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `attrs_hash` (`attrs_hash`),
  KEY `userid` (`userid`),
  KEY `fileid` (`fileid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `software` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `name` char(64) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `user` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `dn` char(255) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

ALTER TABLE `jobidfiles`
  ADD CONSTRAINT `jobidfiles_ibfk_2` FOREIGN KEY (`fileid`) REFERENCES `files` (`id`),
  ADD CONSTRAINT `jobidfiles_ibfk_1` FOREIGN KEY (`jobid`) REFERENCES `jobs` (`id`);

ALTER TABLE `jobs`
  ADD CONSTRAINT `jobs_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `user` (`id`),
  ADD CONSTRAINT `jobs_ibfk_2` FOREIGN KEY (`softwareid`) REFERENCES `software` (`id`),
  ADD CONSTRAINT `jobs_ibfk_3` FOREIGN KEY (`infrastructureid`) REFERENCES `infrastructure` (`id`);

ALTER TABLE `params`
  ADD CONSTRAINT `params_ibfk_1` FOREIGN KEY (`jobid`) REFERENCES `jobs` (`id`);

ALTER TABLE `proxycerts`
  ADD CONSTRAINT `proxycerts_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `user` (`id`),
  ADD CONSTRAINT `proxycerts_ibfk_2` FOREIGN KEY (`fileid`) REFERENCES `files` (`id`);

