1212use Closure ;
1313use OCA \Tables \Vendor \Symfony \Component \Uid \Uuid ;
1414use OCP \DB \ISchemaWrapper ;
15+ use OCP \DB \QueryBuilder \IQueryBuilder ;
1516use OCP \DB \Types ;
1617use OCP \IDBConnection ;
1718use OCP \Migration \IOutput ;
@@ -22,6 +23,7 @@ class Version2020Date20260513185340 extends SimpleMigrationStep {
2223 private const TARGET_TABLE = 'tables_columns ' ;
2324 private const COL_ID = 'id ' ;
2425 private const COL_UUID = 'uuid ' ;
26+ private const COL_SELECTION_OPTIONS = 'selection_options ' ;
2527 private const INDEX_NAME = 'tables_col_uuid_uniq ' ;
2628
2729 public function __construct (
@@ -54,16 +56,52 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
5456 return $ schema ;
5557 }
5658
59+ private function applyColumnOptionsUpdateIfNecessary (IQueryBuilder $ query , int $ columnId , ?string $ rawSelectionOptions ): void {
60+ $ columnSelectionOptions = trim ($ rawSelectionOptions ?? '' );
61+ if ($ columnSelectionOptions === '' ) {
62+ return ;
63+ }
64+
65+ $ selectionOptions = \json_decode ($ columnSelectionOptions , true );
66+ if (!is_array ($ selectionOptions ) || empty ($ selectionOptions )) {
67+ return ;
68+ }
69+
70+ foreach ($ selectionOptions as &$ selectionOption ) {
71+ if (!isset ($ selectionOption ['uuid ' ])) {
72+ $ selectionOption ['uuid ' ] = Uuid::v7 ()->toRfc4122 ();
73+ }
74+ }
75+
76+ $ updatedSelectionOptions = json_encode ($ selectionOptions );
77+ unset($ selectionOption );
78+ $ query ->setParameters (
79+ [
80+ 'columnLocalId ' => $ columnId ,
81+ 'columnSelectionOptions ' => $ updatedSelectionOptions ,
82+ ],
83+ [
84+ Types::INTEGER ,
85+ Types::TEXT ,
86+ ]
87+ );
88+ $ query ->executeStatement ();
89+ }
90+
5791 #[Override]
5892 public function postSchemaChange (IOutput $ output , Closure $ schemaClosure , array $ options ): void {
93+ $ qbColUuidUpdate = $ this ->db ->getQueryBuilder ();
94+ $ qbColUuidUpdate ->update (self ::TARGET_TABLE )
95+ ->set (self ::COL_UUID , $ qbColUuidUpdate ->createParameter ('columnUuid ' ))
96+ ->where ($ qbColUuidUpdate ->expr ()->eq (self ::COL_ID , $ qbColUuidUpdate ->createParameter ('columnLocalId ' )));
5997
60- $ qbUpdate = $ this ->db ->getQueryBuilder ();
61- $ qbUpdate ->update (self ::TARGET_TABLE )
62- ->set (self ::COL_UUID , $ qbUpdate ->createParameter ('columnUuid ' ))
63- ->where ($ qbUpdate ->expr ()->eq (self ::COL_ID , $ qbUpdate ->createParameter ('columnLocalId ' )));
98+ $ qbColOptionsUuidUpdate = $ this ->db ->getQueryBuilder ();
99+ $ qbColOptionsUuidUpdate ->update (self ::TARGET_TABLE )
100+ ->set (self ::COL_SELECTION_OPTIONS , $ qbColOptionsUuidUpdate ->createParameter ('columnSelectionOptions ' ))
101+ ->where ($ qbColOptionsUuidUpdate ->expr ()->eq (self ::COL_ID , $ qbColOptionsUuidUpdate ->createParameter ('columnLocalId ' )));
64102
65103 $ qbSelect = $ this ->db ->getQueryBuilder ();
66- $ qbSelect ->select (self ::COL_ID )
104+ $ qbSelect ->select (self ::COL_ID , self :: COL_SELECTION_OPTIONS )
67105 ->from (self ::TARGET_TABLE );
68106 $ select = $ qbSelect ->executeQuery ();
69107
@@ -72,8 +110,9 @@ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array
72110
73111 try {
74112 $ this ->db ->beginTransaction ();
75- while (($ columnId = $ select ->fetchOne ()) !== false ) {
76- $ qbUpdate ->setParameters (
113+ while (($ columnData = $ select ->fetchAssociative ()) !== false ) {
114+ $ columnId = $ columnData [self ::COL_ID ];
115+ $ qbColUuidUpdate ->setParameters (
77116 [
78117 'columnLocalId ' => (int )$ columnId ,
79118 'columnUuid ' => Uuid::v7 ()->toRfc4122 (),
@@ -83,7 +122,10 @@ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array
83122 Types::STRING ,
84123 ]
85124 );
86- $ qbUpdate ->executeStatement ();
125+ $ qbColUuidUpdate ->executeStatement ();
126+
127+ $ this ->applyColumnOptionsUpdateIfNecessary ($ qbColOptionsUuidUpdate , (int )$ columnId , $ columnData [self ::COL_SELECTION_OPTIONS ]);
128+
87129 $ updates ++;
88130 if ($ updates % $ writeBatches === 0 ) {
89131 $ this ->db ->commit ();
0 commit comments