db_5.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. def create_archive_table(self, database=None, table=None):
  2. """
  3. Create the archive table in the archive database.
  4. It checks that Source and Destination table are the identical.
  5. """
  6. # Call create db if archive_db_name is None
  7. if self.archive_db_name is None:
  8. self.create_archive_db(database=database)
  9. else:
  10. logging.debug("Archive db is '%s'", self.archive_db_name)
  11. # Check if table exists
  12. archive_table_exists = False
  13. if self.archive_db_exists:
  14. archive_table_exists = self.archive_table_exists(database=database,
  15. table=table)
  16. # retrieve source tabe create statement
  17. # if archive table exists, compare create statement
  18. # else use the statement to create it
  19. src_create_table_statement = self.get_src_create_table_statement(
  20. database=database, table=table)
  21. if archive_table_exists:
  22. logging.debug("Remote DB has '%s.%s' table", self.archive_db_name,
  23. self.archive_table_name)
  24. dst_table_create_statement = self.get_dst_create_table_statement(
  25. database=self.archive_db_name, table=self.archive_table_name)
  26. self.compare_src_and_dst_create_table_statement(
  27. src_statement=src_create_table_statement,
  28. dst_statement=dst_table_create_statement,
  29. src_table=table,
  30. dst_table=self.archive_table_name)
  31. else:
  32. logging.debug("'%s' table on remote DB does not exists",
  33. self.archive_table_name)
  34. sql = re.sub(
  35. 'TABLE `{table}`'.format(table=table),
  36. 'TABLE `{table}`'.format(table=self.archive_table_name),
  37. src_create_table_statement)
  38. self.db_request(sql=sql,
  39. database=self.archive_db_name,
  40. foreign_key_check=False)
  41. if not self.dry_run:
  42. logging.debug("Successfully created '%s.%s'",
  43. self.archive_db_name, self.archive_table_name)