Browse Source

Update DB\Sqlite\dropIndex() method

Fix for PRIMARY KEY.
Visman 3 years ago
parent
commit
8f838ce769
1 changed files with 52 additions and 12 deletions
  1. 52 12
      app/Core/DB/Sqlite.php

+ 52 - 12
app/Core/DB/Sqlite.php

@@ -552,14 +552,17 @@ class Sqlite
         unset($schema['TABLE']['FIELDS'][$field]);
         unset($schema['TABLE']['FIELDS'][$field]);
 
 
         $tmpTable = $this->createTmpTable($schema, $table);
         $tmpTable = $this->createTmpTable($schema, $table);
-        $result   = \is_string($tmpTable);
+
+        if (! \is_string($tmpTable)) {
+            return false;
+        }
 
 
         $tmp   = '"' . \implode('", "', \array_keys($schema['TABLE']['FIELDS'])) . '"';
         $tmp   = '"' . \implode('", "', \array_keys($schema['TABLE']['FIELDS'])) . '"';
         $query = "INSERT INTO \"{$tmpTable}\" ({$tmp})
         $query = "INSERT INTO \"{$tmpTable}\" ({$tmp})
             SELECT {$tmp}
             SELECT {$tmp}
             FROM \"{$table}\"";
             FROM \"{$table}\"";
 
 
-        return $result && $this->tmpToTable($schema, $query);
+        return $this->tmpToTable($schema, $query);
     }
     }
 
 
     /**
     /**
@@ -594,19 +597,20 @@ class Sqlite
         }
         }
 
 
         if ('PRIMARY' === $index) {
         if ('PRIMARY' === $index) {
-            $schema = $this->tableSchema($table);
-
+            $schema                      = $this->tableSchema($table);
             $schema['TABLE']['OTHERS'][] = 'PRIMARY KEY (' . $this->replIdxs($fields) . ')';
             $schema['TABLE']['OTHERS'][] = 'PRIMARY KEY (' . $this->replIdxs($fields) . ')';
+            $tmpTable                    = $this->createTmpTable($schema, $table);
 
 
-            $tmpTable = $this->createTmpTable($schema, $table);
-            $result   = \is_string($tmpTable);
+            if (! \is_string($tmpTable)) {
+                return false;
+            }
 
 
             $tmp   = '"' . \implode('", "', \array_keys($schema['TABLE']['FIELDS'])) . '"';
             $tmp   = '"' . \implode('", "', \array_keys($schema['TABLE']['FIELDS'])) . '"';
             $query = "INSERT INTO \"{$tmpTable}\" ({$tmp})
             $query = "INSERT INTO \"{$tmpTable}\" ({$tmp})
                 SELECT {$tmp}
                 SELECT {$tmp}
                 FROM \"{$table}\"";
                 FROM \"{$table}\"";
 
 
-            return $result && $this->tmpToTable($schema, $query);
+            return $this->tmpToTable($schema, $query);
         } else {
         } else {
             $index  = $table . '_' . $index;
             $index  = $table . '_' . $index;
 
 
@@ -614,9 +618,9 @@ class Sqlite
 
 
             $unique = $unique ? 'UNIQUE' : '';
             $unique = $unique ? 'UNIQUE' : '';
             $query  = "CREATE {$unique} INDEX \"{$index}\" ON \"{$table}\" (" . $this->replIdxs($fields) . ')';
             $query  = "CREATE {$unique} INDEX \"{$index}\" ON \"{$table}\" (" . $this->replIdxs($fields) . ')';
-        }
 
 
-        return false !== $this->db->exec($query);
+            return false !== $this->db->exec($query);
+        }
     }
     }
 
 
     /**
     /**
@@ -630,11 +634,47 @@ class Sqlite
             return true;
             return true;
         }
         }
 
 
-        $index = $table . '_' . ('PRIMARY' === $index ? 'pkey' : $index);
+        if ('PRIMARY' === $index) {
+            $schema = $this->tableSchema($table);
+
+            foreach ($schema['TABLE']['FIELDS'] as &$value) {
+                $value = \preg_replace(
+                    '%\bPRIMARY\s+KEY\s+(?:(?:ASC|DESC)\s+)?(?:ON\s+CONFLICT\s+(?:ROLLBACK|ABORT|FAIL|IGNORE|REPLACE)\s+)?(?:AUTOINCREMENT\s+)?%si',
+                    '',
+                    $value
+                );
+            }
+
+            unset($value);
+
+            $tmp = [];
 
 
-        $this->nameCheck($index);
+            foreach ($schema['TABLE']['OTHERS'] as $value) {
+                if (\preg_match('%\bPRIMARY\s+KEY\b%si', $value)) {
+                    continue;
+                }
+
+                $tmp[] = $value;
+            }
+
+            $schema['TABLE']['OTHERS'] = $tmp;
+            $tmpTable                  = $this->createTmpTable($schema, $table);
 
 
-        return false !== $this->db->exec("DROP INDEX \"{$index}\"");
+            if (! \is_string($tmpTable)) {
+                return false;
+            }
+
+            $tmp   = '"' . \implode('", "', \array_keys($schema['TABLE']['FIELDS'])) . '"';
+            $query = "INSERT INTO \"{$tmpTable}\" ({$tmp})
+                SELECT {$tmp}
+                FROM \"{$table}\"";
+
+            return $this->tmpToTable($schema, $query);
+        } else {
+            $this->nameCheck($index);
+
+            return false !== $this->db->exec("DROP INDEX \"{$table}_{$index}\"");
+        }
     }
     }
 
 
     /**
     /**