1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ /**
6+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+ * SPDX-License-Identifier: AGPL-3.0-or-later
8+ */
9+
10+ namespace OCA \Tables \Migration ;
11+
12+ use Closure ;
13+ use Doctrine \DBAL \Exception ;
14+ use OCP \DB \ISchemaWrapper ;
15+ use OCP \DB \Types ;
16+ use OCP \Migration \IOutput ;
17+ use OCP \Migration \SimpleMigrationStep ;
18+
19+ class Version001000Date20240328000000 extends SimpleMigrationStep {
20+ /**
21+ * @param IOutput $output
22+ * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
23+ * @param array $options
24+ * @return null|ISchemaWrapper
25+ * @throws Exception
26+ */
27+ public function changeSchema (IOutput $ output , Closure $ schemaClosure , array $ options ): ?ISchemaWrapper {
28+ /** @var ISchemaWrapper $schema */
29+ $ schema = $ schemaClosure ();
30+
31+ if ($ schema ->hasTable ('tables_views ' )) {
32+ $ table = $ schema ->getTable ('tables_views ' );
33+
34+ // Add new column_settings field
35+ if (!$ table ->hasColumn ('column_settings ' )) {
36+ $ table ->addColumn ('column_settings ' , Types::JSON , [
37+ 'notnull ' => false ,
38+ 'comment ' => 'JSON structure for column-specific settings like order, visibility, etc. ' ,
39+ ]);
40+ }
41+
42+ return $ schema ;
43+ }
44+
45+ return null ;
46+ }
47+
48+ /**
49+ * @param IOutput $output
50+ * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
51+ * @param array $options
52+ */
53+ public function postSchemaChange (IOutput $ output , Closure $ schemaClosure , array $ options ): void {
54+ /** @var ISchemaWrapper $schema */
55+ $ schema = $ schemaClosure ();
56+
57+ if ($ schema ->hasTable ('tables_views ' )) {
58+ // Get database connection
59+ $ connection = \OC ::$ server ->get (\OCP \IDBConnection::class);
60+
61+ // Get all views
62+ $ qb = $ connection ->getQueryBuilder ();
63+ $ qb ->select ('id ' , 'columns ' )
64+ ->from ('tables_views ' )
65+ ->where ($ qb ->expr ()->isNotNull ('columns ' ));
66+
67+ $ result = $ qb ->executeQuery ();
68+ $ views = $ result ->fetchAll ();
69+
70+ // Update each view
71+ foreach ($ views as $ view ) {
72+ if (empty ($ view ['columns ' ])) {
73+ continue ;
74+ }
75+
76+ // Parse existing columns JSON
77+ $ columns = json_decode ($ view ['columns ' ], true );
78+ if (!is_array ($ columns )) {
79+ continue ;
80+ }
81+
82+ // Create new column_settings structure
83+ $ columnSettings = [];
84+ foreach ($ columns as $ order => $ columnId ) {
85+ $ columnSettings [$ columnId ] = [
86+ 'order ' => $ order ,
87+ ];
88+ }
89+
90+ // Update the view with new column_settings
91+ $ updateQb = $ connection ->getQueryBuilder ();
92+ $ updateQb ->update ('tables_views ' )
93+ ->set ('column_settings ' , $ updateQb ->createNamedParameter (json_encode ($ columnSettings )))
94+ ->where ($ updateQb ->expr ()->eq ('id ' , $ updateQb ->createNamedParameter ($ view ['id ' ], \PDO ::PARAM_INT )));
95+
96+ $ updateQb ->executeStatement ();
97+ }
98+
99+ $ result ->closeCursor ();
100+ }
101+ }
102+ }
0 commit comments