(self, delete_db=True)
| 122 | # Rebuild sql cache |
| 123 | @util.Noparallel() |
| 124 | def rebuildDb(self, delete_db=True): |
| 125 | self.log.info("Rebuilding db...") |
| 126 | self.has_db = self.isFile("dbschema.json") |
| 127 | if not self.has_db: |
| 128 | return False |
| 129 | |
| 130 | schema = self.loadJson("dbschema.json") |
| 131 | db_path = self.getPath(schema["db_file"]) |
| 132 | if os.path.isfile(db_path) and delete_db: |
| 133 | if self.db: |
| 134 | self.closeDb() # Close db if open |
| 135 | time.sleep(0.5) |
| 136 | self.log.info("Deleting %s" % db_path) |
| 137 | try: |
| 138 | os.unlink(db_path) |
| 139 | except Exception as err: |
| 140 | self.log.error("Delete error: %s" % err) |
| 141 | |
| 142 | if not self.db: |
| 143 | self.db = self.openDb() |
| 144 | self.event_db_busy = gevent.event.AsyncResult() |
| 145 | |
| 146 | self.log.info("Creating tables...") |
| 147 | |
| 148 | # raise DbTableError if not valid |
| 149 | self.db.checkTables() |
| 150 | |
| 151 | cur = self.db.getCursor() |
| 152 | cur.logging = False |
| 153 | s = time.time() |
| 154 | self.log.info("Getting db files...") |
| 155 | db_files = list(self.getDbFiles()) |
| 156 | num_imported = 0 |
| 157 | num_total = len(db_files) |
| 158 | num_error = 0 |
| 159 | |
| 160 | self.log.info("Importing data...") |
| 161 | try: |
| 162 | if num_total > 100: |
| 163 | self.site.messageWebsocket(_["Database rebuilding...<br>Imported {0} of {1} files (error: {2})..."].format("0000", num_total, num_error), "rebuild", 0) |
| 164 | for file_inner_path, file_path in db_files: |
| 165 | try: |
| 166 | if self.updateDbFile(file_inner_path, file=open(file_path, "rb"), cur=cur): |
| 167 | num_imported += 1 |
| 168 | except Exception as err: |
| 169 | self.log.error("Error importing %s: %s" % (file_inner_path, Debug.formatException(err))) |
| 170 | num_error += 1 |
| 171 | |
| 172 | if num_imported and num_imported % 100 == 0: |
| 173 | self.site.messageWebsocket( |
| 174 | _["Database rebuilding...<br>Imported {0} of {1} files (error: {2})..."].format(num_imported, num_total, num_error), |
| 175 | "rebuild", |
| 176 | int(float(num_imported) / num_total * 100) |
| 177 | ) |
| 178 | time.sleep(0.001) # Context switch to avoid UI block |
| 179 | |
| 180 | finally: |
| 181 | cur.close() |
no test coverage detected