12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- def tables_to_archive(self, database=None):
- """
- For a given database, return the list of tables that are eligible to
- archiving.
- - Retrieve tables if needed (*, or empty)
- - Check that tables has 'deleted_at' column (deleted_column
- parameter)
- - Exclude tables in excluded_tables
- - Reorder tables depending foreign key
- """
- if database is None:
- logging.warning("Can not call tables_to_archive on None database")
- return []
- if database in self._tables_to_archive:
- return self._tables_to_archive[database]
- database_tables = [
- v[0] for (i, v) in enumerate(self.get_database_tables(database))
- ]
- logging.info("Tables list of database '%s': %s", database,
- database_tables)
- # Step 1: is to get all the tables we want to archive
- # no table specified or jocker used means we want all tables
- # else we filter against the tables specified
- if self.tables is None or self.tables == '*':
- self._tables_to_archive[database] = database_tables
- else:
- self._tables_to_archive[database] = \
- [t for t in re.split(',|;|\n', self.tables.replace(' ', ''))
- if t in database_tables]
- # Step 2: verify that all tables have the deleted column 'deleted_at'
- logging.debug("Verifying that tables have the '%s' column",
- self.deleted_column)
- tables = []
- for table in self._tables_to_archive[database]:
- if not self.table_has_deleted_column(table=table,
- database=database):
- logging.debug(
- "Table '%s' has no column named '%s',"
- " ignoring it", table, self.deleted_column)
- continue
- tables.append(table)
- # update self._tables_to_archive with the filtered tables
- self._tables_to_archive[database] = tables
- # Step 3: then exclude the one explicitly given
- excluded_tables_regex = "^(" + "|".join(self.excluded_tables) + ")$"
- logging.debug("Ignoring tables matching '%s'", excluded_tables_regex)
- self._tables_to_archive[database] = [
- t for t in self._tables_to_archive[database]
- if not re.match(excluded_tables_regex, t)
- ]
- # Step 4 for each table retrieve child tables referencing the parent
- # table and order them childs first, parents then
- sorted_tables = self.sort_tables(
- database=database, tables=self._tables_to_archive[database])
- self._tables_to_archive[database] = sorted_tables
- logging.debug(
- "Tables ordered depending foreign key dependencies: "
- "'%s'", self._tables_to_archive[database])
- return self._tables_to_archive[database]
|