Version13000Date20170718121200.php 28 KB

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