master.sql 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. -- ***** BEGIN LICENSE BLOCK *****
  2. --
  3. -- This file is part of the Zotero Data Server.
  4. --
  5. -- Copyright © 2010 Center for History and New Media
  6. -- George Mason University, Fairfax, Virginia, USA
  7. -- http://zotero.org
  8. --
  9. -- This program is free software: you can redistribute it and/or modify
  10. -- it under the terms of the GNU Affero General Public License as published by
  11. -- the Free Software Foundation, either version 3 of the License, or
  12. -- (at your option) any later version.
  13. --
  14. -- This program is distributed in the hope that it will be useful,
  15. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. -- GNU Affero General Public License for more details.
  18. --
  19. -- You should have received a copy of the GNU Affero General Public License
  20. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. --
  22. -- ***** END LICENSE BLOCK *****
  23. SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
  24. CREATE TABLE `abstractCreators` (
  25. `creatorID` int(10) unsigned NOT NULL
  26. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  27. CREATE TABLE `abstractItems` (
  28. `itemID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  29. PRIMARY KEY (`itemID`)
  30. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  31. CREATE TABLE `baseFieldMappings` (
  32. `itemTypeID` smallint(5) unsigned NOT NULL,
  33. `baseFieldID` smallint(5) unsigned NOT NULL,
  34. `fieldID` smallint(5) unsigned NOT NULL,
  35. PRIMARY KEY (`itemTypeID`,`baseFieldID`,`fieldID`),
  36. KEY `baseFieldID` (`baseFieldID`),
  37. KEY `fieldID` (`fieldID`)
  38. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  39. CREATE TABLE `charsets` (
  40. `charsetID` tinyint(3) unsigned NOT NULL,
  41. `charset` varchar(50) NOT NULL,
  42. PRIMARY KEY (`charsetID`),
  43. KEY `charset` (`charset`)
  44. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  45. CREATE TABLE `creatorTypes` (
  46. `creatorTypeID` smallint(5) unsigned NOT NULL,
  47. `creatorTypeName` varchar(50) NOT NULL,
  48. `custom` tinyint(1) unsigned NOT NULL,
  49. PRIMARY KEY (`creatorTypeID`),
  50. UNIQUE KEY `creatorTypeName` (`creatorTypeName`),
  51. KEY `custom` (`custom`)
  52. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  53. CREATE TABLE `fields` (
  54. `fieldID` smallint(5) unsigned NOT NULL,
  55. `fieldName` varchar(50) NOT NULL,
  56. `fieldFormatID` tinyint(3) unsigned DEFAULT NULL,
  57. `custom` tinyint(1) unsigned NOT NULL,
  58. PRIMARY KEY (`fieldID`),
  59. UNIQUE KEY `fieldName` (`fieldName`),
  60. KEY `custom` (`custom`)
  61. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  62. CREATE TABLE `groups` (
  63. `groupID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  64. `libraryID` int(10) unsigned NOT NULL,
  65. `name` varchar(255) NOT NULL,
  66. `slug` varchar(255) DEFAULT NULL,
  67. `type` enum('PublicOpen','PublicClosed','Private') NOT NULL DEFAULT 'Private',
  68. `libraryEditing` enum('admins','members') NOT NULL DEFAULT 'admins',
  69. `libraryReading` enum('members','all') NOT NULL DEFAULT 'all',
  70. `fileEditing` enum('none','admins','members') NOT NULL DEFAULT 'admins',
  71. `description` text NOT NULL,
  72. `url` varchar(255) NOT NULL,
  73. `hasImage` tinyint(1) unsigned NOT NULL DEFAULT '0',
  74. `dateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  75. `dateModified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  76. `version` tinyint(3) unsigned NOT NULL DEFAULT '1',
  77. PRIMARY KEY (`groupID`),
  78. UNIQUE KEY `libraryID` (`libraryID`),
  79. UNIQUE KEY `slug` (`slug`)
  80. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  81. CREATE TABLE `groupUsers` (
  82. `groupID` int(10) unsigned NOT NULL,
  83. `userID` int(10) unsigned NOT NULL,
  84. `role` enum('owner','admin','member') NOT NULL DEFAULT 'member',
  85. `joined` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  86. `lastUpdated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  87. PRIMARY KEY (`groupID`,`userID`),
  88. KEY `userID` (`userID`)
  89. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  90. CREATE TABLE `itemTypeCreatorTypes` (
  91. `itemTypeID` smallint(5) unsigned NOT NULL,
  92. `creatorTypeID` smallint(5) unsigned NOT NULL,
  93. `primaryField` tinyint(1) unsigned NOT NULL,
  94. PRIMARY KEY (`itemTypeID`,`creatorTypeID`),
  95. KEY `creatorTypeID` (`creatorTypeID`)
  96. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  97. CREATE TABLE `itemTypeFields` (
  98. `itemTypeID` smallint(5) unsigned NOT NULL,
  99. `fieldID` smallint(5) unsigned NOT NULL,
  100. `hide` tinyint(3) unsigned NOT NULL DEFAULT '0',
  101. `orderIndex` tinyint(3) unsigned NOT NULL,
  102. PRIMARY KEY (`itemTypeID`,`fieldID`),
  103. KEY `fieldID` (`fieldID`)
  104. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  105. CREATE TABLE `itemTypes` (
  106. `itemTypeID` smallint(5) unsigned NOT NULL,
  107. `itemTypeName` varchar(50) NOT NULL,
  108. `custom` tinyint(1) unsigned NOT NULL,
  109. PRIMARY KEY (`itemTypeID`),
  110. UNIQUE KEY `itemTypeName` (`itemTypeName`),
  111. KEY `custom` (`custom`)
  112. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  113. CREATE TABLE IF NOT EXISTS `keyAccessLog` (
  114. `keyID` int(10) unsigned NOT NULL,
  115. `ipAddress` int(10) unsigned NOT NULL DEFAULT '0',
  116. `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  117. PRIMARY KEY (`keyID`,`ipAddress`)
  118. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  119. CREATE TABLE `keyPermissions` (
  120. `keyID` int(10) unsigned NOT NULL,
  121. `libraryID` int(10) unsigned NOT NULL,
  122. `permission` enum('library','notes','write') NOT NULL,
  123. `granted` tinyint(1) unsigned NOT NULL,
  124. PRIMARY KEY (`keyID`,`libraryID`,`permission`)
  125. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  126. CREATE TABLE `keys` (
  127. `keyID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  128. `key` char(24) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  129. `userID` int(10) unsigned NOT NULL,
  130. `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  131. `dateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  132. `lastUsed` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  133. PRIMARY KEY (`keyID`),
  134. UNIQUE KEY `key` (`key`),
  135. KEY `userID` (`userID`)
  136. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  137. CREATE TABLE `libraries` (
  138. `libraryID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  139. `libraryType` enum('user','group') NOT NULL,
  140. `lastUpdated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  141. `version` int(10) unsigned NOT NULL DEFAULT '0',
  142. `shardID` smallint(5) unsigned NOT NULL,
  143. PRIMARY KEY (`libraryID`),
  144. KEY `libraryType` (`libraryType`),
  145. KEY `shardID` (`shardID`)
  146. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  147. CREATE TABLE `processorDaemons` (
  148. `mode` enum('download','upload','error','index') NOT NULL,
  149. `addr` int(10) unsigned NOT NULL,
  150. `port` smallint(5) unsigned NOT NULL,
  151. `lastSeen` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  152. PRIMARY KEY (`mode`,`addr`)
  153. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  154. CREATE TABLE `sessions` (
  155. `sessionID` char(32) CHARACTER SET ascii NOT NULL,
  156. `userID` int(10) unsigned NOT NULL,
  157. `ipAddress` int(10) unsigned DEFAULT NULL,
  158. `exclusive` tinyint(1) unsigned NOT NULL DEFAULT '0',
  159. `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  160. PRIMARY KEY (`sessionID`),
  161. KEY `userID` (`userID`)
  162. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  163. CREATE TABLE `shardHosts` (
  164. `shardHostID` tinyint(3) unsigned NOT NULL,
  165. `address` varchar(15) NOT NULL,
  166. `port` smallint(5) unsigned NOT NULL DEFAULT 3306,
  167. `state` enum('up','readonly','down') NOT NULL,
  168. PRIMARY KEY (`shardHostID`)
  169. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  170. CREATE TABLE `shards` (
  171. `shardID` smallint(5) unsigned NOT NULL,
  172. `shardHostID` tinyint(3) unsigned NOT NULL,
  173. `db` varchar(20) NOT NULL,
  174. `state` enum('up','readonly','down') NOT NULL,
  175. `items` mediumint(8) unsigned NOT NULL DEFAULT '0',
  176. PRIMARY KEY (`shardID`),
  177. KEY `shardHostID` (`shardHostID`)
  178. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  179. CREATE TABLE `storageAccounts` (
  180. `userID` int(10) unsigned NOT NULL,
  181. `quota` mediumint(8) unsigned DEFAULT 10000 NOT NULL,
  182. `expiration` timestamp NULL DEFAULT DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 10 YEARS),
  183. PRIMARY KEY (`userID`)
  184. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  185. CREATE TABLE `storageDownloadLog` (
  186. `ownerUserID` int(10) unsigned NOT NULL,
  187. `downloadUserID` int(10) unsigned DEFAULT NULL,
  188. `ipAddress` int(10) unsigned NULL,
  189. `storageFileID` int(10) unsigned NOT NULL,
  190. `filename` varchar(1024) NOT NULL,
  191. `size` int(10) unsigned NOT NULL,
  192. `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
  193. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  194. CREATE TABLE `storageFiles` (
  195. `storageFileID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  196. `hash` char(32) NOT NULL,
  197. `filename` varchar(255) NOT NULL,
  198. `size` int(10) unsigned NOT NULL,
  199. `zip` tinyint(1) unsigned NOT NULL,
  200. `lastAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  201. PRIMARY KEY (`storageFileID`),
  202. UNIQUE KEY `hash` (`hash`,`filename`,`zip`)
  203. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  204. CREATE TABLE `storageFilesExisting` (
  205. `storageFileID` int(10) unsigned NOT NULL,
  206. PRIMARY KEY (`storageFileID`)
  207. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  208. CREATE TABLE `storageLastSync` (
  209. `userID` int(10) unsigned NOT NULL,
  210. `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  211. PRIMARY KEY (`userID`)
  212. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  213. CREATE TABLE `storageUploadLog` (
  214. `ownerUserID` int(10) unsigned NOT NULL,
  215. `uploadUserID` int(10) unsigned NOT NULL,
  216. `ipAddress` int(10) unsigned NULL,
  217. `storageFileID` int(10) unsigned NOT NULL,
  218. `filename` varchar(1024) NOT NULL,
  219. `size` int(10) unsigned NOT NULL,
  220. `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
  221. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  222. CREATE TABLE `storageUploadQueue` (
  223. `uploadKey` char(32) NOT NULL,
  224. `userID` int(10) unsigned NOT NULL,
  225. `hash` char(32) NOT NULL,
  226. `filename` varchar(1024) NOT NULL,
  227. `zip` tinyint(1) unsigned NOT NULL,
  228. `size` int(10) unsigned NOT NULL,
  229. `mtime` bigint(13) unsigned NOT NULL,
  230. `contentType` varchar(75) DEFAULT NULL,
  231. `charset` varchar(25) DEFAULT NULL,
  232. `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  233. PRIMARY KEY (`uploadKey`),
  234. KEY `userID` (`userID`)
  235. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  236. CREATE TABLE `syncDownloadProcessLog` (
  237. `userID` int(10) unsigned NOT NULL,
  238. `lastsync` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  239. `objects` int(10) unsigned NOT NULL,
  240. `ipAddress` int(10) unsigned NOT NULL,
  241. `processorHost` int(10) unsigned NOT NULL,
  242. `processDuration` float(6,2) NOT NULL,
  243. `totalDuration` smallint(5) unsigned NOT NULL,
  244. `error` tinyint(4) NOT NULL DEFAULT '0',
  245. `finished` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  246. KEY `finished` (`finished`),
  247. KEY `userID` (`userID`)
  248. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  249. CREATE TABLE `syncDownloadQueue` (
  250. `syncDownloadQueueID` int(10) unsigned NOT NULL,
  251. `processorHost` int(10) unsigned DEFAULT NULL,
  252. `userID` int(10) unsigned NOT NULL,
  253. `sessionID` char(32) CHARACTER SET ascii NOT NULL,
  254. `lastsync` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  255. `lastsyncMS` smallint(5) unsigned NOT NULL DEFAULT '0',
  256. `version` smallint(5) unsigned NOT NULL,
  257. `params` mediumtext NOT NULL,
  258. `added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  259. `objects` int(10) unsigned NOT NULL,
  260. `lastCheck` timestamp NULL DEFAULT NULL,
  261. `tries` smallint(5) unsigned NOT NULL DEFAULT '0',
  262. `started` timestamp NULL DEFAULT NULL,
  263. `syncDownloadProcessID` int(10) unsigned DEFAULT NULL,
  264. `finished` timestamp NULL DEFAULT NULL,
  265. `finishedMS` smallint(5) unsigned NOT NULL DEFAULT '0',
  266. `xmldata` longtext,
  267. `errorCode` int(10) unsigned DEFAULT NULL,
  268. `errorMessage` text,
  269. PRIMARY KEY (`syncDownloadQueueID`),
  270. KEY `userID` (`userID`),
  271. KEY `sessionID` (`sessionID`),
  272. KEY `started` (`started`),
  273. KEY `syncDownloadProcessID` (`syncDownloadProcessID`)
  274. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  275. CREATE TABLE `syncProcesses` (
  276. `syncProcessID` int(10) unsigned NOT NULL,
  277. `userID` int(10) unsigned NOT NULL,
  278. `started` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  279. PRIMARY KEY (`syncProcessID`),
  280. UNIQUE KEY `userID` (`userID`)
  281. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  282. CREATE TABLE `syncProcessLocks` (
  283. `syncProcessID` int(10) unsigned NOT NULL,
  284. `libraryID` int(10) unsigned NOT NULL,
  285. PRIMARY KEY (`syncProcessID`,`libraryID`),
  286. KEY `libraryID` (`libraryID`)
  287. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  288. CREATE TABLE `syncUploadQueue` (
  289. `syncUploadQueueID` int(10) unsigned NOT NULL,
  290. `processorHost` int(10) unsigned DEFAULT NULL,
  291. `xmldata` longtext NOT NULL,
  292. `dataLength` int(10) unsigned NOT NULL DEFAULT '0',
  293. `hasCreator` tinyint(3) unsigned NOT NULL DEFAULT '0',
  294. `userID` int(10) unsigned NOT NULL,
  295. `sessionID` char(32) CHARACTER SET ascii NOT NULL,
  296. `added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  297. `errorCheck` tinyint(1) unsigned NOT NULL DEFAULT '0',
  298. `tries` smallint(5) unsigned NOT NULL DEFAULT '0',
  299. `started` timestamp NULL DEFAULT NULL,
  300. `startedMS` smallint(5) unsigned NOT NULL DEFAULT '0',
  301. `syncProcessID` int(10) unsigned DEFAULT NULL,
  302. `finished` timestamp NULL DEFAULT NULL,
  303. `finishedMS` smallint(5) unsigned NOT NULL DEFAULT '0',
  304. `errorCode` int(10) unsigned DEFAULT NULL,
  305. `errorMessage` mediumtext,
  306. PRIMARY KEY (`syncUploadQueueID`),
  307. UNIQUE KEY `sessionID` (`sessionID`),
  308. UNIQUE KEY `syncProcessID` (`syncProcessID`),
  309. KEY `userID` (`userID`),
  310. KEY `started` (`started`)
  311. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  312. CREATE TABLE `syncUploadQueueLocks` (
  313. `syncUploadQueueID` int(10) unsigned NOT NULL,
  314. `libraryID` int(10) unsigned NOT NULL,
  315. PRIMARY KEY (`syncUploadQueueID`,`libraryID`),
  316. KEY `libraryID` (`libraryID`)
  317. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  318. CREATE TABLE `syncUploadProcessLog` (
  319. `userID` int(10) unsigned NOT NULL,
  320. `dataLength` int(10) unsigned NOT NULL,
  321. `processorHost` int(10) unsigned NOT NULL,
  322. `processDuration` float(6,2) NOT NULL,
  323. `totalDuration` smallint(5) unsigned NOT NULL,
  324. `error` tinyint(4) NOT NULL DEFAULT '0',
  325. `finished` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  326. KEY `finished` (`finished`),
  327. KEY `userID` (`userID`)
  328. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  329. CREATE TABLE `syncUploadQueuePostWriteLog` (
  330. `syncUploadQueueID` int(10) unsigned NOT NULL,
  331. `objectType` enum('group','groupUser') NOT NULL,
  332. `ids` varchar(30) NOT NULL,
  333. `action` enum('update','delete') NOT NULL,
  334. PRIMARY KEY (`syncUploadQueueID`,`objectType`,`ids`,`action`)
  335. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  336. CREATE TABLE `users` (
  337. `userID` int(10) unsigned NOT NULL,
  338. `libraryID` int(10) unsigned NOT NULL,
  339. `username` varchar(255) NOT NULL,
  340. `joined` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  341. `lastSyncTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  342. PRIMARY KEY (`userID`),
  343. UNIQUE KEY `libraryID` (`libraryID`)
  344. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  345. ALTER TABLE `baseFieldMappings`
  346. ADD CONSTRAINT `baseFieldMappings_ibfk_1` FOREIGN KEY (`itemTypeID`) REFERENCES `itemTypes` (`itemTypeID`),
  347. ADD CONSTRAINT `baseFieldMappings_ibfk_2` FOREIGN KEY (`baseFieldID`) REFERENCES `fields` (`fieldID`),
  348. ADD CONSTRAINT `baseFieldMappings_ibfk_3` FOREIGN KEY (`fieldID`) REFERENCES `fields` (`fieldID`);
  349. ALTER TABLE `groups`
  350. ADD CONSTRAINT `groups_ibfk_1` FOREIGN KEY (`libraryID`) REFERENCES `libraries` (`libraryID`) ON DELETE CASCADE;
  351. ALTER TABLE `groupUsers`
  352. ADD CONSTRAINT `groupUsers_ibfk_1` FOREIGN KEY (`groupID`) REFERENCES `groups` (`groupID`) ON DELETE CASCADE,
  353. ADD CONSTRAINT `groupUsers_ibfk_2` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`) ON DELETE CASCADE,
  354. ADD CONSTRAINT `groupUsers_ibfk_3` FOREIGN KEY (`groupID`) REFERENCES `groups` (`groupID`) ON DELETE CASCADE,
  355. ADD CONSTRAINT `groupUsers_ibfk_4` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`) ON DELETE CASCADE;
  356. ALTER TABLE `itemTypeCreatorTypes`
  357. ADD CONSTRAINT `itemTypeCreatorTypes_ibfk_1` FOREIGN KEY (`itemTypeID`) REFERENCES `itemTypes` (`itemTypeID`),
  358. ADD CONSTRAINT `itemTypeCreatorTypes_ibfk_2` FOREIGN KEY (`creatorTypeID`) REFERENCES `creatorTypes` (`creatorTypeID`);
  359. ALTER TABLE `itemTypeFields`
  360. ADD CONSTRAINT `itemTypeFields_ibfk_1` FOREIGN KEY (`itemTypeID`) REFERENCES `itemTypes` (`itemTypeID`),
  361. ADD CONSTRAINT `itemTypeFields_ibfk_2` FOREIGN KEY (`fieldID`) REFERENCES `fields` (`fieldID`);
  362. ALTER TABLE `keys`
  363. ADD CONSTRAINT `keys_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`) ON DELETE CASCADE;
  364. ALTER TABLE `keyPermissions`
  365. ADD CONSTRAINT `keyPermissions_ibfk_1` FOREIGN KEY (`keyID`) REFERENCES `keys` (`keyID`) ON DELETE CASCADE;
  366. ALTER TABLE `libraries`
  367. ADD CONSTRAINT `libraries_ibfk_1` FOREIGN KEY (`shardID`) REFERENCES `shards` (`shardID`);
  368. ALTER TABLE `sessions`
  369. ADD CONSTRAINT `sessions_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`) ON DELETE CASCADE;
  370. ALTER TABLE `shards`
  371. ADD CONSTRAINT `shards_ibfk_1` FOREIGN KEY (`shardHostID`) REFERENCES `shardHosts` (`shardHostID`);
  372. ALTER TABLE `storageAccounts`
  373. ADD CONSTRAINT `storageAccounts_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`) ON DELETE CASCADE ON UPDATE CASCADE;
  374. ALTER TABLE `storageLastSync`
  375. ADD CONSTRAINT `storageLastSync_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`) ON DELETE CASCADE ON UPDATE CASCADE;
  376. ALTER TABLE `storageUploadQueue`
  377. ADD CONSTRAINT `storageUploadQueue_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`) ON DELETE CASCADE ON UPDATE CASCADE;
  378. ALTER TABLE `syncDownloadQueue`
  379. ADD CONSTRAINT `syncDownloadQueue_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`) ON DELETE CASCADE ON UPDATE CASCADE,
  380. ADD CONSTRAINT `syncDownloadQueue_ibfk_3` FOREIGN KEY (`sessionID`) REFERENCES `sessions` (`sessionID`) ON DELETE CASCADE ON UPDATE CASCADE;
  381. ALTER TABLE `syncProcesses`
  382. ADD CONSTRAINT `syncProcesses_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`);
  383. ALTER TABLE `syncProcessLocks`
  384. ADD CONSTRAINT `syncProcessLocks_ibfk_1` FOREIGN KEY (`syncProcessID`) REFERENCES `syncProcesses` (`syncProcessID`) ON DELETE CASCADE ON UPDATE CASCADE,
  385. ADD CONSTRAINT `syncProcessLocks_ibfk_2` FOREIGN KEY (`libraryID`) REFERENCES `libraries` (`libraryID`) ON DELETE CASCADE ON UPDATE CASCADE;
  386. ALTER TABLE `syncUploadQueue`
  387. ADD CONSTRAINT `syncUploadQueue_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `users` (`userID`) ON DELETE CASCADE ON UPDATE CASCADE,
  388. ADD CONSTRAINT `syncUploadQueue_ibfk_2` FOREIGN KEY (`syncProcessID`) REFERENCES `syncProcesses` (`syncProcessID`) ON DELETE SET NULL,
  389. ADD CONSTRAINT `syncUploadQueue_ibfk_3` FOREIGN KEY (`sessionID`) REFERENCES `sessions` (`sessionID`) ON DELETE CASCADE ON UPDATE CASCADE;
  390. ALTER TABLE `syncUploadQueueLocks`
  391. ADD CONSTRAINT `syncUploadQueueLocks_ibfk_1` FOREIGN KEY (`syncUploadQueueID`) REFERENCES `syncUploadQueue` (`syncUploadQueueID`) ON DELETE CASCADE ON UPDATE CASCADE,
  392. ADD CONSTRAINT `syncUploadQueueLocks_ibfk_2` FOREIGN KEY (`libraryID`) REFERENCES `libraries` (`libraryID`) ON DELETE CASCADE ON UPDATE CASCADE;
  393. ALTER TABLE `syncUploadQueuePostWriteLog`
  394. ADD CONSTRAINT `syncUploadQueuePostWriteLog_ibfk_1` FOREIGN KEY (`syncUploadQueueID`) REFERENCES `syncUploadQueue` (`syncUploadQueueID`) ON DELETE CASCADE;
  395. ALTER TABLE `users`
  396. ADD CONSTRAINT `users_ibfk_1` FOREIGN KEY (`libraryID`) REFERENCES `libraries` (`libraryID`) ON DELETE CASCADE;
  397. CREATE EVENT sessionGC ON SCHEDULE EVERY 5 MINUTE DO
  398. DELETE S FROM sessions S LEFT JOIN syncUploadQueue USING (sessionID) WHERE timestamp<DATE_SUB(NOW(), INTERVAL 1 HOUR) AND (syncUploadQueue.sessionID IS NULL OR finished IS NOT NULL);