99namespace OCA \Polls \Command \Db ;
1010
1111use Doctrine \DBAL \Schema \Schema ;
12- use OCA \Polls \Db \TableManager ;
13- use OCA \Polls \Db \IndexManager ;
12+ use OCA \Polls \Db \V2 \ TableManager ;
13+ use OCA \Polls \Db \V2 \ IndexManager ;
1414use OCA \Polls \Command \Command ;
1515use OCP \IDBConnection ;
1616
@@ -52,32 +52,33 @@ protected function runCommands(): int {
5252 $ this ->deleteGenericIndices ();
5353 $ this ->deleteUniqueIndices ();
5454 $ this ->deleteNamedIndices ();
55-
56- $ this ->printComment ('Step 2. Remove all orphaned tables and columns ' );
57- $ this ->removeObsoleteTables ();
58- $ this ->removeObsoleteColumns ();
59-
6055 $ this ->connection ->migrateToSchema ($ this ->schema );
6156
57+ $ this ->printComment ('Step 2. Tidy records before rebuilding the schema ' );
58+ $ this ->fixNullish ();
59+ $ this ->cleanTables ();
60+
6261 $ this ->printComment ('Step 3. Create or update tables to current shema ' );
6362 $ this ->createOrUpdateSchema ();
63+ $ this ->connection ->migrateToSchema ($ this ->schema );
6464
65+ $ this ->printComment ('Step 4. Remove orphaned tables and columns ' );
66+ $ this ->dropObsoleteTables ();
67+ $ this ->dropObsoleteColumns ();
6568 $ this ->connection ->migrateToSchema ($ this ->schema );
6669
67- $ this ->printComment ('Step 4. set hashes for votes and options ' );
70+ $ this ->printComment ('Step 5. Validate and fix records ' );
6871 $ this ->migrateOptionsToHash ();
69-
70- $ this ->printComment ('Step 5. Remove invalid records (orphaned and duplicates) ' );
71- $ this ->cleanTables ();
72+ $ this ->setLastInteraction ();
7273
7374 $ this ->printComment ('Step 6. Recreate unique indices and foreign key constraints ' );
7475 $ this ->addForeignKeyConstraints ();
7576 $ this ->addUniqueIndices ();
77+ $ this ->connection ->migrateToSchema ($ this ->schema );
7678
79+ $ this ->printComment ('Rebuild finished. The database structure is now up to date. ' );
7780 $ this ->printComment ('Execute \'occ db:add-missing-indices \' to add missing optional indices ' );
7881
79- $ this ->connection ->migrateToSchema ($ this ->schema );
80-
8182 return 0 ;
8283 }
8384
@@ -90,6 +91,14 @@ private function addForeignKeyConstraints(): void {
9091 $ this ->printInfo ($ messages , ' ' );
9192 }
9293
94+ private function fixNullish (): void {
95+ $ this ->printComment (' - Fix nullish values ' );
96+ $ messages = $ this ->tableManager ->fixNullishShares ();
97+ $ this ->printInfo ($ messages , ' ' );
98+
99+ $ messages = $ this ->tableManager ->fixNullishPollGroupRelations ();
100+ $ this ->printInfo ($ messages , ' ' );
101+ }
93102 /**
94103 * Create index for $table
95104 */
@@ -118,7 +127,7 @@ private function migrateOptionsToHash(): void {
118127 $ this ->printInfo ($ messages , ' ' );
119128 }
120129
121- private function removeObsoleteColumns (): void {
130+ private function dropObsoleteColumns (): void {
122131 $ this ->printComment (' - Drop orphaned columns ' );
123132 $ messages = $ this ->tableManager ->removeObsoleteColumns ();
124133 $ this ->printInfo ($ messages , ' ' );
@@ -127,7 +136,7 @@ private function removeObsoleteColumns(): void {
127136 /**
128137 * Remove obsolete tables if they still exist
129138 */
130- private function removeObsoleteTables (): void {
139+ private function dropObsoleteTables (): void {
131140 $ this ->printComment (' - Drop orphaned tables ' );
132141 $ messages = $ this ->tableManager ->removeObsoleteTables ();
133142 $ this ->printInfo ($ messages , ' ' );
@@ -136,8 +145,8 @@ private function removeObsoleteTables(): void {
136145 /**
137146 * Initialize last poll interactions timestamps
138147 */
139- public function resetLastInteraction (): void {
140- $ messages = $ this ->tableManager ->resetLastInteraction ();
148+ public function setLastInteraction (): void {
149+ $ messages = $ this ->tableManager ->setLastInteraction ();
141150 $ this ->printInfo ($ messages , ' ' );
142151 }
143152
@@ -146,9 +155,9 @@ public function resetLastInteraction(): void {
146155 */
147156 private function cleanTables (): void {
148157 $ this ->printComment (' - Remove orphaned records ' );
149- $ orphaned = $ this ->tableManager ->removeOrphaned ();
150- foreach ($ orphaned as $ table => $ count ) {
151- $ this ->printInfo (" Removed $ count orphaned records from $ table " );
158+ $ messages = $ this ->tableManager ->removeOrphaned ();
159+ foreach ($ messages as $ message ) {
160+ $ this ->printInfo (" $ message " );
152161 }
153162
154163 $ this ->printComment (' - Remove duplicates ' );
@@ -187,7 +196,7 @@ private function deleteUniqueIndices(): void {
187196 * remove all named indices
188197 */
189198 private function deleteNamedIndices (): void {
190- $ this ->printComment (' - Remove common indices ' );
199+ $ this ->printComment (' - Remove optional indices ' );
191200 $ messages = $ this ->indexManager ->removeNamedIndices ();
192201 $ this ->printInfo ($ messages , ' - ' );
193202 }
0 commit comments