db_3.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. def create_archive_db(self, database=None):
  2. """
  3. Create the Destination database
  4. It checks that if the Destination database exists, the show create
  5. statement are the same than Source which is useful to detect Db schema
  6. upgrade
  7. """
  8. # Check if db exists
  9. archive_db_exists = self.archive_db_exists(database=database)
  10. # retrieve source db create statement
  11. # if archive database exists, compare create statement
  12. # else use the statement to create it
  13. src_db_create_statement = self.get_src_create_db_statement(
  14. database=database)
  15. if archive_db_exists:
  16. logging.debug("Destination DB has '%s' database",
  17. self.archive_db_name)
  18. dst_db_create_statement = self.get_dst_create_db_statement(
  19. database=self.archive_db_name)
  20. # compare create statement substituing db name in dst (arbitrary
  21. # choice)
  22. to_compare_dst_db_create_statement = re.sub(
  23. 'DATABASE `{dst_db}`'.format(dst_db=self.archive_db_name),
  24. 'DATABASE `{src_db}`'.format(src_db=database),
  25. dst_db_create_statement)
  26. if src_db_create_statement == to_compare_dst_db_create_statement:
  27. logging.info("source and destination database are identical")
  28. else:
  29. logging.debug(
  30. difflib.SequenceMatcher(
  31. None, src_db_create_statement,
  32. to_compare_dst_db_create_statement))
  33. raise db_errors.OSArchiverNotEqualDbCreateStatements
  34. else:
  35. logging.debug("'%s' on remote DB does not exists",
  36. self.archive_db_name)
  37. sql = re.sub('`{db}`'.format(db=database),
  38. '`{db}`'.format(db=self.archive_db_name),
  39. src_db_create_statement)
  40. self.db_request(sql=sql)
  41. if not self.dry_run:
  42. logging.debug("Successfully created '%s'",
  43. self.archive_db_name)