Version13000Date20170718121200.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Mario Danic <mario@lovelyhq.com>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. *
  16. * @license GNU AGPL version 3 or any later version
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License as
  20. * published by the Free Software Foundation, either version 3 of the
  21. * License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. */
  32. namespace OC\Core\Migrations;
  33. use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
  34. use OCP\DB\ISchemaWrapper;
  35. use OCP\DB\Types;
  36. use OCP\IDBConnection;
  37. use OCP\Migration\IOutput;
  38. use OCP\Migration\SimpleMigrationStep;
  39. class Version13000Date20170718121200 extends SimpleMigrationStep {
  40. public function __construct(
  41. private IDBConnection $connection,
  42. ) {
  43. }
  44. public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
  45. /** @var ISchemaWrapper $schema */
  46. $schema = $schemaClosure();
  47. if (!$schema->hasTable('properties')) {
  48. return;
  49. }
  50. // in case we have a properties table from oc we drop it since we will only migrate
  51. // the dav_properties values in the postSchemaChange step
  52. $table = $schema->getTable('properties');
  53. if ($table->hasColumn('fileid')) {
  54. $qb = $this->connection->getQueryBuilder();
  55. $qb->delete('properties');
  56. $qb->execute();
  57. }
  58. }
  59. /**
  60. * @param IOutput $output
  61. * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
  62. * @param array $options
  63. * @return null|ISchemaWrapper
  64. * @since 13.0.0
  65. */
  66. public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
  67. /** @var ISchemaWrapper $schema */
  68. $schema = $schemaClosure();
  69. if (!$schema->hasTable('appconfig')) {
  70. $table = $schema->createTable('appconfig');
  71. $table->addColumn('appid', 'string', [
  72. 'notnull' => true,
  73. 'length' => 32,
  74. 'default' => '',
  75. ]);
  76. $table->addColumn('configkey', 'string', [
  77. 'notnull' => true,
  78. 'length' => 64,
  79. 'default' => '',
  80. ]);
  81. $table->addColumn('configvalue', 'text', [
  82. 'notnull' => false,
  83. ]);
  84. $table->setPrimaryKey(['appid', 'configkey']);
  85. $table->addIndex(['configkey'], 'appconfig_config_key_index');
  86. $table->addIndex(['appid'], 'appconfig_appid_key');
  87. }
  88. if (!$schema->hasTable('storages')) {
  89. $table = $schema->createTable('storages');
  90. $table->addColumn('id', 'string', [
  91. 'notnull' => false,
  92. 'length' => 64,
  93. ]);
  94. $table->addColumn('numeric_id', Types::BIGINT, [
  95. 'autoincrement' => true,
  96. 'notnull' => true,
  97. 'length' => 20,
  98. ]);
  99. $table->addColumn('available', 'integer', [
  100. 'notnull' => true,
  101. 'default' => 1,
  102. ]);
  103. $table->addColumn('last_checked', 'integer', [
  104. 'notnull' => false,
  105. ]);
  106. $table->setPrimaryKey(['numeric_id']);
  107. $table->addUniqueIndex(['id'], 'storages_id_index');
  108. }
  109. if (!$schema->hasTable('mounts')) {
  110. $table = $schema->createTable('mounts');
  111. $table->addColumn('id', 'integer', [
  112. 'autoincrement' => true,
  113. 'notnull' => true,
  114. 'length' => 4,
  115. ]);
  116. $table->addColumn('storage_id', Types::BIGINT, [
  117. 'notnull' => true,
  118. 'length' => 20,
  119. ]);
  120. $table->addColumn('root_id', Types::BIGINT, [
  121. 'notnull' => true,
  122. 'length' => 20,
  123. ]);
  124. $table->addColumn('user_id', 'string', [
  125. 'notnull' => true,
  126. 'length' => 64,
  127. ]);
  128. $table->addColumn('mount_point', 'string', [
  129. 'notnull' => true,
  130. 'length' => 4000,
  131. ]);
  132. $table->addColumn('mount_id', Types::BIGINT, [
  133. 'notnull' => false,
  134. 'length' => 20,
  135. ]);
  136. $table->setPrimaryKey(['id']);
  137. $table->addIndex(['user_id'], 'mounts_user_index');
  138. $table->addIndex(['storage_id'], 'mounts_storage_index');
  139. $table->addIndex(['root_id'], 'mounts_root_index');
  140. $table->addIndex(['mount_id'], 'mounts_mount_id_index');
  141. $table->addIndex(['user_id', 'root_id', 'mount_point'], 'mounts_user_root_path_index', [], ['lengths' => [null, null, 128]]);
  142. } else {
  143. $table = $schema->getTable('mounts');
  144. if (!$table->hasColumn('mount_id')) {
  145. $table->addColumn('mount_id', Types::BIGINT, [
  146. 'notnull' => false,
  147. 'length' => 20,
  148. ]);
  149. }
  150. if (!$table->hasIndex('mounts_mount_id_index')) {
  151. $table->addIndex(['mount_id'], 'mounts_mount_id_index');
  152. }
  153. }
  154. if (!$schema->hasTable('mimetypes')) {
  155. $table = $schema->createTable('mimetypes');
  156. $table->addColumn('id', Types::BIGINT, [
  157. 'autoincrement' => true,
  158. 'notnull' => true,
  159. 'length' => 20,
  160. ]);
  161. $table->addColumn('mimetype', 'string', [
  162. 'notnull' => true,
  163. 'length' => 255,
  164. 'default' => '',
  165. ]);
  166. $table->setPrimaryKey(['id']);
  167. $table->addUniqueIndex(['mimetype'], 'mimetype_id_index');
  168. }
  169. if (!$schema->hasTable('filecache')) {
  170. $table = $schema->createTable('filecache');
  171. $table->addColumn('fileid', Types::BIGINT, [
  172. 'autoincrement' => true,
  173. 'notnull' => true,
  174. 'length' => 20,
  175. ]);
  176. $table->addColumn('storage', Types::BIGINT, [
  177. 'notnull' => true,
  178. 'length' => 20,
  179. 'default' => 0,
  180. ]);
  181. $table->addColumn('path', 'string', [
  182. 'notnull' => false,
  183. 'length' => 4000,
  184. ]);
  185. $table->addColumn('path_hash', 'string', [
  186. 'notnull' => true,
  187. 'length' => 32,
  188. 'default' => '',
  189. ]);
  190. $table->addColumn('parent', Types::BIGINT, [
  191. 'notnull' => true,
  192. 'length' => 20,
  193. 'default' => 0,
  194. ]);
  195. $table->addColumn('name', 'string', [
  196. 'notnull' => false,
  197. 'length' => 250,
  198. ]);
  199. $table->addColumn('mimetype', Types::BIGINT, [
  200. 'notnull' => true,
  201. 'length' => 20,
  202. 'default' => 0,
  203. ]);
  204. $table->addColumn('mimepart', Types::BIGINT, [
  205. 'notnull' => true,
  206. 'length' => 20,
  207. 'default' => 0,
  208. ]);
  209. $table->addColumn('size', 'bigint', [
  210. 'notnull' => true,
  211. 'length' => 8,
  212. 'default' => 0,
  213. ]);
  214. $table->addColumn('mtime', Types::BIGINT, [
  215. 'notnull' => true,
  216. 'length' => 20,
  217. 'default' => 0,
  218. ]);
  219. $table->addColumn('storage_mtime', Types::BIGINT, [
  220. 'notnull' => true,
  221. 'length' => 20,
  222. 'default' => 0,
  223. ]);
  224. $table->addColumn('encrypted', 'integer', [
  225. 'notnull' => true,
  226. 'length' => 4,
  227. 'default' => 0,
  228. ]);
  229. $table->addColumn('unencrypted_size', 'bigint', [
  230. 'notnull' => true,
  231. 'length' => 8,
  232. 'default' => 0,
  233. ]);
  234. $table->addColumn('etag', 'string', [
  235. 'notnull' => false,
  236. 'length' => 40,
  237. ]);
  238. $table->addColumn('permissions', 'integer', [
  239. 'notnull' => false,
  240. 'length' => 4,
  241. 'default' => 0,
  242. ]);
  243. $table->addColumn('checksum', 'string', [
  244. 'notnull' => false,
  245. 'length' => 255,
  246. ]);
  247. $table->setPrimaryKey(['fileid']);
  248. $table->addUniqueIndex(['storage', 'path_hash'], 'fs_storage_path_hash');
  249. $table->addIndex(['parent', 'name'], 'fs_parent_name_hash');
  250. $table->addIndex(['storage', 'mimetype'], 'fs_storage_mimetype');
  251. $table->addIndex(['storage', 'mimepart'], 'fs_storage_mimepart');
  252. $table->addIndex(['storage', 'size', 'fileid'], 'fs_storage_size');
  253. $table->addIndex(['fileid', 'storage', 'size'], 'fs_id_storage_size');
  254. $table->addIndex(['parent'], 'fs_parent');
  255. $table->addIndex(['mtime'], 'fs_mtime');
  256. $table->addIndex(['size'], 'fs_size');
  257. if (!$schema->getDatabasePlatform() instanceof PostgreSQL94Platform) {
  258. $table->addIndex(['storage', 'path'], 'fs_storage_path_prefix', [], ['lengths' => [null, 64]]);
  259. }
  260. }
  261. if (!$schema->hasTable('group_user')) {
  262. $table = $schema->createTable('group_user');
  263. $table->addColumn('gid', 'string', [
  264. 'notnull' => true,
  265. 'length' => 64,
  266. 'default' => '',
  267. ]);
  268. $table->addColumn('uid', 'string', [
  269. 'notnull' => true,
  270. 'length' => 64,
  271. 'default' => '',
  272. ]);
  273. $table->setPrimaryKey(['gid', 'uid']);
  274. $table->addIndex(['uid'], 'gu_uid_index');
  275. }
  276. if (!$schema->hasTable('group_admin')) {
  277. $table = $schema->createTable('group_admin');
  278. $table->addColumn('gid', 'string', [
  279. 'notnull' => true,
  280. 'length' => 64,
  281. 'default' => '',
  282. ]);
  283. $table->addColumn('uid', 'string', [
  284. 'notnull' => true,
  285. 'length' => 64,
  286. 'default' => '',
  287. ]);
  288. $table->setPrimaryKey(['gid', 'uid']);
  289. $table->addIndex(['uid'], 'group_admin_uid');
  290. }
  291. if (!$schema->hasTable('groups')) {
  292. $table = $schema->createTable('groups');
  293. $table->addColumn('gid', 'string', [
  294. 'notnull' => true,
  295. 'length' => 64,
  296. 'default' => '',
  297. ]);
  298. $table->setPrimaryKey(['gid']);
  299. }
  300. if (!$schema->hasTable('preferences')) {
  301. $table = $schema->createTable('preferences');
  302. $table->addColumn('userid', 'string', [
  303. 'notnull' => true,
  304. 'length' => 64,
  305. 'default' => '',
  306. ]);
  307. $table->addColumn('appid', 'string', [
  308. 'notnull' => true,
  309. 'length' => 32,
  310. 'default' => '',
  311. ]);
  312. $table->addColumn('configkey', 'string', [
  313. 'notnull' => true,
  314. 'length' => 64,
  315. 'default' => '',
  316. ]);
  317. $table->addColumn('configvalue', 'text', [
  318. 'notnull' => false,
  319. ]);
  320. $table->setPrimaryKey(['userid', 'appid', 'configkey']);
  321. $table->addIndex(['appid', 'configkey'], 'preferences_app_key');
  322. }
  323. if (!$schema->hasTable('properties')) {
  324. $table = $schema->createTable('properties');
  325. $table->addColumn('id', 'integer', [
  326. 'autoincrement' => true,
  327. 'notnull' => true,
  328. 'length' => 4,
  329. ]);
  330. $table->addColumn('userid', 'string', [
  331. 'notnull' => true,
  332. 'length' => 64,
  333. 'default' => '',
  334. ]);
  335. $table->addColumn('propertypath', 'string', [
  336. 'notnull' => true,
  337. 'length' => 255,
  338. 'default' => '',
  339. ]);
  340. $table->addColumn('propertyname', 'string', [
  341. 'notnull' => true,
  342. 'length' => 255,
  343. 'default' => '',
  344. ]);
  345. $table->addColumn('propertyvalue', 'text', [
  346. 'notnull' => true,
  347. ]);
  348. $table->setPrimaryKey(['id']);
  349. // Dropped in Version29000Date20240131122720 because property_index is redundant with properties_path_index
  350. // $table->addIndex(['userid'], 'property_index');
  351. $table->addIndex(['userid', 'propertypath'], 'properties_path_index');
  352. $table->addIndex(['propertypath'], 'properties_pathonly_index');
  353. } else {
  354. $table = $schema->getTable('properties');
  355. if ($table->hasColumn('propertytype')) {
  356. $table->dropColumn('propertytype');
  357. }
  358. if ($table->hasColumn('fileid')) {
  359. $table->dropColumn('fileid');
  360. }
  361. if (!$table->hasColumn('propertypath')) {
  362. $table->addColumn('propertypath', 'string', [
  363. 'notnull' => true,
  364. 'length' => 255,
  365. ]);
  366. }
  367. if (!$table->hasColumn('userid')) {
  368. $table->addColumn('userid', 'string', [
  369. 'notnull' => false,
  370. 'length' => 64,
  371. 'default' => '',
  372. ]);
  373. }
  374. }
  375. if (!$schema->hasTable('share')) {
  376. $table = $schema->createTable('share');
  377. $table->addColumn('id', 'integer', [
  378. 'autoincrement' => true,
  379. 'notnull' => true,
  380. 'length' => 4,
  381. ]);
  382. $table->addColumn('share_type', 'smallint', [
  383. 'notnull' => true,
  384. 'length' => 1,
  385. 'default' => 0,
  386. ]);
  387. $table->addColumn('share_with', 'string', [
  388. 'notnull' => false,
  389. 'length' => 255,
  390. ]);
  391. $table->addColumn('password', 'string', [
  392. 'notnull' => false,
  393. 'length' => 255,
  394. ]);
  395. $table->addColumn('uid_owner', 'string', [
  396. 'notnull' => true,
  397. 'length' => 64,
  398. 'default' => '',
  399. ]);
  400. $table->addColumn('uid_initiator', 'string', [
  401. 'notnull' => false,
  402. 'length' => 64,
  403. ]);
  404. $table->addColumn('parent', 'integer', [
  405. 'notnull' => false,
  406. 'length' => 4,
  407. ]);
  408. $table->addColumn('item_type', 'string', [
  409. 'notnull' => true,
  410. 'length' => 64,
  411. 'default' => '',
  412. ]);
  413. $table->addColumn('item_source', 'string', [
  414. 'notnull' => false,
  415. 'length' => 255,
  416. ]);
  417. $table->addColumn('item_target', 'string', [
  418. 'notnull' => false,
  419. 'length' => 255,
  420. ]);
  421. $table->addColumn('file_source', 'integer', [
  422. 'notnull' => false,
  423. 'length' => 4,
  424. ]);
  425. $table->addColumn('file_target', 'string', [
  426. 'notnull' => false,
  427. 'length' => 512,
  428. ]);
  429. $table->addColumn('permissions', 'smallint', [
  430. 'notnull' => true,
  431. 'length' => 1,
  432. 'default' => 0,
  433. ]);
  434. $table->addColumn('stime', 'bigint', [
  435. 'notnull' => true,
  436. 'length' => 8,
  437. 'default' => 0,
  438. ]);
  439. $table->addColumn('accepted', 'smallint', [
  440. 'notnull' => true,
  441. 'length' => 1,
  442. 'default' => 0,
  443. ]);
  444. $table->addColumn('expiration', 'datetime', [
  445. 'notnull' => false,
  446. ]);
  447. $table->addColumn('token', 'string', [
  448. 'notnull' => false,
  449. 'length' => 32,
  450. ]);
  451. $table->addColumn('mail_send', 'smallint', [
  452. 'notnull' => true,
  453. 'length' => 1,
  454. 'default' => 0,
  455. ]);
  456. $table->addColumn('share_name', 'string', [
  457. 'notnull' => false,
  458. 'length' => 64,
  459. ]);
  460. $table->setPrimaryKey(['id']);
  461. $table->addIndex(['item_type', 'share_type'], 'item_share_type_index');
  462. $table->addIndex(['file_source'], 'file_source_index');
  463. $table->addIndex(['token'], 'token_index');
  464. $table->addIndex(['share_with'], 'share_with_index');
  465. $table->addIndex(['parent'], 'parent_index');
  466. $table->addIndex(['uid_owner'], 'owner_index');
  467. $table->addIndex(['uid_initiator'], 'initiator_index');
  468. } else {
  469. $table = $schema->getTable('share');
  470. if (!$table->hasColumn('password')) {
  471. $table->addColumn('password', 'string', [
  472. 'notnull' => false,
  473. 'length' => 255,
  474. ]);
  475. }
  476. }
  477. if (!$schema->hasTable('jobs')) {
  478. $table = $schema->createTable('jobs');
  479. $table->addColumn('id', 'integer', [
  480. 'autoincrement' => true,
  481. 'notnull' => true,
  482. 'length' => 4,
  483. 'unsigned' => true,
  484. ]);
  485. $table->addColumn('class', 'string', [
  486. 'notnull' => true,
  487. 'length' => 255,
  488. 'default' => '',
  489. ]);
  490. $table->addColumn('argument', 'string', [
  491. 'notnull' => true,
  492. 'length' => 4000,
  493. 'default' => '',
  494. ]);
  495. $table->addColumn('last_run', 'integer', [
  496. 'notnull' => false,
  497. 'default' => 0,
  498. ]);
  499. $table->addColumn('last_checked', 'integer', [
  500. 'notnull' => false,
  501. 'default' => 0,
  502. ]);
  503. $table->addColumn('reserved_at', 'integer', [
  504. 'notnull' => false,
  505. 'default' => 0,
  506. ]);
  507. $table->addColumn('execution_duration', 'integer', [
  508. 'notnull' => true,
  509. 'default' => 0,
  510. ]);
  511. $table->setPrimaryKey(['id']);
  512. $table->addIndex(['class'], 'job_class_index');
  513. $table->addIndex(['last_checked', 'reserved_at'], 'job_lastcheck_reserved');
  514. }
  515. if (!$schema->hasTable('users')) {
  516. $table = $schema->createTable('users');
  517. $table->addColumn('uid', 'string', [
  518. 'notnull' => true,
  519. 'length' => 64,
  520. 'default' => '',
  521. ]);
  522. $table->addColumn('displayname', 'string', [
  523. 'notnull' => false,
  524. 'length' => 64,
  525. ]);
  526. $table->addColumn('password', 'string', [
  527. 'notnull' => true,
  528. 'length' => 255,
  529. 'default' => '',
  530. ]);
  531. $table->setPrimaryKey(['uid']);
  532. }
  533. if (!$schema->hasTable('authtoken')) {
  534. $table = $schema->createTable('authtoken');
  535. $table->addColumn('id', 'integer', [
  536. 'autoincrement' => true,
  537. 'notnull' => true,
  538. 'length' => 4,
  539. 'unsigned' => true,
  540. ]);
  541. $table->addColumn('uid', 'string', [
  542. 'notnull' => true,
  543. 'length' => 64,
  544. 'default' => '',
  545. ]);
  546. $table->addColumn('login_name', 'string', [
  547. 'notnull' => true,
  548. 'length' => 64,
  549. 'default' => '',
  550. ]);
  551. $table->addColumn('password', 'text', [
  552. 'notnull' => false,
  553. ]);
  554. $table->addColumn('name', 'text', [
  555. 'notnull' => true,
  556. 'default' => '',
  557. ]);
  558. $table->addColumn('token', 'string', [
  559. 'notnull' => true,
  560. 'length' => 200,
  561. 'default' => '',
  562. ]);
  563. $table->addColumn('type', 'smallint', [
  564. 'notnull' => false,
  565. 'length' => 2,
  566. 'default' => 0,
  567. 'unsigned' => true,
  568. ]);
  569. $table->addColumn('remember', 'smallint', [
  570. 'notnull' => false,
  571. 'length' => 1,
  572. 'default' => 0,
  573. 'unsigned' => true,
  574. ]);
  575. $table->addColumn('last_activity', 'integer', [
  576. 'notnull' => false,
  577. 'length' => 4,
  578. 'default' => 0,
  579. 'unsigned' => true,
  580. ]);
  581. $table->addColumn('last_check', 'integer', [
  582. 'notnull' => false,
  583. 'length' => 4,
  584. 'default' => 0,
  585. 'unsigned' => true,
  586. ]);
  587. $table->addColumn('scope', 'text', [
  588. 'notnull' => false,
  589. ]);
  590. $table->setPrimaryKey(['id']);
  591. $table->addUniqueIndex(['token'], 'authtoken_token_index');
  592. $table->addIndex(['last_activity'], 'authtoken_last_activity_idx');
  593. } else {
  594. $table = $schema->getTable('authtoken');
  595. $table->addColumn('scope', 'text', [
  596. 'notnull' => false,
  597. ]);
  598. }
  599. if (!$schema->hasTable('bruteforce_attempts')) {
  600. $table = $schema->createTable('bruteforce_attempts');
  601. $table->addColumn('id', 'integer', [
  602. 'autoincrement' => true,
  603. 'notnull' => true,
  604. 'length' => 4,
  605. 'unsigned' => true,
  606. ]);
  607. $table->addColumn('action', 'string', [
  608. 'notnull' => true,
  609. 'length' => 64,
  610. 'default' => '',
  611. ]);
  612. $table->addColumn('occurred', 'integer', [
  613. 'notnull' => true,
  614. 'length' => 4,
  615. 'default' => 0,
  616. 'unsigned' => true,
  617. ]);
  618. $table->addColumn('ip', 'string', [
  619. 'notnull' => true,
  620. 'length' => 255,
  621. 'default' => '',
  622. ]);
  623. $table->addColumn('subnet', 'string', [
  624. 'notnull' => true,
  625. 'length' => 255,
  626. 'default' => '',
  627. ]);
  628. $table->addColumn('metadata', 'string', [
  629. 'notnull' => true,
  630. 'length' => 255,
  631. 'default' => '',
  632. ]);
  633. $table->setPrimaryKey(['id']);
  634. $table->addIndex(['ip'], 'bruteforce_attempts_ip');
  635. $table->addIndex(['subnet'], 'bruteforce_attempts_subnet');
  636. }
  637. if (!$schema->hasTable('vcategory')) {
  638. $table = $schema->createTable('vcategory');
  639. $table->addColumn('id', 'integer', [
  640. 'autoincrement' => true,
  641. 'notnull' => true,
  642. 'length' => 4,
  643. 'unsigned' => true,
  644. ]);
  645. $table->addColumn('uid', 'string', [
  646. 'notnull' => true,
  647. 'length' => 64,
  648. 'default' => '',
  649. ]);
  650. $table->addColumn('type', 'string', [
  651. 'notnull' => true,
  652. 'length' => 64,
  653. 'default' => '',
  654. ]);
  655. $table->addColumn('category', 'string', [
  656. 'notnull' => true,
  657. 'length' => 255,
  658. 'default' => '',
  659. ]);
  660. $table->setPrimaryKey(['id']);
  661. $table->addIndex(['uid'], 'uid_index');
  662. $table->addIndex(['type'], 'type_index');
  663. $table->addIndex(['category'], 'category_index');
  664. }
  665. if (!$schema->hasTable('vcategory_to_object')) {
  666. $table = $schema->createTable('vcategory_to_object');
  667. $table->addColumn('objid', 'integer', [
  668. 'notnull' => true,
  669. 'length' => 4,
  670. 'default' => 0,
  671. 'unsigned' => true,
  672. ]);
  673. $table->addColumn('categoryid', 'integer', [
  674. 'notnull' => true,
  675. 'length' => 4,
  676. 'default' => 0,
  677. 'unsigned' => true,
  678. ]);
  679. $table->addColumn('type', 'string', [
  680. 'notnull' => true,
  681. 'length' => 64,
  682. 'default' => '',
  683. ]);
  684. $table->setPrimaryKey(['categoryid', 'objid', 'type']);
  685. $table->addIndex(['objid', 'type'], 'vcategory_objectd_index');
  686. }
  687. if (!$schema->hasTable('systemtag')) {
  688. $table = $schema->createTable('systemtag');
  689. $table->addColumn('id', 'integer', [
  690. 'autoincrement' => true,
  691. 'notnull' => true,
  692. 'length' => 4,
  693. 'unsigned' => true,
  694. ]);
  695. $table->addColumn('name', 'string', [
  696. 'notnull' => true,
  697. 'length' => 64,
  698. 'default' => '',
  699. ]);
  700. $table->addColumn('visibility', 'smallint', [
  701. 'notnull' => true,
  702. 'length' => 1,
  703. 'default' => 1,
  704. ]);
  705. $table->addColumn('editable', 'smallint', [
  706. 'notnull' => true,
  707. 'length' => 1,
  708. 'default' => 1,
  709. ]);
  710. $table->setPrimaryKey(['id']);
  711. $table->addUniqueIndex(['name', 'visibility', 'editable'], 'tag_ident');
  712. }
  713. if (!$schema->hasTable('systemtag_object_mapping')) {
  714. $table = $schema->createTable('systemtag_object_mapping');
  715. $table->addColumn('objectid', 'string', [
  716. 'notnull' => true,
  717. 'length' => 64,
  718. 'default' => '',
  719. ]);
  720. $table->addColumn('objecttype', 'string', [
  721. 'notnull' => true,
  722. 'length' => 64,
  723. 'default' => '',
  724. ]);
  725. $table->addColumn('systemtagid', 'integer', [
  726. 'notnull' => true,
  727. 'length' => 4,
  728. 'default' => 0,
  729. 'unsigned' => true,
  730. ]);
  731. $table->setPrimaryKey(['objecttype', 'objectid', 'systemtagid'], 'som_pk');
  732. // $table->addUniqueIndex(['objecttype', 'objectid', 'systemtagid'], 'mapping');
  733. $table->addIndex(['systemtagid', 'objecttype'], 'systag_by_tagid');
  734. }
  735. if (!$schema->hasTable('systemtag_group')) {
  736. $table = $schema->createTable('systemtag_group');
  737. $table->addColumn('systemtagid', 'integer', [
  738. 'notnull' => true,
  739. 'length' => 4,
  740. 'default' => 0,
  741. 'unsigned' => true,
  742. ]);
  743. $table->addColumn('gid', 'string', [
  744. 'notnull' => true,
  745. ]);
  746. $table->setPrimaryKey(['gid', 'systemtagid']);
  747. }
  748. if (!$schema->hasTable('file_locks')) {
  749. $table = $schema->createTable('file_locks');
  750. $table->addColumn('id', 'integer', [
  751. 'autoincrement' => true,
  752. 'notnull' => true,
  753. 'length' => 4,
  754. 'unsigned' => true,
  755. ]);
  756. $table->addColumn('lock', 'integer', [
  757. 'notnull' => true,
  758. 'length' => 4,
  759. 'default' => 0,
  760. ]);
  761. $table->addColumn('key', 'string', [
  762. 'notnull' => true,
  763. 'length' => 64,
  764. ]);
  765. $table->addColumn('ttl', 'integer', [
  766. 'notnull' => true,
  767. 'length' => 4,
  768. 'default' => -1,
  769. ]);
  770. $table->setPrimaryKey(['id']);
  771. $table->addUniqueIndex(['key'], 'lock_key_index');
  772. $table->addIndex(['ttl'], 'lock_ttl_index');
  773. }
  774. if (!$schema->hasTable('comments')) {
  775. $table = $schema->createTable('comments');
  776. $table->addColumn('id', 'integer', [
  777. 'autoincrement' => true,
  778. 'notnull' => true,
  779. 'length' => 4,
  780. 'unsigned' => true,
  781. ]);
  782. $table->addColumn('parent_id', 'integer', [
  783. 'notnull' => true,
  784. 'length' => 4,
  785. 'default' => 0,
  786. 'unsigned' => true,
  787. ]);
  788. $table->addColumn('topmost_parent_id', 'integer', [
  789. 'notnull' => true,
  790. 'length' => 4,
  791. 'default' => 0,
  792. 'unsigned' => true,
  793. ]);
  794. $table->addColumn('children_count', 'integer', [
  795. 'notnull' => true,
  796. 'length' => 4,
  797. 'default' => 0,
  798. 'unsigned' => true,
  799. ]);
  800. $table->addColumn('actor_type', 'string', [
  801. 'notnull' => true,
  802. 'length' => 64,
  803. 'default' => '',
  804. ]);
  805. $table->addColumn('actor_id', 'string', [
  806. 'notnull' => true,
  807. 'length' => 64,
  808. 'default' => '',
  809. ]);
  810. $table->addColumn('message', 'text', [
  811. 'notnull' => false,
  812. ]);
  813. $table->addColumn('verb', 'string', [
  814. 'notnull' => false,
  815. 'length' => 64,
  816. ]);
  817. $table->addColumn('creation_timestamp', 'datetime', [
  818. 'notnull' => false,
  819. ]);
  820. $table->addColumn('latest_child_timestamp', 'datetime', [
  821. 'notnull' => false,
  822. ]);
  823. $table->addColumn('object_type', 'string', [
  824. 'notnull' => true,
  825. 'length' => 64,
  826. 'default' => '',
  827. ]);
  828. $table->addColumn('object_id', 'string', [
  829. 'notnull' => true,
  830. 'length' => 64,
  831. 'default' => '',
  832. ]);
  833. $table->addColumn('reference_id', 'string', [
  834. 'notnull' => false,
  835. 'length' => 64,
  836. ]);
  837. $table->setPrimaryKey(['id']);
  838. $table->addIndex(['parent_id'], 'comments_parent_id_index');
  839. $table->addIndex(['topmost_parent_id'], 'comments_topmost_parent_id_idx');
  840. $table->addIndex(['object_type', 'object_id', 'creation_timestamp'], 'comments_object_index');
  841. $table->addIndex(['actor_type', 'actor_id'], 'comments_actor_index');
  842. }
  843. if (!$schema->hasTable('comments_read_markers')) {
  844. $table = $schema->createTable('comments_read_markers');
  845. $table->addColumn('user_id', 'string', [
  846. 'notnull' => true,
  847. 'length' => 64,
  848. 'default' => '',
  849. ]);
  850. $table->addColumn('marker_datetime', 'datetime', [
  851. 'notnull' => false,
  852. ]);
  853. $table->addColumn('object_type', 'string', [
  854. 'notnull' => true,
  855. 'length' => 64,
  856. 'default' => '',
  857. ]);
  858. $table->addColumn('object_id', 'string', [
  859. 'notnull' => true,
  860. 'length' => 64,
  861. 'default' => '',
  862. ]);
  863. $table->addIndex(['object_type', 'object_id'], 'comments_marker_object_index');
  864. $table->setPrimaryKey(['user_id', 'object_type', 'object_id'], 'crm_pk');
  865. // $table->addUniqueIndex(['user_id', 'object_type', 'object_id'], 'comments_marker_index');
  866. }
  867. // if (!$schema->hasTable('credentials')) {
  868. // $table = $schema->createTable('credentials');
  869. // $table->addColumn('user', 'string', [
  870. // 'notnull' => false,
  871. // 'length' => 64,
  872. // ]);
  873. // $table->addColumn('identifier', 'string', [
  874. // 'notnull' => true,
  875. // 'length' => 64,
  876. // ]);
  877. // $table->addColumn('credentials', 'text', [
  878. // 'notnull' => false,
  879. // ]);
  880. // $table->setPrimaryKey(['user', 'identifier']);
  881. // $table->addIndex(['user'], 'credentials_user');
  882. // }
  883. if (!$schema->hasTable('admin_sections')) {
  884. $table = $schema->createTable('admin_sections');
  885. $table->addColumn('id', 'string', [
  886. 'notnull' => true,
  887. 'length' => 64,
  888. ]);
  889. $table->addColumn('class', 'string', [
  890. 'notnull' => true,
  891. 'length' => 255,
  892. 'default' => '',
  893. ]);
  894. $table->addColumn('priority', 'smallint', [
  895. 'notnull' => true,
  896. 'length' => 1,
  897. 'default' => 0,
  898. ]);
  899. $table->setPrimaryKey(['id']);
  900. $table->addUniqueIndex(['class'], 'admin_sections_class');
  901. }
  902. if (!$schema->hasTable('admin_settings')) {
  903. $table = $schema->createTable('admin_settings');
  904. $table->addColumn('id', 'integer', [
  905. 'autoincrement' => true,
  906. 'notnull' => true,
  907. 'length' => 4,
  908. ]);
  909. $table->addColumn('class', 'string', [
  910. 'notnull' => true,
  911. 'length' => 255,
  912. 'default' => '',
  913. ]);
  914. $table->addColumn('section', 'string', [
  915. 'notnull' => false,
  916. 'length' => 64,
  917. ]);
  918. $table->addColumn('priority', 'smallint', [
  919. 'notnull' => true,
  920. 'length' => 1,
  921. 'default' => 0,
  922. ]);
  923. $table->setPrimaryKey(['id']);
  924. $table->addUniqueIndex(['class'], 'admin_settings_class');
  925. $table->addIndex(['section'], 'admin_settings_section');
  926. }
  927. if (!$schema->hasTable('personal_sections')) {
  928. $table = $schema->createTable('personal_sections');
  929. $table->addColumn('id', 'string', [
  930. 'notnull' => true,
  931. 'length' => 64,
  932. ]);
  933. $table->addColumn('class', 'string', [
  934. 'notnull' => true,
  935. 'length' => 255,
  936. 'default' => '',
  937. ]);
  938. $table->addColumn('priority', 'smallint', [
  939. 'notnull' => true,
  940. 'length' => 1,
  941. 'default' => 0,
  942. ]);
  943. $table->setPrimaryKey(['id']);
  944. $table->addUniqueIndex(['class'], 'personal_sections_class');
  945. }
  946. if (!$schema->hasTable('personal_settings')) {
  947. $table = $schema->createTable('personal_settings');
  948. $table->addColumn('id', 'integer', [
  949. 'autoincrement' => true,
  950. 'notnull' => true,
  951. 'length' => 4,
  952. ]);
  953. $table->addColumn('class', 'string', [
  954. 'notnull' => true,
  955. 'length' => 255,
  956. 'default' => '',
  957. ]);
  958. $table->addColumn('section', 'string', [
  959. 'notnull' => false,
  960. 'length' => 64,
  961. ]);
  962. $table->addColumn('priority', 'smallint', [
  963. 'notnull' => true,
  964. 'length' => 1,
  965. 'default' => 0,
  966. ]);
  967. $table->setPrimaryKey(['id']);
  968. $table->addUniqueIndex(['class'], 'personal_settings_class');
  969. $table->addIndex(['section'], 'personal_settings_section');
  970. }
  971. if (!$schema->hasTable('accounts')) {
  972. $table = $schema->createTable('accounts');
  973. $table->addColumn('uid', 'string', [
  974. 'notnull' => true,
  975. 'length' => 64,
  976. 'default' => '',
  977. ]);
  978. $table->addColumn('data', 'text', [
  979. 'notnull' => true,
  980. 'default' => '',
  981. ]);
  982. $table->setPrimaryKey(['uid']);
  983. }
  984. return $schema;
  985. }
  986. public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
  987. /** @var ISchemaWrapper $schema */
  988. $schema = $schemaClosure();
  989. if (!$schema->hasTable('dav_properties')) {
  990. return;
  991. }
  992. $query = $this->connection->getQueryBuilder();
  993. $query->select('*')
  994. ->from('dav_properties');
  995. $insert = $this->connection->getQueryBuilder();
  996. $insert->insert('properties')
  997. ->setValue('propertypath', $insert->createParameter('propertypath'))
  998. ->setValue('propertyname', $insert->createParameter('propertyname'))
  999. ->setValue('propertyvalue', $insert->createParameter('propertyvalue'))
  1000. ->setValue('userid', $insert->createParameter('userid'));
  1001. $result = $query->execute();
  1002. while ($row = $result->fetch()) {
  1003. preg_match('/(calendar)\/([A-z0-9-@_]+)\//', $row['propertypath'], $match);
  1004. $insert->setParameter('propertypath', (string) $row['propertypath'])
  1005. ->setParameter('propertyname', (string) $row['propertyname'])
  1006. ->setParameter('propertyvalue', (string) $row['propertyvalue'])
  1007. ->setParameter('userid', ($match[2] ?? ''));
  1008. $insert->execute();
  1009. }
  1010. }
  1011. }