db_2.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. def tables_to_archive(self, database=None):
  2. """
  3. For a given database, return the list of tables that are eligible to
  4. archiving.
  5. - Retrieve tables if needed (*, or empty)
  6. - Check that tables has 'deleted_at' column (deleted_column
  7. parameter)
  8. - Exclude tables in excluded_tables
  9. - Reorder tables depending foreign key
  10. """
  11. if database is None:
  12. logging.warning("Can not call tables_to_archive on None database")
  13. return []
  14. if database in self._tables_to_archive:
  15. return self._tables_to_archive[database]
  16. database_tables = [
  17. v[0] for (i, v) in enumerate(self.get_database_tables(database))
  18. ]
  19. logging.info("Tables list of database '%s': %s", database,
  20. database_tables)
  21. # Step 1: is to get all the tables we want to archive
  22. # no table specified or jocker used means we want all tables
  23. # else we filter against the tables specified
  24. if self.tables is None or self.tables == '*':
  25. self._tables_to_archive[database] = database_tables
  26. else:
  27. self._tables_to_archive[database] = \
  28. [t for t in re.split(',|;|\n', self.tables.replace(' ', ''))
  29. if t in database_tables]
  30. # Step 2: verify that all tables have the deleted column 'deleted_at'
  31. logging.debug("Verifying that tables have the '%s' column",
  32. self.deleted_column)
  33. tables = []
  34. for table in self._tables_to_archive[database]:
  35. if not self.table_has_deleted_column(table=table,
  36. database=database):
  37. logging.debug(
  38. "Table '%s' has no column named '%s',"
  39. " ignoring it", table, self.deleted_column)
  40. continue
  41. tables.append(table)
  42. # update self._tables_to_archive with the filtered tables
  43. self._tables_to_archive[database] = tables
  44. # Step 3: then exclude the one explicitly given
  45. excluded_tables_regex = "^(" + "|".join(self.excluded_tables) + ")$"
  46. logging.debug("Ignoring tables matching '%s'", excluded_tables_regex)
  47. self._tables_to_archive[database] = [
  48. t for t in self._tables_to_archive[database]
  49. if not re.match(excluded_tables_regex, t)
  50. ]
  51. # Step 4 for each table retrieve child tables referencing the parent
  52. # table and order them childs first, parents then
  53. sorted_tables = self.sort_tables(
  54. database=database, tables=self._tables_to_archive[database])
  55. self._tables_to_archive[database] = sorted_tables
  56. logging.debug(
  57. "Tables ordered depending foreign key dependencies: "
  58. "'%s'", self._tables_to_archive[database])
  59. return self._tables_to_archive[database]