Example #1
0
    def _RepairRepopulateTables(
            self, table_names,
            cursor_transaction_wrapper: HydrusDBBase.DBCursorTransactionWrapper
    ):

        self.Repopulate()

        cursor_transaction_wrapper.CommitAndBegin()
Example #2
0
    def _RepairRepopulateTables(
            self, repopulate_table_names,
            cursor_transaction_wrapper: HydrusDBBase.DBCursorTransactionWrapper
    ):

        for service_id in self._GetServiceIdsWeGenerateDynamicTablesFor():

            table_generation_dict = self._GetServiceTableGenerationDict(
                service_id)

            this_service_table_names = set(table_generation_dict.keys())

            this_service_needs_repopulation = len(
                this_service_table_names.intersection(
                    repopulate_table_names)) > 0

            if this_service_needs_repopulation:

                self._service_ids_to_applicable_service_ids = None
                self._service_ids_to_interested_service_ids = None

                self.Regen((service_id, ))

                cursor_transaction_wrapper.CommitAndBegin()
Example #3
0
    def Repair(
            self, current_db_version,
            cursor_transaction_wrapper: HydrusDBBase.DBCursorTransactionWrapper
    ):

        # core, initial tables first

        table_generation_dict = self._GetInitialTableGenerationDict()

        missing_table_rows = [
            (table_name, create_query_without_name)
            for (table_name, (create_query_without_name,
                              version_added)) in table_generation_dict.items()
            if version_added <= current_db_version
            and not self._TableExists(table_name)
        ]

        if len(missing_table_rows) > 0:

            missing_table_names = sorted([
                missing_table_row[0]
                for missing_table_row in missing_table_rows
            ])

            critical_table_names = self._GetCriticalTableNames()

            missing_critical_table_names = set(
                missing_table_names).intersection(critical_table_names)

            if len(missing_critical_table_names) > 0:

                message = 'Unfortunately, this database is missing one or more critical tables! This database is non functional and cannot be repaired. Please check out "install_dir/db/help my db is broke.txt" for the next steps.'

                raise HydrusExceptions.DBAccessException(message)

            self._PresentMissingTablesWarningToUser(missing_table_names)

            for (table_name, create_query_without_name) in missing_table_rows:

                self._Execute(create_query_without_name.format(table_name))

                cursor_transaction_wrapper.CommitAndBegin()

            self._RepairRepopulateTables(missing_table_names,
                                         cursor_transaction_wrapper)

            cursor_transaction_wrapper.CommitAndBegin()

        # now indices for those tables

        index_generation_dict = self._GetInitialIndexGenerationDict()

        missing_index_rows = [
            (self._GenerateIndexName(table_name,
                                     columns), table_name, columns, unique)
            for (table_name, columns, unique, version_added
                 ) in self._FlattenIndexGenerationDict(index_generation_dict)
            if version_added <= current_db_version
            and not self._IndexExists(table_name, columns)
        ]

        if len(missing_index_rows):

            self._PresentMissingIndicesWarningToUser(
                sorted([
                    index_name for (index_name, table_name, columns,
                                    unique) in missing_index_rows
                ]))

            for (index_name, table_name, columns,
                 unique) in missing_index_rows:

                self._CreateIndex(table_name, columns, unique=unique)

                cursor_transaction_wrapper.CommitAndBegin()

        # now do service tables, same thing over again

        table_generation_dict = self._GetServicesTableGenerationDict()

        missing_table_rows = [
            (table_name, create_query_without_name)
            for (table_name, (create_query_without_name,
                              version_added)) in table_generation_dict.items()
            if version_added <= current_db_version
            and not self._TableExists(table_name)
        ]

        if len(missing_table_rows) > 0:

            missing_table_names = sorted([
                missing_table_row[0]
                for missing_table_row in missing_table_rows
            ])

            self._PresentMissingTablesWarningToUser(missing_table_names)

            for (table_name, create_query_without_name) in missing_table_rows:

                self._Execute(create_query_without_name.format(table_name))

                cursor_transaction_wrapper.CommitAndBegin()

            self._RepairRepopulateTables(missing_table_names,
                                         cursor_transaction_wrapper)

            cursor_transaction_wrapper.CommitAndBegin()

        # now indices for those tables

        index_generation_dict = self._GetServicesIndexGenerationDict()

        missing_index_rows = [
            (self._GenerateIndexName(table_name,
                                     columns), table_name, columns, unique)
            for (table_name, columns, unique, version_added
                 ) in self._FlattenIndexGenerationDict(index_generation_dict)
            if version_added <= current_db_version
            and not self._IndexExists(table_name, columns)
        ]

        if len(missing_index_rows):

            self._PresentMissingIndicesWarningToUser(
                sorted([
                    index_name for (index_name, table_name, columns,
                                    unique) in missing_index_rows
                ]))

            for (index_name, table_name, columns,
                 unique) in missing_index_rows:

                self._CreateIndex(table_name, columns, unique=unique)

                cursor_transaction_wrapper.CommitAndBegin()